facebook

Blog

Stay updated

Let’s automatize the boring part of the API development with Swagger and .Net
Documenting API with Swagger
Tuesday, February 19, 2019

I recently worked on the backend of an application, for one of our clients. Some tasks concerned the introduction of new API to be noticed with Swagger and, specifically, one of these requires to habilitate Swagger only for the debug environment and to disable it for the production environment.

But what is Swagger? It is a tools set, that accompanies the specification OpenApi (OAS), namely the standard for API Rest description, to create and document these API. The OAS standard is independent from the programming language, and it has been designed to be comprehensible both for people and for computers. The goal is to make understandable what is made available from a services, with no need to access to source codes or to an additional documentation.

In a .Net project it’s possible to integrate Swagger installing, via Nuget, the Open Source package Swashbuckle. Once installed, we will find in the folder App_start the file SwaggerConfig.cs.

This file contains many useful comments for the configuration of Swagger for our application. In particular, we can indicate a XML file, where will be saved the comments in which we can save our API.

c.IncludeXmlComments(GetXmlCommentsPath());

To avoid mistakes, we need to go in project properties, tab Build, section Output, flag the option “XML documentation file”, indicate the destination file XML.

As soon as we finish this configuration, it’s possible to notice the API with following tags :

  • Summary
/// <summary>
/// Esempio Commenti Swagger
/// </summary>
  • Remarks
/// <remarks>Osservazioni </remarks>
  • Parameter
/// <param name="id" cref="int">Value id</param>
/// <param name="value" cref="string">Value string</param>
  • Response
///<response code="500">Ops!!! Error 500 </response>

We consider following action that makes a GET.

/// <summary>
/// Esempio Commenti Swagger
/// </summary>
/// <param name="id" cref="int">Value id</param>
/// <param name="value" cref="string">Value string</param>
/// <remarks>Osservazioni </remarks>
///<response code="500">Ops!!! Error 500 </response>
public string Get(int id, string value)
{
    return value+id.ToString();
}

We can see the result of the notice in the SwaggerUI reachable to the address: [host]:[port]/swagger.

How can we configure Swagger, making it available only for development environment and disabling it for the production environment? There are some simple steps to do. We define in the file web.config a new key inside the tag <appsettings> for first:

<appSettings>
    <add key="DisableSwagger" value="False" />
</appSettings>

Later, inside the file SwaggerConfig.cs and specifically inside the methos Register(), we insert a control on the key value, previously defined.

if (ConfigurationManager.AppSettings["DisableSwagger"] == "True")
{
    return;
}

We create then a transformation rule inside the Web.Release.config, thanks to which the parameter DisableSwagger will be set at value true, so that the condition will not take place for the production environment.

<add key="DisableSwagger" value="True" xdt:Transform="Replace" xdt:Locator="Match(key)" />

If we right click on Web.Release.config we can see the effect of this transformation and, selecting “Preview Transform”

We will obtan the intended effect:

Fast and simple! I wish it could be useful to you, as it was for me.

See you next