猿问

如何使用 C# .NET CORE 在 NSwag 文档中添加自定义标头?

我需要添加自定义标头,但无法弄清楚。我正在尝试使用新的 services.AddOpenApiDocument() 而不是 services.AddSwaggerDocument()。我想在我的整个 API 上添加这些自定义标头,而不仅仅是单个方法或控制器。我试图添加一个运算处理器,但是当我加载 swagger UI 时,我收到以下错误“😱 无法呈现此组件,请查看控制台。”


这是我的片段ConfigureServices():


    services.AddOpenApiDocument(document =>

    {

        ...

        // this works fine

        document.OperationProcessors.Add(new OperationSecurityScopeProcessor("Bearer"));

        document.DocumentProcessors.Add(new SecurityDefinitionAppender("Bearer", new SwaggerSecurityScheme

            {

                Type = SwaggerSecuritySchemeType.ApiKey,

                Name = "Authorization",

                In = SwaggerSecurityApiKeyLocation.Header

            })

        );


        // this is the header i want to show up for all endpoints that is breaking

        document.OperationProcessors.Add(new SampleHeaderOperationProcessor());

    });

这是我的操作处理器:


public class SampleHeaderOperationProcessor : IOperationProcessor

{

    public Task<bool> ProcessAsync(OperationProcessorContext context)

    {

        context.OperationDescription.Operation.Parameters.Add(

            new SwaggerParameter {

                Name = "Sample",

                Kind = SwaggerParameterKind.Header,

                Type = NJsonSchema.JsonObjectType.String,

                IsRequired = false,

                Description = "This is a test header",

                Default = "{{\"field1\": \"value1\", \"field2\": \"value2\"}}"

            });


        return Task.FromResult(true);

    }

}

我在 Configure() 中唯一与此有关的东西:


    app.UseSwagger();

    app.UseSwaggerUi3();                              

这是我的错误和控制台日志: 我的错误和控制台日志


如果它有帮助,我正在使用ASP .NET CORE 2.2和NSwag.AspNetCore v12.1.0


MM们
浏览 90回答 3
3回答

海绵宝宝撒

这是我在项目中实现的示例。对我来说,它工作正常:接口“IOperationProcessor”的实现:using NSwag;using NSwag.SwaggerGeneration.Processors;using NSwag.SwaggerGeneration.Processors.Contexts;using System.Threading.Tasks;namespace api.mstiDFE._Helpers.Swagger{&nbsp; &nbsp; public class AddRequiredHeaderParameter : IOperationProcessor&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public Task<bool> ProcessAsync(OperationProcessorContext context)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.OperationDescription.Operation.Parameters.Add(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new SwaggerParameter&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = "token",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Kind = SwaggerParameterKind.Header,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Type = NJsonSchema.JsonObjectType.String,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IsRequired = false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Description = "Chave de acesso à API, fornecida pela RevendaCliente",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Default = "Default Value"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Task.FromResult(true);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}startup.cs 中的引用:internal static void ConfigureServices(IServiceCollection services, IConfiguration configuration){&nbsp; &nbsp; // Register the Swagger services&nbsp; &nbsp; services.AddSwaggerDocument(config =>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Adds the "token" parameter in the request header, to authorize access to the APIs&nbsp; &nbsp; &nbsp; &nbsp; config.OperationProcessors.Add(new AddRequiredHeaderParameter());&nbsp; &nbsp; &nbsp; &nbsp; config.PostProcess = document =>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.Info.Version = "v1";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.Info.Title = "Title ";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.Info.Description = "API para geração de Documentos Fiscais Eletrônicos (DF-e) do projeto SPED";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.Info.TermsOfService = "None";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.Info.Contact = new NSwag.SwaggerContact&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = "Name",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Email = "Email ",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Url = "Url "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.Info.License = new NSwag.SwaggerLicense&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = "Use under LICX",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Url = "https://example.com/license"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}

波斯汪

非常感谢该线程上的原始答案。由于 NSwag 更新,我不得不对上述答案进行一些小更新。以下适用于我的版本(NSwag.Core:13.1.2,NJsonSchema:10.0.24):context.OperationDescription.Operation.Parameters.Add(&nbsp; &nbsp; new OpenApiParameter&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Name = "HEADER_NAME",&nbsp; &nbsp; &nbsp; &nbsp; Kind = OpenApiParameterKind.Header,&nbsp; &nbsp; &nbsp; &nbsp; Schema = new JsonSchema { Type = JsonObjectType.String },&nbsp; &nbsp; &nbsp; &nbsp; IsRequired = true,&nbsp; &nbsp; &nbsp; &nbsp; Description = "Description",&nbsp; &nbsp; &nbsp; &nbsp; Default = "Default Value"&nbsp; &nbsp; });

小唯快跑啊

这最终对我有用。直接来自 Rico Suter 的解决方案,尝试Schema&nbsp;=&nbsp;new&nbsp;JsonSchema4&nbsp;{&nbsp;Type&nbsp;=&nbsp;NJsonSchema.JsonObjectType.String&nbsp;}代替Type&nbsp;=&nbsp;NJsonSchema.JsonObjectType.String(我认为 Type 在 OpenAPI 3 中已被弃用)
随时随地看视频慕课网APP
我要回答