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

StatusCode Metadata #4101

Closed
chadboyer opened this issue Feb 15, 2016 · 14 comments
Closed

StatusCode Metadata #4101

chadboyer opened this issue Feb 15, 2016 · 14 comments
Assignees
Milestone

Comments

@chadboyer
Copy link

Will there be a way to add metadata to my API actions depicting the different status codes it returns?

For example:

[ResposeType( HttpStatusCode.OK, typeof(Customer) ]
[ResponseType( HttpStatusCode.BadRequest, typeof(ValidationException)]

I believe this will be useful for the various tooling ( Swagger Generators and Help Doc Generators ) to understand the types of HTTP status codes your Web API can return.

@danroth27
Copy link
Member

Swashbuckle defines it's own response attributes for specifying the additional metadata needed for Swagger generation.

@rynowak Should we have built-in attributes for capturing this metadata and make the metadata available via ApiExplorer?

@chadboyer
Copy link
Author

Yes, status codes should be available via the ApiExplorer too. Right now, each vender is creating their own custom attribute to get around this limitation.

Almost all tooling around Web APIs needs to know about the status codes. - help docs, swagger, ect.

@chadboyer
Copy link
Author

Knowing the status codes is every bit as important as knowing the response type.

@danroth27
Copy link
Member

@domaindrivendev FYI

@domaindrivendev
Copy link

Awesome!

@chadboyer
Copy link
Author

+1 - Awesome!

@rynowak
Copy link
Member

rynowak commented Mar 3, 2016

Some design notes on this:

Example

[Produces("application/json, Type = typeof(Widget)]
[ProducesResponseTypeAttribute(typeof(Errors), StatusCodes.BadRequest)]
public IActionResult DoThing() { }

Update IApiResponseMetadataProvider

public class IApiResponseMetadataProvider
{
    int StatusCode { get; set; }
}

Update ProducesAttribute and set StatusCode to 200 by default

Add ProducesResponseTypeAttribute (allow multiple)

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class ProducesResponseTypeAttribute : Attribute, IFilterMetadata, IApiResponseMetadataProvider
{
    public ProducesResponseTypeAttribute(Type type, int statusCode)
    public int StatusCode { get; }
    public Type Type { get; }
}

Move ApiDescription.ResponseModelMetadata and ApiDescription.ResponseType to ApiResponseFormat. Add StatusCode to ApiResponseFormat

Update the DefaultApiDescriptionProvider to take the cross product of "content-type" X "tuple(status code, type)" and run it through conneg.

@rynowak
Copy link
Member

rynowak commented Mar 3, 2016

@kichalla - added some design notes

@domaindrivendev - any concerns with this?

@danroth27
Copy link
Member

Should the ProducesAttribute still have a Type property if we are going to introduce a ProducesResponseTypeAttribute?

@kichalla
Copy link
Member

kichalla commented Mar 3, 2016

👍 on what @danroth27 mentioned.

@rynowak
Copy link
Member

rynowak commented Mar 3, 2016

I think it's nicer to have as a shorthand for when you only want to specify one + you're already specifying a content type.

@khellang
Copy link
Contributor

khellang commented Mar 3, 2016

One thing; would you flip the order of the int and Type parameters?

I think it makes more sense to specify which status code produces which model, instead of the other way around:

[ProducesResponseTypeAttribute(200, typeof(Customer))]
[ProducesResponseTypeAttribute(500, typeof(GenericError))]
[ProducesResponseTypeAttribute(422, typeof(ValidationError))]
public IActionResult DoThing() { }

kichalla added a commit that referenced this issue Mar 8, 2016
…er to expose response type and StatusCode.

[Fixes #4101] StatusCode Metadata
kichalla added a commit that referenced this issue Mar 8, 2016
…er to expose response type and StatusCode.

[Fixes #4101] StatusCode Metadata
kichalla added a commit that referenced this issue Mar 9, 2016
…er to expose response type and StatusCode.

[Fixes #4101] StatusCode Metadata
kichalla added a commit that referenced this issue Mar 9, 2016
…er to expose response type and StatusCode.

[Fixes #4101] StatusCode Metadata
@kichalla
Copy link
Member

I like @khellang's suggestion here. One of the scenarios I am encountering where this makes more sense is with overriding. Example:

[ProducesResponseType(500, typeof(GenericError))]
public class TestController
{
      [Produces("text/json", "application/json", typeof(ProductDetails))]
      [ProducesResponseType(500, typeof(MoreSpecificErrorForThisAction))]
      public IActionResult Details()
      {
              ....
      }
}

So for the Details action and for the 500 status code, the more specific error is used and for the rest of the actions, the one on the controller is used.

@rynowak thoughts?

kichalla added a commit that referenced this issue Mar 10, 2016
…er to expose response type and StatusCode.

[Fixes #4101] StatusCode Metadata
@rynowak
Copy link
Member

rynowak commented Mar 10, 2016

The overriding part just falls out of the nature of being a filter.

re: the ordering of parameters, let's come up with a name that we like and let that dictate what order we use. ProducesResponseType was just a strawman to start work 😆

kichalla added a commit that referenced this issue Mar 10, 2016
…er to expose response type and StatusCode.

[Fixes #4101] StatusCode Metadata
kichalla added a commit that referenced this issue Mar 17, 2016
…er to expose response type and StatusCode.

[Fixes #4101] StatusCode Metadata
kichalla added a commit that referenced this issue Mar 17, 2016
…er to expose response type and StatusCode.

[Fixes #4101] StatusCode Metadata
kichalla added a commit that referenced this issue Mar 24, 2016
…er to expose response type and StatusCode.

[Fixes #4101] StatusCode Metadata
kichalla added a commit that referenced this issue Mar 24, 2016
…er to expose response type and StatusCode.

[Fixes #4101] StatusCode Metadata
kichalla added a commit that referenced this issue Mar 30, 2016
…er to expose response type and StatusCode.

[Fixes #4101] StatusCode Metadata
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

6 participants