Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swashbuckle: Multiple [SwaggerResponse] with the same status code but different description/model

I'm writing a demonstration shopping cart API and I've come across a situation where I can return NotFound for multiple reasons (either a cart item isn't found or the cart itself is not found). I want to return a Swagger description for both cases. I've tried this but it doesn't work.

    [SwaggerResponse(HttpStatusCode.NotFound, Type = typeof(CartNotFoundResponse), Description = "Cart not found (code=1)")]
    [SwaggerResponse(HttpStatusCode.NotFound, Type = typeof(ItemNotFoundResponse), Description = "Item not found (code=104)")]
    [SwaggerResponse(HttpStatusCode.NoContent)]
    public async Task<IHttpActionResult> DeleteItemWithSKUAsync(Guid cartId, string sku)
    {
    }

Any suggestions?

like image 290
yozepi Avatar asked Jun 20 '17 22:06

yozepi


1 Answers

Unfortunately that is not possible with the current implementation: https://github.com/domaindrivendev/Swashbuckle/blob/master/Swashbuckle.Core/Swagger/SwaggerDocument.cs#L29

As you can see the responses is a dictionary and the key is the StatusCode.

My recommendation: use a more generic class that can cover both of you cases (CartNotFound & ItemNotFound)

like image 72
Helder Sepulveda Avatar answered Nov 01 '22 23:11

Helder Sepulveda