简体   繁体   English

如何在Swashbuckle中添加标题文档?

[英]How do I add header documentation in Swashbuckle?

I am using the library Swashbuckle. 我正在使用图书馆Swashbuckle。 Currently there is no stackoverflow tag for it. 当前没有针对它的stackoverflow标签。

I don't quite understand the documentation here: https://github.com/domaindrivendev/Swashbuckle/blob/master/README.md 我对此处的文档不太了解: https : //github.com/domaindrivendev/Swashbuckle/blob/master/README.md

The section titled "Describing Security/Authorization Schemes" mentions a piece of code 标题为“描述安全性/授权方案”的部分提到了一段代码

   c.ApiKey("apiKey")
                .Description("API Key Authentication")
                .Name("apiKey")
                .In("header");

However when I include this nothing happens. 但是,当我将其包括在内时,什么也不会发生。 I would also like this to only appear on certain API methods. 我也希望仅出现在某些API方法上。 It does mention 它确实提到

"need to be coupled with a corresponding "security" property at the document " “需要与文档上的相应“安全”属性相结合”

But I don't understand this. 但是我不明白。

Can anyone explain? 谁能解释?

I had the same question and solve it this way: 我有同样的问题,并以此方式解决:

At SwaggerConfig: 在SwaggerConfig上:

var applyApiKeySecurity = new ApplyApiKeySecurity(
    key: "ServiceBusToken",
    name: "Authorization",
    description: "Service Bus Token, e.g. 'SharedAccessSignature sr=...&sig=...&se=...&skn=...'",
    @in: "header"
);
applyApiKeySecurity.Apply(c);

ApplyApiKeySecurity: ApplyApiKeySecurity:

public class ApplyApiKeySecurity : IDocumentFilter, IOperationFilter
{
    public ApplyApiKeySecurity(string key, string name, string description, string @in)
    {
        Key = key;
        Name = name;
        Description = description;
        In = @in;
    }

    public string Description { get; private set; }

    public string In { get; private set; }

    public string Key { get; private set; }

    public string Name { get; private set; }

    public void Apply(SwaggerDocument swaggerDoc, SchemaRegistry schemaRegistry, System.Web.Http.Description.IApiExplorer apiExplorer)
    {
        IList<IDictionary<string, IEnumerable<string>>> security = new List<IDictionary<string, IEnumerable<string>>>();
        security.Add(new Dictionary<string, IEnumerable<string>> {
            {Key, new string[0]}
        });

        swaggerDoc.security = security;
    }

    public void Apply(Operation operation, SchemaRegistry schemaRegistry, System.Web.Http.Description.ApiDescription apiDescription)
    {
        operation.parameters = operation.parameters ?? new List<Parameter>();
        operation.parameters.Add(new Parameter
        {
            name = Name,
            description = Description,
            @in = In,
            required = true,
            type = "string"
        });
    }

    public void Apply(Swashbuckle.Application.SwaggerDocsConfig c)
    {
        c.ApiKey(Key)
            .Name(Name)
            .Description(Description)
            .In(In);
        c.DocumentFilter(() => this);
        c.OperationFilter(() => this);
    }
}

Then the swagger file has the security definiton: 然后,swagger文件具有安全性定义:

"securityDefinitions":{  
  "ServiceBusToken":{  
     "type":"apiKey",
     "description":"Service Bus Token, e.g. 'SharedAccessSignature sr=...&sig=...&se=...&skn=...'",
     "name":"Authorization",
     "in":"header"
  }
}

Applied to all operations at document level: 适用于文档级别的所有操作:

"security":[  
  {  
     "ServiceBusToken":[]
  }
]

And all operations have the header parameter assigned: 并且所有操作都分配有标头参数:

"parameters":[  
   {  
      "name":"Authorization",
      "in":"header",
      "description":"Service Bus Token, e.g. 'SharedAccessSignature sr=...&sig=...&se=...&skn=...'",
      "required":true,
      "type":"string"
   }
]

Swashbuckle maintainer recommends us to provide a custom index.html to do this because he will remove these configurations in the next major version. Swashbuckle维护者建议我们提供一个自定义index.html来执行此操作,因为他将在下一个主要版本中删除这些配置。 See this issue . 看到这个问题

Provide your own "index" file 提供您自己的“索引”文件

Use the CustomAsset option to instruct Swashbuckle to return your version instead of the default when a request is made for "index". 使用CustomAsset选项指示Swashbuckle在请求“索引”时返回您的版本而不是默认版本。 As with all custom content, the file must be included in your project as an "Embedded Resource", and then the resource's "Logical Name" is passed to the method as shown below. 与所有自定义内容一样,该文件必须作为“嵌入式资源”包含在您的项目中,然后将资源的“逻辑名”传递给方法,如下所示。 See Injecting Custom Content for step by step instructions. 有关分步说明,请参阅注入自定义内容

For compatibility, you should base your custom "index.html" off this version . 为了兼容,您应该基于此版本自定义“ index.html”。

httpConfiguration
    .EnableSwagger(c => c.SingleApiVersion("v1", "A title for your API"))
    .EnableSwaggerUi(c =>
        {
            c.CustomAsset("index", yourAssembly, "YourWebApiProject.SwaggerExtensions.index.html");
        });

In the index.html you will want to change the method below to something like this: 在index.html中,您将需要将以下方法更改为如下所示:

function addApiKeyAuthorization(){
    var key = encodeURIComponent($('#input_apiKey')[0].value);
    if(key && key.trim() != "") {
        var apiKeyAuth = new SwaggerClient.ApiKeyAuthorization("sessionId", key, "header");
        window.swaggerUi.api.clientAuthorizations.add("sessionId", apiKeyAuth);
        log("added key " + key);
    }
}
        config.EnableSwagger(c =>
        {
            c.SingleApiVersion("v1", "TestApiWithToken");

            c.ApiKey("Token")
            .Description("Filling bearer token here")
            .Name("Authorization")
            .In("header");
        })
        .EnableSwaggerUi(c =>
        {
            c.EnableApiKeySupport("Authorization", "header");
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM