Swashbuckle Swagger - 如何注释内容类型?

Luk*_*ett 15 asp.net-web-api2 swagger-2.0 swashbuckle

如何对ASP.NET WebAPI操作进行注释,以便swagger元数据包含我的资源支持的内容类型?

具体来说,我希望文档显示我的一个资源可以返回'原始' application/json,application/xml但现在也返回一个新格式,application/vnd.blah+json或者+xml.

OzB*_*Bob 43

扩展@ VisualBean的答案

在Controller的API方法中,您可以使用以下代码来设置属性,如:

[SwaggerResponseContentType(responseType:"application/pdf", Exclusive=true)]
public HttpResponseMessage GetAuthorityForm(string id)
{
....
Run Code Online (Sandbox Code Playgroud)

注意:'Exclusive = true'将删除所有其他内容类型,否则使用新属性将在Swagger UI下拉列表中添加新的响应内容类型.它不会仅修改文档中的Controller或API.

SwaggerConfig.cs

 GlobalConfiguration.Configuration
            .EnableSwagger(c =>
 // Set filter to apply Custom Content Types to operations
 //
 c.OperationFilter<ResponseContentTypeOperationFilter>();
Run Code Online (Sandbox Code Playgroud)

SwaggerReponseContentTypeAttribute.cs

/// <summary>
/// SwaggerResponseContentTypeAttribute
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public sealed class SwaggerResponseContentTypeAttribute : Attribute
{
    /// <summary>
    /// SwaggerResponseContentTypeAttribute
    /// </summary>
    /// <param name="responseType"></param>
    public SwaggerResponseContentTypeAttribute(string responseType)
    {
        ResponseType = responseType;
    }
    /// <summary>
    /// Response Content Type
    /// </summary>
    public string ResponseType { get; private set; }

    /// <summary>
    /// Remove all other Response Content Types
    /// </summary>
    public bool Exclusive { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

ResponseContentTypeOperationFilter.cs

public class ResponseContentTypeOperationFilter : IOperationFilter
{
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
    {
        var requestAttributes = apiDescription.GetControllerAndActionAttributes<SwaggerResponseContentTypeAttribute>().FirstOrDefault();

        if (requestAttributes != null)
        {
            if (requestAttributes.Exclusive)
                operation.produces.Clear();

            operation.produces.Add(requestAttributes.ResponseType);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Vis*_*ean 10

你需要做的是这个; Swagger规范: 您需要将响应类型添加到该操作的响应类型列表中

"produces": [
            "application/json",
            "text/json"
            ],
Run Code Online (Sandbox Code Playgroud)

这可以通过OperationFilter完成

伪码传入!!!

public class CustomResponseType : IOperationFilter
{        
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
    {            
            if (operation.operationId == "myCustomName")
            {
                operation.produces.Add("application/vnd.blah+json");
            }            
    }      
}
Run Code Online (Sandbox Code Playgroud)

可以通过[SwaggerOperation("myCustomName")]注释设置OperationId .

然后在swaggerConfig.cs中应用operationsFilter

c.OperationFilter<CustomResponseType>();
Run Code Online (Sandbox Code Playgroud)

注意:代替operation.operationId == "myCustomName" 您可以为特定路线或其他任何基本上做.ApiDescription提供了大量有关上下文的信息.


bjo*_*vin 5

@ OzBob的答案假设您只想向方法添加单个属性.如果要为同一方法添加和记录多个内容类型,可以使用以下方法:

SwaggerReponseContentTypeAttribute.cs

/// <summary>
/// SwaggerResponseContentTypeAttribute
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class SwaggerResponseContentTypeAttribute : Attribute
{
    /// <summary>
    /// SwaggerResponseContentTypeAttribute
    /// </summary>
    /// <param name="responseType"></param>
    public SwaggerResponseContentTypeAttribute(string responseType)
    {
        ResponseType = responseType;
    }
    /// <summary>
    /// Response Content Type
    /// </summary>
    public string ResponseType { get; private set; }

    /// <summary>
    /// Remove all other Response Content Types
    /// </summary>
    public bool Exclusive { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

ResponseContentTypeOperationFilter.cs

public class ResponseContentTypeOperationFilter : IOperationFilter
{
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription)
    {
        var requestAttributes = apiDescription.GetControllerAndActionAttributes<SwaggerResponseContentTypeAttribute>();

        foreach (var requestAttribute in requestAttributes)
        {
            if (requestAttribute.Exclusive)
            {
                operation.produces.Clear();
            }
            operation.produces.Add(requestAttribute.ResponseType);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,如果在同一方法上有多个属性并且要替换现有内容类型,则应Exclusive = true仅设置第一个属性.否则,您将无法获得文档中的所有属性.

  • 必须将 Exclusive=true 作为第一个属性对开发人员来说并不明显。我认为属性的顺序不应该影响输出。对过滤器中的属性列表进行排序应该消除限制: requestAttributes.OrderByDescending(a=&gt;a.Exclusive) (2认同)