Giannis Grivas
Giannis Grivas

Reputation: 3412

How to force NSwag to include custom response codes from xml comments at the auto-generated swagger json of a web API call

This is the definition added in clean1.csproj file based on NSwag's documentation

  <Target Name="AfterBuild">
<Exec Command="$(NSwagExe) webapi2swagger /assembly:$(OutDir)/Clean1.dll /referencepath: $(ProjectDir)  /output:$(ProjectDir)/clean1swagger.json" />   

The problem is that only 200 response code is being generated like:

        ],
    "responses": {
      "200": {
        "description": "",
        "schema": {
          "$ref": "#/definitions/Product"
        },
        "x-nullable": true
      }
    }

Here are the XML comments at the controller's demo call.

    /// <summary>
    /// Gets a product by Id
    /// </summary>
    /// <remarks>
    /// Remarks-description text.
    /// </remarks>
    /// <response code="200">test 200</response>
    /// <response code="201">test 201/response>
    /// <response code="400">test 400</response></response>
    [HttpGet]
    [ResponseType(typeof(Product))]
    public IHttpActionResult GetProduct(int id)
    {
        var product = products.FirstOrDefault((p) => p.Id == id);
        if (product == null)
        {
            return NotFound();
        }
        return Ok(product);
    }

The json should include and generate automatically the other responses.

Upvotes: 0

Views: 2873

Answers (1)

Rico Suter
Rico Suter

Reputation: 11858

This is currently not possible. We considered adding this feature but in most cases you need to specify the type and this cannot be done via xml comments. For now you have to use the SwaggerResponseAttribute for that. But please create an issue on Github so that the feature is considered to be added in the future...

Upvotes: 3

Related Questions