Swashbuckle.aspnetcore: Suggestion: System.Net.HttpStatusCode support in SwaggerResponseAttribute constructor

Created on 13 Jan 2017  路  3Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

It would be great, if it was possible to write [SwaggerResponse(HttpStatusCode.BadRequest)] instead of [SwaggerResponse(400)]. For example

    [SwaggerResponse(HttpStatusCode.BadRequest)]
    public async Task<IActionResult> MyMethod(MyModel model)
    {
      if (!ModelState.IsValid)
        return BadRequest();
      ...

would be more readable than

    [SwaggerResponse(400)]
    public async Task<IActionResult> MyMethod(MyModel model)
    {
      if (!ModelState.IsValid)
        return BadRequest();
      ...

as you can directly see that the [SwaggerResponse(HttpStatusCode.BadRequest)] relates to the return BadRequest();.

This could be achieved by adding a constructor to the SwaggerResponseAttribute that has a System.Net.HttpStatusCode statusCode parameter instead of the int statusCode. System.Net.HttpStatusCode is in System.Net.Primitives which is already (indirectly) referenced by Swashbuckle.SwaggerGen.

Most helpful comment

Simple reason: because I didn't know about them. Thanks for the tip.

All 3 comments

I think this makes sense, and PR should be the one line kind.

why not use the StatusCodes constants in Microsoft.AspNetCore.Http?

[SwaggerResponse(StatusCodes.Status400BadRequest)]

they are already ints and you can do it today :)

Simple reason: because I didn't know about them. Thanks for the tip.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

govin picture govin  路  3Comments

alasvant picture alasvant  路  3Comments

NinoFloris picture NinoFloris  路  3Comments

JoelAdamWeiss picture JoelAdamWeiss  路  4Comments

brucewilkins picture brucewilkins  路  3Comments