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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With