Reputation: 382
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?
Upvotes: 8
Views: 5387
Reputation: 757
Consider not caring about:
Just respond with a NoContent whether or not you actually deleted something or not. If it is not there, it is not there, which is exactly what you wanted to happen.
Why do I advice to do this? Because it makes your API less complex and it keeps you from having to think about the situation where either the cart or the cart item is not there at all.
But what if my DB throws when I try to delete something that is not there? Just catch that and pretend you deleted it. Make your API respond with NoContent, because there really is no content.
Upvotes: -1
Reputation: 17604
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)
Upvotes: 3