Remove Schema on Swagger UI -continued

Viewed 1419

I have followed the top article : .Net Core 3.1 Remove Schema on Swagger UI

I applied this filter:

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);
        }
    }
}

I have added it here:

services.AddSwaggerGen(options =>
            {
                options.OperationFilter<AddRequiredHeaderParameter>(Configuration.GetSection("DefaultConfig")["TenantId"]);
                options.DocumentFilter<RemoveSchemasFilter>();
             }

All good Schema is removed from the bottom of Swagger UI. However, when I click on a method it brings a dialogue of errors. It works but this windows stays on top and it is very annoying.

enter image description here

Let's solve this problem together as it was unsolved previously!

1 Answers

The schema filter is useless.

All needs to be done is in

app.UseSwaggerUI(options =>
{
    options.DefaultModelsExpandDepth(-1);
}

Note: It is Default Models not Default Model. DIfference is DefaultModel is the default expansion depth for the model on the model-example section, whereas DefaultModels is the expansion depth for models.

Related