RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.61k stars 1.22k forks source link

WebApiToSwaggerGenerator not respecting mimetypes per operation #2147

Open MattFenner opened 5 years ago

MattFenner commented 5 years ago

Hi,

We are using the WebApiToSwaggerGenerator.GenerateForControllersAsync(controllerTypes) method to generate our swagger file, but it doesn't seem to respect any ProducesAttribute put on any of the rest endpoint methods.

For example we have a method defined:

[HttpGet("ds/csv")]
        [SwaggerResponse(HttpStatusCode.OK, typeof(CsvSwaggerFeatureResponse<DSRealtimeCsvEntry>))]
        [SwaggerResponse(HttpStatusCode.Forbidden, typeof(string))]
        [SwaggerTag("Traffic Data Service")]
        [Description("Get Detector Site Real-time Measures")]
        [LongDescription("Returns the realtime traffic measures for detector sites.")]
        [Produces("text/csv")]
        public async Task<IActionResult> GetDsRealtime()`

I would expect the generated swagger should be something like:

"paths": {
    "/traffic/v1/ds/csv": {
      "get": {
        "tags": [
          "Traffic Data Service"
        ],
        "summary": "Get Detector Site Real-time Measures",
        "produces": [
            "text/csv"
        ],
        "operationId": "Traffic_GetDsRealtime",`

but it is missing the produces section on the operation.

From what I can tell this feature is implemented in some of the other Generator classes. Would it be possible to also implement it on WebApiToSwaggerGenerator. Or are we forced to 'upgrade' to one of the other generators?

Thanks

darman96 commented 5 years ago

noticed the same behavior, the only mime types which worked for me were "application/json", "text/json" and "text/plain".

Edit: Oh and what other generator classes do you mean? I'm using NSwag in a ASP.net Core project (AspNetCoreToSwaggerGenerator?) and I have the same problem.

RicoSuter commented 5 years ago

WebApiToSwaggerGenerator and AspNetCoreToSwaggerGenerator are different generators - i suspect that WebApiToSwaggerGenerator might not correctly support this - because it's deprecated I don't have the time to look into this...

Do we also have the problem with the AspNetCoreToSwaggerGenerator?

darman96 commented 5 years ago

Well how do I specify the generator? I thought since I'm on a AspNetCore Project nswag defaults to the AspNetCoreToSwaggerGenerator.

RicoSuter commented 5 years ago

@darman96 how did you integrate NSwag in your Startup.cs?

darman96 commented 5 years ago

in ConfigureContainer() i have this: services.AddSwaggerDocument( settings => { settings.DocumentName = "swagger"; });

and in my Configure(): `app.UseSwagger( settings => { settings.DocumentName = "swagger"; settings.Path = "/swagger/v1/swagger.json"; });

app.UseSwaggerUi3( settings => { settings.Path = "/swagger"; settings.DocumentPath = "/swagger/v1/swagger.json"; });`

RicoSuter commented 5 years ago

Yes, this is using the AspNetCoreToSwaggerGenerator... otherwise you'd see obsolete warnings... please create another issue if its AspNetCoreToSwaggerGenerator related

darman96 commented 5 years ago

Are you sure? Because when I hover over app.UseSwaggerUi3() I get this tool tip: 06-05-2019 17-26-28

RicoSuter commented 5 years ago

That's still a legacy leftover and will be removed as soon as the WebApiToSwaggerGenerator is completely removed from NSwag.AspNetCore (currently there for legacy users) in v13 - this is not a problem. It's important that you don't use the overloads where you provide an assembly...

MattFenner commented 5 years ago

Thanks for your quick response Rico, we will look into upgrading to a different generator.