Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide models from the buttom of the swagger ui #583

Open
nunidoron opened this issue Feb 2, 2019 · 7 comments
Open

Hide models from the buttom of the swagger ui #583

nunidoron opened this issue Feb 2, 2019 · 7 comments

Comments

@nunidoron
Copy link

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
Copy link
Author

nunidoron commented Mar 3, 2019 via email

@tiagobarreto
Copy link

tiagobarreto commented Mar 3, 2019

@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>'

@nunidoron
Copy link
Author

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

@francoishill
Copy link

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

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

@lionelkimbs
Copy link

lionelkimbs commented Apr 20, 2020

@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

@musmanjalil
Copy link

musmanjalil commented Oct 5, 2021

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

@sahilpysquad
Copy link

In your serializer just define in meta class
ref_name = None

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants