Hi,
I recently upgraded to Asp.Net Core 2.1. It allows adding type to ActionResult which nicely displays in as swagger response sample for 200. But if I add [ProducesResponseType(201)], then the response sample is not generated.
[HttpPost]
[ProducesResponseType(201)]
public async Task<ActionResult<PolicyDto>> Create([FromBody] PolicyViewModel viewModel)
{ ...
Try including the return type with the attribute:
[HttpPost]
[ProducesResponseType(typeof(PolicyDto), 201)]
public async Task<ActionResult<PolicyDto>> Create([FromBody] PolicyViewModel viewModel)
{ ...
This worked, but shouldn't it work without typeof(PolicyDto)
Not according to the semantics of the ProducesResponseTypeAttribute.
A filter that specifies the type of the value and status code returned by the action
This was introduced in ASP.NET Core to be explicit about the response type that's surfaced up through ASP.NET Core's built-in metadata layer - ApiExplorer. It completely overrides the status _and_ response type that would be otherwise inferred from the signature. So, if you don't specify a type, ApiExplorer assumes you're saying there's no content returned.
I'd like to reopen the issue as ASP.Net Core 2.1 is released.
We can see this in the doc (https://docs.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-2.1#actionresultt-type)
ActionResult
offers the following benefits over the IActionResult type:
The [ProducesResponseType] attribute's Type property can be excluded.
And they also talk about it in this section https://docs.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-2.1&tabs=visual-studio%2Cvisual-studio-xml#describe-response-types where they explicitly don't put the type.
I support this request. To fully support ASP.NET Core 2.1 Swashbuckle should react to the new behavior of [ProducesResponseType]. As stated by @NatMarchand people will start dropping the Type property for the default response type.
Off the top of my head this points to a bug in ApiExplorer. Swashbuckle builds on top of this, inspecting ApiDescription.SupportedResponseTypes to build the corresponding Swagger Responses. If 2.1 introduced a change to the semantics of ProducesResponseType, then that should be reflected by ApiExplorer.
I鈥檒l need to dig deeper but I suspect this is the root cause
I confirmed this to be the case and submitted the following issue in the Mvc repo:
https://github.com/aspnet/Mvc/issues/7871
Closing as the upstream issue is now resolved as of ASP.NET Core 2.2
Most helpful comment
I confirmed this to be the case and submitted the following issue in the Mvc repo:
https://github.com/aspnet/Mvc/issues/7871