I am in need of adding custom headers, but cannot figure it out. I want to add these custom headers on my whole API not just a single method or controller. I attempted to add an operation processor, but when I load up the swagger UI I receive the following error "馃槺 Could not render this component, see the console."
Here is my snippet within my 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());
});
Here is my operation processor:
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);
}
}
The only thing I have pertaining to this within my Configure:
app.UseSwagger();
app.UseSwaggerUi3();
Here is my error and the console log:
My error and console log
If it helps I'm using ASP .NET CORE 2.2 and NSwag.AspNetCore v12.1.0
Any help? :(
This code works fine if I use the old services.AddSwaggerDocument(), but fails when I switch to using services.AddOpenApiDocument().
So it works without the SampleHeaderOperationProcessor?
Try
Schema = new JsonSchema4 { Type = NJsonSchema.JsonObjectType.String }
instead of
Type = NJsonSchema.JsonObjectType.String
(I think Type is deprecated in OpenAPI 3)
The last comment worked! Thank you @RicoSuter.
resolved
The ui should really display a better error... we should create an issue in the swagger ui repo
Is there a way to convert this issue into that or should I create a new issue?
We need to create a new one and add a link to this one, maybe also add a sample spec
Most helpful comment
Try
instead of
(I think Type is deprecated in OpenAPI 3)