NSwag icon indicating copy to clipboard operation
NSwag copied to clipboard

Add ProducesResponseType globally

Open ahmet8282 opened this issue 6 years ago • 8 comments

Hello,

Is it possible to add ProducesResponseType for all controllers and actions globally? For some error types, responses are global. I have tried adding it as a filter but it didn't work.

Thank you

ahmet8282 avatar May 30 '18 11:05 ahmet8282

You can do this with an operation or document processor: https://github.com/RSuter/NSwag/wiki/Document-Processors-and-Operation-Processors

RicoSuter avatar May 30 '18 14:05 RicoSuter

Which one would be easier? Thank you

ahmet8282 avatar May 30 '18 14:05 ahmet8282

A globally registered operation processor (via settings object)… or you can inherit from a base controller class and add the SwaggerResponseAttribute + SwaggerDefaultResponseAttribute to the base class.

https://github.com/RSuter/NSwag/tree/master/src/NSwag.Annotations

RicoSuter avatar May 30 '18 14:05 RicoSuter

I only want to add global response types to some methods. I think operation processor will make it simpler to filter. So far I have this. But what I need to write to add ProducesResponseType?

public class MyOperationProcessor : IOperationProcessor
    {
        public async Task<bool> ProcessAsync(OperationProcessorContext context)
        {
            if(context.OperationDescription.Method == SwaggerOperationMethod.Post || context.OperationDescription.Method == SwaggerOperationMethod.Put)
            {
                //What to do here?
            }
            return await Task.FromResult<bool>(true);
        }
    }

ahmet8282 avatar Jun 06 '18 16:06 ahmet8282

I'm not sure if this is a dead conversation, but I found a clean way to solve the problem. Just add a filter to the AddMvc func, in the ConfigureServices method in Startup.cs

Eg:

services.AddMvc(options => { options.Filters.Add(new ProducesResponseTypeAttribute(500)); })

jacksondaw avatar Sep 07 '18 21:09 jacksondaw

You can write your own ApiConventions and define there ProducesResponseTypeAttribute(500))

Take as sample the DefaultApiConventions that comes by default in the framework https://raw.githubusercontent.com/aspnet/Mvc/release/2.2/src/Microsoft.AspNetCore.Mvc.Core/DefaultApiConventions.cs

Then you can register globally in your startup.cs

[assembly: ApiConventionType(typeof(YourCustomApiConventions))]

carlesdavila avatar Mar 01 '19 07:03 carlesdavila

You can write your own ApiConventions and define there ProducesResponseTypeAttribute(500))

Take as sample the DefaultApiConventions that comes by default in the framework https://raw.githubusercontent.com/aspnet/Mvc/release/2.2/src/Microsoft.AspNetCore.Mvc.Core/DefaultApiConventions.cs

Then you can register globally in your startup.cs

[assembly: ApiConventionType(typeof(YourCustomApiConventions))]

Is it just me that can't get any conventions at all to work with NSwag. Only ProducesResponseType is working in out projekt.

Drol avatar May 17 '22 12:05 Drol

@jacksondaw wrote:

Just add a filter to the AddMvc func, in the ConfigureServices method in Startup.cs services.AddMvc(options => { options.Filters.Add(new ProducesResponseTypeAttribute(500)); })

Placing this filter destroys any HTTP 200[OK] response type NSwag generates. Furthermore NSwag dumps any type information the 200er came along. The final result: All types are gone!!!

aureole82 avatar Sep 14 '22 11:09 aureole82

You can write your own ApiConventions and define there ProducesResponseTypeAttribute(500)) Take as sample the DefaultApiConventions that comes by default in the framework https://raw.githubusercontent.com/aspnet/Mvc/release/2.2/src/Microsoft.AspNetCore.Mvc.Core/DefaultApiConventions.cs Then you can register globally in your startup.cs [assembly: ApiConventionType(typeof(YourCustomApiConventions))]

Is it just me that can't get any conventions at all to work with NSwag. Only ProducesResponseType is working in out projekt.

It seems like this issue is outdated, but for me it does not work as well. Did you manage to fix it somehow?

sergey-bulavskiy avatar Mar 24 '23 09:03 sergey-bulavskiy