Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Adding ProducesResponseTypeAttribute(int statusCode) constructor #4863

Closed
domaindrivendev opened this issue Jun 14, 2016 · 10 comments
Closed

Comments

@domaindrivendev
Copy link

Is there a convenient way to indicate additional HTTP status codes for an API action, without specifying a response type? For example, with a 404 response, it's extremely common to return no content in the response body. It looks like the following syntax is required for this:

[ProducesResponseType(typeof(Cart), 200)]
[ProducesResponseType(typeof(void), 404)]
[HttpGet("/carts/{id}")]
public IActionResult GetById(int id)
{
    ....
}

This is probably fine but gives me a "niggly" feeling that the semantics for ProducesResponseType don't quite align with typical API design. In HTTP, there's ALWAYS a status code and OPTIONALLY a response body. IMO, this reality would be better reflected by renaming the attribute to "ProducesResponse" (or even just "Produces") with an optional Type parameter:

[ProducesResponse(200, typeof(Cart))]
[ProducesResponse(404)]
[HttpGet("/carts/{id}")]
public Cart GetById(int id)
{
    ....
}

Perhaps I'm missing something so would be great to hear other's opinions. It's not the end of the world either way but just wanted to put forward my two cents.

@javiercn
Copy link
Member

@kichalla

@bklooste
Copy link

bklooste commented Jul 22, 2016

This doesnt work at all imho .., swagger only has a single response generated so i dont know how you can go 404 with null or an object,with 200. Especially with proxies with hard checks .

 "responses": {
          "200": {
            "description": "OK",
            "schema": {
              "$ref": "#/definitions/Config"
            }
          }

@rynowak
Copy link
Member

rynowak commented Jul 22, 2016

@bklooste - Can you provide an example of the action + attributes that you think isn't working right? We can figure out if this is a problem on our side or not.

@Eilon
Copy link
Member

Eilon commented Jul 25, 2016

@rynowak is the work item here (as far as we know) to add a new ProducesResponseAttribute that has a ctor like ProducesResponse(int statusCode), but is otherwise identical to https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.Core/ProducesResponseTypeAttribute.cs?

@domaindrivendev would this satisfy your suggestion?

@clydin
Copy link

clydin commented Jul 26, 2016

I ran into the same issue.
With the status code first, I've also found the code is easier to read due to the status code having a consistent length of 3 characters; whereas the type varies.

I've been adding the following into each of my new projects:

public class ProducesResponseAttribute : ProducesResponseTypeAttribute
{
        public ProducesResponseAttribute(int statusCode, Type type = null)  
            : base(type ?? typeof(void), statusCode)  
        { }  
}  

@Eilon
Copy link
Member

Eilon commented Oct 4, 2016

Let's add the ProducesResponse(int statusCode) ctor.

@jbagga jbagga changed the title How to indicate API respones, that don't return a body, with ProducesResponseTypes Adding ProducesResponse(int StatusCode) constructor Oct 14, 2016
@jbagga jbagga changed the title Adding ProducesResponse(int StatusCode) constructor Adding ProducesResponse(int statusCode) constructor Oct 14, 2016
@jbagga jbagga changed the title Adding ProducesResponse(int statusCode) constructor Adding ProducesResponseTypeAttribute(int statusCode) constructor Oct 18, 2016
@jbagga jbagga closed this as completed Oct 20, 2016
@goenning
Copy link

When will this land on NuGet? Just updated to 1.1.0-preview1-final and it's not there yet.

@jbagga
Copy link
Contributor

jbagga commented Nov 13, 2016

It didn't make it to the preview but it will be in the 1.1.0 release!

@ormico
Copy link

ormico commented Oct 25, 2018

Sorry to dig up this old issue. I'm really late to the game, but I just went through this same thought process and discovered this thread.
I don't see ProducesResponse in ASP.NET Core 2.1 even though the last comment said it would be in 1.1. ProducesResponseType is still there. What happened to this? Is there a better way to specify the expected Response Codes like 404 w/o specifying a type. Did people just not think it was useful?

@pranavkm
Copy link
Contributor

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

10 participants