Codit / practical-api-guidelines

Practical guidelines for building & designing APIs with .NET.
MIT License
16 stars 5 forks source link

SwaggerConsumesProduces attribute #85

Closed MassimoC closed 5 years ago

MassimoC commented 5 years ago

As part of #19 SwaggerConsumesProduces attribute introduced to have more control on the produced/consumed media types

tomkerkhove commented 5 years ago

Isn't there a built-in way to achieve this?

fgheysels commented 5 years ago

I'm wondering why a custom attribute has been created to achieve this instead of using the ConsumesAttribute and ProducesAttribute

Having your operations decorated with these attributes should also affect the Swagger documentation.

You could for instance have this on the GetPlayer method:

[SwaggerResponse((int)HttpStatusCode.OK, "Player data object")]
[SwaggerResponse((int)HttpStatusCode.NotFound, "Player not found")]         
[SwaggerResponse((int)HttpStatusCode.InternalServerError, "API is not available")]
[Produces("application/json")]
public async Task<IActionResult> GetPlayer(int id) {}

or this on the CreatePlayer method:

[SwaggerResponse((int)HttpStatusCode.Created, "Player has been created. New playes is returned along with the link to the new resource (Location header)")]
[SwaggerResponse((int)HttpStatusCode.BadRequest, "Invalid request")]            
[SwaggerResponse((int)HttpStatusCode.InternalServerError, "API is not available")]
[Consumes("application/json", "text/json")]
[Produces("application/json")]
public async Task<IActionResult> Create(NewPlayerDto player) {}
MassimoC commented 5 years ago

Because I am completely missed the built-in attributes. Will drop the custom attribute and introduce these ones

MassimoC commented 5 years ago

content-negotiation moved to level 2