NSwag
NSwag copied to clipboard
Add ProducesResponseType globally
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
You can do this with an operation or document processor: https://github.com/RSuter/NSwag/wiki/Document-Processors-and-Operation-Processors
Which one would be easier? Thank you
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
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);
}
}
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)); })
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))]
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.
@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!!!
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?