4

I'm trying to build a filter for Swashbuckle to omit in the API documentation the models / Entities / Schema of the project, keeping the controllers. The technology employed is Swashbuckle.AspNetCore v3.0.0 / Swagger UI v3.17.1. I already found ways to omit a certain method in the controller, but I wanted to omit the models in the documentation. I found a problem similar to mine, including hiding only the properties.

Follow filter code

public void Apply(OpenApiSchema schema, SchemaFilterContext context)
{
    if (!(context.ApiModel is ApiObject))
    {
        return;
    }

    var model = context as ApiObject;

    if (schema?.Properties == null || model?.ApiProperties == null)
    {
        return;
    }

    var excludedProperties = model.Type
        .GetProperties()
        .Where(
            t => t.GetCustomAttribute<SwaggerExcludeAttribute>() != null
        );

    var excludedSchemaProperties = model.ApiProperties
        .Where(
            ap => excludedProperties.Any(
                pi => pi.Name == ap.MemberInfo.Name
            )
        );

    foreach (var propertyToExclude in excludedSchemaProperties)
    {
         schema.Properties.Remove(propertyToExclude.ApiName);
    }
}

quote: How to configure Swashbuckle to ignore property on model

Would anyone have any suggestions to hide only the models / Entities / Schemas from the documentation and not just their attributes? As the image below.

Helen
  • 87,344
  • 17
  • 243
  • 314
  • Related: [How to hide the Models section in Swagger UI?](https://stackoverflow.com/q/57138564/113116) – Helen Feb 11 '20 at 08:12

2 Answers2

4

Set DefaultModelsExpandDepth to -1 in your Swashbuckle / Swagger UI configuration:

app.UseSwaggerUI(c =>
{
    ...
    c.DefaultModelsExpandDepth(-1);
}
Helen
  • 87,344
  • 17
  • 243
  • 314
  • 1
    Thanks for the sugestion! But when using the line of code, it did not achieve the desired goal of omitting the schema / entity / model from the documentation. – Nathalino Pachêco Feb 11 '20 at 13:47
  • 1
    Please make sure you used `DefaultModel*s*...` (with an "s") and not `DefaultModel...` – Helen Feb 11 '20 at 13:55
  • What's the difference between `DefaultModelExpandDepth`, `DefaultModelRendering`, `DefaultModelsExpandDepth`? They looks very similar in name, do they have similar functionality? – liang Feb 17 '21 at 03:05
  • @liang please see Swagger UI docs for the description of each config option https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md#display – Helen Feb 17 '21 at 06:38
1

At least for me I had to do something like:

internal class SwaggerSchemaFilter : ISchemaFilter
{
    public void Apply(OpenApiSchema schema, SchemaFilterContext context)
    {
        var keys = new System.Collections.Generic.List<string>();
        var prefix = "My.Prefix";
        foreach(var key in context.SchemaRepository.Schemas.Keys)
        {
            if (key.StartsWith(prefix))
            {
                keys.Add(key);
            }
        }

        foreach(var key in keys)
        {
            context.SchemaRepository.Schemas.Remove(key);
        }
    }
}
Ciarán Bruen
  • 5,221
  • 13
  • 59
  • 69
orellabac
  • 2,077
  • 2
  • 26
  • 34