Swashbuckle.aspnetcore: Consumes attribute ignored

Created on 15 Dec 2015  路  8Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

Seems that Consumes attribute is ignored and that the generated json is without it. I've see a commented line (148 of DefaultSwaggerProvider.cs) that should handle consumes...

Most helpful comment

For anyone else with this problem, I'm using the following IOperationFilter as a workaround:

public class ComsumesOperationFilter : IOperationFilter
{
    public void Apply(Operation operation, OperationFilterContext context)
    {
        var consumes = context.ApiDescription.ActionDescriptor.ActionConstraints.OfType<ConsumesAttribute>().FirstOrDefault();
        if (consumes != null)
        {
            operation.Consumes.Clear();
            foreach (var contentType in consumes.ContentTypes)
            {
                operation.Consumes.Add(contentType);
            }
        }
    }
}

All 8 comments

@rynowak - any idea where to find this metadata on an ApiDescription? "Produces" shows up in SupportedResponseFormats but there doesn't appear to be an equivalent for "Consumes".

Looks like we need to add it. It will be in RC2. https://github.com/aspnet/Mvc/issues/3818

Any thoughts on API design? Anything missing?

I don't know if it helps but I've seen that the "non vnext" code use SupportedRequestBodyFormatters of ApiDescription (https://github.com/domaindrivendev/Swashbuckle/blob/1e4c00e3cf36ecad0d7c7100be1238797da2af04/Swashbuckle.Core/Swagger/ApiDescriptionExtensions.cs) but in the new api explorer (https://github.com/aspnet/Mvc/blob/eef6c3883a7e27b8387b0925f0b6a88df0a484c5/src/Microsoft.AspNet.Mvc.ApiExplorer/ApiDescription.cs) it seems that it has not been implemented yet.

This has been added for RC2 aspnet/Mvc@420f44248755f16dd529d8d2c027066b02aedc3b

Should be available in beta902

@domaindrivendev This has not made it into beta902. Adding the Consumes attribute has no effect.

For anyone else with this problem, I'm using the following IOperationFilter as a workaround:

public class ComsumesOperationFilter : IOperationFilter
{
    public void Apply(Operation operation, OperationFilterContext context)
    {
        var consumes = context.ApiDescription.ActionDescriptor.ActionConstraints.OfType<ConsumesAttribute>().FirstOrDefault();
        if (consumes != null)
        {
            operation.Consumes.Clear();
            foreach (var contentType in consumes.ContentTypes)
            {
                operation.Consumes.Add(contentType);
            }
        }
    }
}

I'm having the same problem. Adding [Consumes("multipart/form-data"] doesn't do anything in the generated swagger.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

voroninp picture voroninp  路  3Comments

mrmartan picture mrmartan  路  3Comments

vanillajonathan picture vanillajonathan  路  3Comments

brucewilkins picture brucewilkins  路  3Comments

NinoFloris picture NinoFloris  路  3Comments