flask-restplus icon indicating copy to clipboard operation
flask-restplus copied to clipboard

Hide models from the buttom of the swagger ui

Open nunidoron opened this issue 6 years ago • 7 comments

I would like to hide the models section from the swagger website. in the swagger doc the is an option to disable it by setting the param "DEFAULT_MODELS_EXPAND_DEPTH" to -1. I tried to do it here also but nothing works. can you add it please?

nunidoron avatar Feb 02 '19 14:02 nunidoron

Unfortunately no. still waiting for a solution.

nunidoron avatar Mar 03 '19 07:03 nunidoron

@nunidoron Not the best way to do it but there is a hack using CSS in your API description below:

Api(app=app, title='API', description='API <style>.models {display: none !important}</style>'

tiagobarreto avatar Mar 03 '19 16:03 tiagobarreto

@tiagobarreto you are so creative :) that one really did the job done. thank you 👍

nunidoron avatar Mar 04 '19 07:03 nunidoron

This is currently possible in Swashbuckle.AspNetCore version 5.0.0-rc4

app.UseSwaggerUI(c =>
{
    ...
    c.DefaultModelsExpandDepth(-1); // hide completely
    // c.DefaultModelsExpandDepth(0); // collapse
    ...
});

francoishill avatar Oct 01 '19 03:10 francoishill

@nunidoron Not the best way to do it but there is a hack using CSS in your API description below:

Api(app=app, title='API', description='API <style>.models {display: none !important}</style>'

This is the funniest solution I've ever seen. Well done my friend hahaa

lionelkimbs avatar Apr 20 '20 17:04 lionelkimbs

that can also be hidden by a custom ISchemaFilter Class Somehow its not working with me in .Net 5.0 so I did this way.

`using Microsoft.OpenApi.Any; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.SwaggerGen; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks;

namespace YourNamespace { ///

/// Hide API Schemas from Swagger that aren’t yet ready for public consumption.
///
public class SchemasVisibility : ISchemaFilter { private readonly string[] VisibleSchemas = { "Schema1", "Schema2" };

    public void Apply(OpenApiSchema schema, SchemaFilterContext context)
    {
        foreach (var key in context.SchemaRepository.Schemas.Keys)
        {
            if(!VisibleSchemas.Contains(key))
                context.SchemaRepository.Schemas.Remove(key);
        }
    }

}

}`

Then in Startup.cs class

services.AddSwaggerGen(c => { c.SchemaFilter<SchemasVisibility>(); }

musmanjalil avatar Oct 05 '21 03:10 musmanjalil

In your serializer just define in meta class ref_name = None

sahilpysquad avatar Dec 20 '22 06:12 sahilpysquad