Reputation: 43
I am generating Swagger document for my .Net core 6 based API. I have some error models which will be returned by my Gateway. These are not used/returned in any of my API endpoints. I want to add these models to Swagger document schema collection. I am able to successfully add them if I follow inline way. Is there a way to include existing models in Swagger schema collection?
I also want to include webhook models and examples to schemas collection without going through inline option.
Thanks for your time.
Upvotes: 4
Views: 1914
Reputation: 2600
You can do following to add the model to schema collection.
Create a custom document filter
public class CustomDocumentFilter : IDocumentFilter
{
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
{
context.SchemaGenerator.GenerateSchema(typeof(GWErrorModel), context.SchemaRepository);
}
}
add it to program.cs
builder.Services.AddSwaggerGen(o =>
{
o.DocumentFilter<CustomDocumentFilter>();
});
you will see the model is added to schema collection.
Hope it helps.
Upvotes: 6