I have .Net 3.1 Web Api, I would like to remove this sections "Schemas" on Swagger UI.
How to do it?
No need for a schema filter. After busting on it for days I have found out:
All needs to be done is in
app.UseSwaggerUI(options =>
{
options.DefaultModelsExpandDepth(-1);
});
Note: It is DefaultModels (plural) not DefaultModel (singular). Difference is DefaultModels is the default expansion depth for the model on the model-example section, whereas DefaultModels is the expansion depth for models.
On Adding Swagger UI just add the following line:
app.UseSwaggerUI(c => {
c.DefaultModelsExpandDepth(-1);
});
After a lot of breaking my head, using the user's suggestion "CoffeeCodeConverterImpl" I made the class like this:
public class RemoveSchemasFilter : IDocumentFilter
{
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
{
IDictionary<string, OpenApiSchema> _remove = swaggerDoc.Components.Schemas;
foreach (KeyValuePair<string, OpenApiSchema> _item in _remove)
{
swaggerDoc.Components.Schemas.Remove(_item.Key);
}
}
}
Implementation:
c.DocumentFilter<RemoveSchemasFilter>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With