我需要添加自定义标题,但无法弄清楚。我正在尝试利用新的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();
这是我的错误和控制台日志: My error and console log
如果有帮助,我正在使用ASP .NET CORE 2.2
和NSwag.AspNetCore v12.1.0
答案 0 :(得分:1)
非常感谢此主题的原始答案。
由于NSwag更新,我不得不对上述答案进行一些小的更新。
以下内容适用于我的版本(NSwag.Core:13.1.2,NJsonSchema:10.0.24):
context.OperationDescription.Operation.Parameters.Add(
new OpenApiParameter
{
Name = "HEADER_NAME",
Kind = OpenApiParameterKind.Header,
Schema = new JsonSchema { Type = JsonObjectType.String },
IsRequired = true,
Description = "Description",
Default = "Default Value"
});
答案 1 :(得分:0)
这是我在项目中实现的示例。对于我来说,它正常工作:
“ IOperationProcessor”接口的实现:
using NSwag;
using NSwag.SwaggerGeneration.Processors;
using NSwag.SwaggerGeneration.Processors.Contexts;
using System.Threading.Tasks;
namespace api.mstiDFE._Helpers.Swagger
{
public class AddRequiredHeaderParameter : IOperationProcessor
{
public Task<bool> ProcessAsync(OperationProcessorContext context)
{
context.OperationDescription.Operation.Parameters.Add(
new SwaggerParameter
{
Name = "token",
Kind = SwaggerParameterKind.Header,
Type = NJsonSchema.JsonObjectType.String,
IsRequired = false,
Description = "Chave de acesso à API, fornecida pela RevendaCliente",
Default = "Default Value"
});
return Task.FromResult(true);
}
}
}
startup.cs中的引用:
internal static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
// Register the Swagger services
services.AddSwaggerDocument(config =>
{
// Adds the "token" parameter in the request header, to authorize access to the APIs
config.OperationProcessors.Add(new AddRequiredHeaderParameter());
config.PostProcess = document =>
{
document.Info.Version = "v1";
document.Info.Title = "Title ";
document.Info.Description = "API para geração de Documentos Fiscais Eletrônicos (DF-e) do projeto SPED";
document.Info.TermsOfService = "None";
document.Info.Contact = new NSwag.SwaggerContact
{
Name = "Name",
Email = "Email ",
Url = "Url "
};
document.Info.License = new NSwag.SwaggerLicense
{
Name = "Use under LICX",
Url = "https://example.com/license"
};
};
});
}
答案 2 :(得分:0)
这终于对我有用。直接从Rico Suter获得解决方案,
尝试
Schema = new JsonSchema4 { Type = NJsonSchema.JsonObjectType.String }
代替
Type = NJsonSchema.JsonObjectType.String
(我认为TypeType在OpenAPI 3中已弃用)