Swashbuckle.aspnetcore: Exclude model from Models list

Created on 8 Aug 2018  路  3Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

Content & configuration

Swagger/OpenAPI definition:
I use Swashbuckle.AspNetCore v3.0.0 / Swagger-Ui v3.17.1

Is your feature request related to a problem?

I need to exlude (i.e. do not show) model in Models list. As our partners do not use AspNet.Core, they don't need to know anything about IFormFile interface which is specific to AspNet.Core.

Describe the solution you'd like

It will be very useful for us, to decorate the method parameter with some attribute to exclude IFormFile from rendering.

public async Task Post([IgnoreModel]IFormFile document)

Additional context

image

Most helpful comment

For the specific example you gave, there's a broader issue for handling IFormFile parameters better on the backlog - #193

For the general case, you should just create a Document Filter (see readme) that removes the unwanted models from the Definitions dictionary:

// Startup.cs
services.AddSwaggerGen(c =>
{
    ...

    c.DocumentFilter<RemoveBogusDefinitionsDocumentFilter>();
}

// RemoveBogusDefinitionsDocumentFilter.cs
public class RemoveBogusDefinitionsDocumentFilter : IDocumentFilter
{
    public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
    {
        swaggerDoc.Definitions.Remove("IFormFile");
    }
}

All 3 comments

For the specific example you gave, there's a broader issue for handling IFormFile parameters better on the backlog - #193

For the general case, you should just create a Document Filter (see readme) that removes the unwanted models from the Definitions dictionary:

// Startup.cs
services.AddSwaggerGen(c =>
{
    ...

    c.DocumentFilter<RemoveBogusDefinitionsDocumentFilter>();
}

// RemoveBogusDefinitionsDocumentFilter.cs
public class RemoveBogusDefinitionsDocumentFilter : IDocumentFilter
{
    public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
    {
        swaggerDoc.Definitions.Remove("IFormFile");
    }
}

Thanks, it helped. Just curious why Swashbuckle do not have an option for this?

For others looking for a solution that is more dynamic or generic, like the OP was originally hoping for, I've written a document filter which can access a custom attribute you can write using reflection and then remove the definitions which are a match (based on type name).

https://github.com/domaindrivendev/Swashbuckle/issues/178#issuecomment-546161898

Was this page helpful?
0 / 5 - 0 ratings

Related issues

flipchart picture flipchart  路  4Comments

jderus picture jderus  路  4Comments

mrmartan picture mrmartan  路  3Comments

alasvant picture alasvant  路  3Comments

vanillajonathan picture vanillajonathan  路  3Comments