Nswag: "Consumes" attribute on controller action ignored

Created on 16 Nov 2016  Â·  24Comments  Â·  Source: RicoSuter/NSwag

"Consumes" attribute on controller action does not generate the swagger.json with a "path" with the indicated "consumes" property

NSwag.SwaggerGeneration.WebApi enhancement

Most helpful comment

I'm not seeing [Consumes("text/plain")] respected. Using NSwag.AspNetCore 12.3.1

image

image

All 24 comments

Can you provide a sample action method and the expected swagger json?

I expect the consume attribute to generate the right section in open api. Usually the consumes per action means an overwrite of the configured per website consumes.
I have a situation in which the web api usually consumes Json, but some actions consume form/encoded.

I noticed this is still an open issue. Is there any progress on this? Or can I do this any another way?

Can you provide a sample operation to reproduce this?

I've created a PR: Can you check the test and the rest? Is this what you'd expect?

https://github.com/RSuter/NSwag/pull/1849

Yes, this works. The Try it out functionallity is working now with the proper Content-Type. Sorry I could not reply to you sooner, but thanks very much for taking your time. Very much appreciated.

@RSuter any idea when the PR will be merged? Is it correct that we can use the Consumes attribute on Action methods for ASP .NET Core?

@LockTar will check ASAP. There must have been some problems otherwise i would have merged it :-)

Hi @RSuter, did you found an issue?

See @pranavkm answer in the PR:

ConsumesAttribute works in tandem with formatters - the idea is that you can only consume a media type that you could read from the body. It's likely that there isn't a formatter for text/html in your test above. Here's the relevant piece of code: https://github.com/aspnet/AspNetCore/blob/master/src/Mvc/src/Microsoft.AspNetCore.Mvc.ApiExplorer/DefaultApiDescriptionProvider.cs#L426-L461

Hi @RSuter, sorry I don't understand your reply. I checked the PR but I think I do the same as in the PR.
I have a Controller with the attributes:

```c#
[Produces("application/json")]
[ApiController]

I than have an action with the following attributes:

```c#
[HttpPost("add")]
[Consumes("application/json")]
[ProducesResponseType(201)]
[ProducesResponseType(400)]

In the swagger.json, in the "add" path i have:

"consumes": [
"application/json-patch+json",
"application/json",
"application/*+json"
]

The api works as expected with application/json as contenttype.

So is this what you expect? Or do you want a different outcome?

I was only expecting "application/json" because that is in the Consumes attribute.

I think the other types are registered globally in asp.net core startup or introduced by the used api convention

I think "application/json-patch+json" and "application/*+json" are part of the default then. I can't find anything in the documentation about this.

It doesn't matter if they are automatically added globally, because the way asp core works is that if you have a Consumes attribute, it will only accept that type. So it makes sense to support this feature. This is easily tested btw.

It doesn't matter if they are automatically added globally, because the way asp core works is that if you have a Consumes attribute, it will only accept that type. So it makes sense to support this feature. This is easily tested btw.

@TheSilvermind So… You would expect also that if you have a Consume attribute [Consumes("application/json")] on your action that you would only see the following?

"consumes": [
"application/json"
]

Yes, because if I apply the attribute and choose another mime type in the swagger 'try it out' functionallity, an exception is thrown that the provided mime type is not supported.

These mime types are reported by ASP.NET Core - so I think they are part of the default API Convention or automatically added...

@RSuter @LockTar Sorry, you're right. I wasn't paying proper attention. It seems it was the text mime-type that caused problems, but that one seems no longer generated as previously reported.

I'm not seeing [Consumes("text/plain")] respected. Using NSwag.AspNetCore 12.3.1

image

image

It appears that for OpenAPI spec generation, the MIME type for content is hardcoded to application/json (or application/octet-stream for binary files).

https://github.com/RicoSuter/NSwag/blob/4e94b9d2bd558eb7bc2fa5f944b6a3ff08d62c68/src/NSwag.Core/OpenApiResponse.cs#L73-L86

My API will produce JSON:API formatted documents so I need to be able to set the content type to application/vnd.api+json.

Currently, the only way I have found to achieve this is via a PostProcess action where I traverse down through the Operations and Responses of the generated spec and remove/replace application/json keyed Content items with application/vnd.api+json.

services.AddOpenApiDocument(config =>
{
    config.PostProcess = document =>
    {
        const string fromKey = "application/json";
        const string toKey = "application/vnd.api+json";

        foreach (var o in document.Operations)
        {
            foreach (var r in o.Operation.Responses)
            {
                if (r.Value.Content.TryGetValue(fromKey, out var value))
                {
                    r.Value.Content.Remove(fromKey);
                    r.Value.Content.Add(toKey, value);
                }
            }
        }
    }
});

I have the same problem ([Consumes ("text / plain")] ignored). I even opened this question on the stackoverflow in an attempt to get some help.

Was this page helpful?
0 / 5 - 0 ratings