QUESTION: How to generate "examples:" section for an API endpoint?
jehrenzweig-leagueapps opened this issue · 1 comments
jehrenzweig-leagueapps commented
Is there an attribute that will generate an OpenAPI examples:
section, which can describe multiple API responses for a single HTTP status code? Something like this (the examples:
node is declared in the last 12 lines of the snippet):
paths:
'/cart/clear':
post:
tags:
- CartApi
summary: Delete all items in a cart.
description: <p><i>BLAH BLAH BLAH.</i></p>
operationId: CartApi_DeleteAllItemsInCart
responses:
'200':
description: <p>The API request was successfully processed; an object describing the empty shopping cart is returned.</p>
content:
application/json:
schema:
$ref: '#/components/schemas/GetCartSummaryResponse'
example:
cartGuid: 0fa1ef5f-5395-4679-901d-fd6c941f3460
cartItems: []
'422':
description: The API request was unsuccessfully processed; some other problem happened.
content:
application/json:
schema:
$ref: '#/components/schemas/ProblemDetails'
examples:
Shopping cart ID is null or whitespace:
value:
type: 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422'
title: Shopping cart was not found.
status: 422
detail: Shopping cart ID is null, empty, blank, or whitespace.
Shopping cart ID not found:
value:
type: 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422'
title: Shopping cart was not found.
status: 422
detail: Shopping cart ID was not found.
cartGuid: 0fa1ef5f-5395-4679-901d-fd6c941f3460
Here's how I'm currently adding a single response example per HTTP status code:
[SwaggerOperation(summary: "Delete all items in a cart.", description: "<p>BLAH BLAH BLAH.</p>")]
[SwaggerResponse(statusCode: 200, description: "<p>The API request was successfully processed; an object describing the empty shopping cart is returned.</p>", type: typeof(GetCartSummaryResponse)), SwaggerResponseExample(statusCode: 200, examplesProviderType: typeof(CartApi_DeleteAllItemsInCart_Http200ResponseExampleProvider))]
[SwaggerResponse(statusCode: 422, description: "The API request was unsuccessfully processed; one or more business requirements failed to be met as the request was processed.", type: typeof(ProblemDetails))]
[SwaggerResponseExample(statusCode: 422, examplesProviderType: typeof(CartApi_DeleteAllItemsInCart_Http422ResponseExampleProvider))]
[HttpPost("/cart/clear")]
[Consumes("application/json")]
[Produces("application/json")]
public ActionResult<GetCartSummaryResponse> DeleteAllItemsInCart()
{
throw new NotImplementedException();
}
However, using the SwaggerResponseExample
annotation in this fashion only generates an OpenAPI example:
section:
Generates an OpenAPI "example:" (singular) section:
'422':
description: The API request was unsuccessfully processed; some other problem happened.
content:
application/json:
schema:
$ref: '#/components/schemas/ProblemDetails'
example: {"type":"https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422","title":"Shopping cart was not found.","status":422,"detail":"Shopping cart ID was not found.","cartGuid":"0fa1ef5f-5395-4679-901d-fd6c941f3460"}
jehrenzweig-leagueapps commented
I just found the solution to my problem: use IMultipleExamplesProvider<T>
instead of IExampleProvider<T>
. Works like a charm.