如何强制NSwag在Web API调用的自动生成的swagger json中包含来自xml注释的自定义响应代码

时间:2017-07-09 16:31:42

标签: asp.net-web-api swagger swagger-ui nswag

这是基于NSwag's documentation

在clean1.csproj文件中添加的定义
  <Target Name="AfterBuild">
<Exec Command="$(NSwagExe) webapi2swagger /assembly:$(OutDir)/Clean1.dll /referencepath: $(ProjectDir)  /output:$(ProjectDir)/clean1swagger.json" />   

问题是只生成200个响应代码,如:

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

以下是控制器演示调用中的XML注释。

    /// <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);
    }

json应该包含并自动生成其他响应。

1 个答案:

答案 0 :(得分:3)

目前无法做到这一点。我们考虑添加此功能,但在大多数情况下,您需要指定类型,这不能通过xml注释完成。现在你必须使用SwaggerResponseAttribute。但请在Github上创建一个问题,以便将来考虑添加该功能......