Swashbuckle.AspNetCore Swagger [5.0.0 | OpenAPI 3.0] filter to set Consumes/Produces

2.2k Views Asked by At

While migrating to .NET Core 3.0 and Swagger 5.0.0-rc4 I need to re-implement following document filter:

internal sealed class CommonDocumentFilter : IDocumentFilter
{
    public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
    {
        swaggerDoc.Consumes = new List<string> { "application/json" };
        swaggerDoc.Produces = new List<string> { "application/json" };
    }
}

SwaggerDocument now migrated to OpenApiDocument and no longer provides the ability to set document's Consumes/Provides directly. I'm aware that I can decorate controllers with Microsoft.AspNetCore.Mvc.ConsumesAttribute/ProducesAttribute, however, it will require a lot of refactoring and forces developers to use these attributes for every new controller.

There is an open issue in Github: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1296

UPD:

Meanwhile, are there any alternatives to solve this?

It is possible to set global filter instead of updating controllers annotations:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc(options =>
    {
        options.Filters.Add(new ConsumesAttribute("application/json"));
        options.Filters.Add(new ProducesAttribute("application/json"));
    });
}
0

There are 0 best solutions below