-
I have a middleware that catches and packages exceptions that occur on our API in a common This is the code I tried: sealed class GenerateSchemaFor<T> : IDocumentFilter where T : class
{
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
{
if (!context.SchemaRepository.Schemas.ContainsKey(typeof(T).Name))
{
context.SchemaGenerator.GenerateSchema(typeof(T), context.SchemaRepository);
}
if (!context.SchemaRepository.Schemas.ContainsKey(typeof(T).Name))
{
throw new SwaggerGeneratorException($"Could not register schema for {typeof(T).Name}!");
}
}
} I call it in the swagger configuration: .AddSwaggerGen(options => options.AddModel<ApiException>())` However this always throws: It's quite odd. I've read through the code of |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
We'd need to see more information (an inner exception and a stack trace) to determine whether However you should be able to use filters to add an additional error response to all of your operations to handle the behaviour your middleware adds. The code for the filter you've provided looks wrong as you create a schema, but then you don't actually add it to the document. |
Beta Was this translation helpful? Give feedback.
It must've been something related to the specific type I was trying to generate. I called the code above on another type, and it worked out of the box. I have no idea why.