diff --git a/aspnetcore/fundamentals/minimal-apis/openapi.md b/aspnetcore/fundamentals/minimal-apis/openapi.md index e5b4b5381398..9478a20b5780 100644 --- a/aspnetcore/fundamentals/minimal-apis/openapi.md +++ b/aspnetcore/fundamentals/minimal-apis/openapi.md @@ -4,7 +4,7 @@ author: rick-anderson description: Learn how to use OpenAPI (Swagger and Swashbuckle) features of minimal APIs in ASP.NET Core. ms.author: riande monikerRange: '>= aspnetcore-6.0' -ms.date: 10/11/2022 +ms.date: 10/24/2022 uid: fundamentals/minimal-apis/openapi --- @@ -198,6 +198,22 @@ When setting the response type for endpoints that may return a ProblemDetails re When there are no explicit annotations provided by one of the strategies above, the framework attempts to determine a default response type by examining the signature of the response. This default response is populated under the `200` status code in the OpenAPI definition. +### Multiple response types + +If an endpoint can return different response types in different scenarios, you can provide metadata in the following ways: + +* Call the [`Produces`](/dotnet/api/microsoft.aspnetcore.http.openapiroutehandlerbuilderextensions.produces) extension method multiple times, as shown in the following example: + + [!code-csharp[](samples/todo/Program.cs?name=snippet_getCustom)] + +* Use [`Results`](xref:Microsoft.AspNetCore.Http.HttpResults.Results%606) in the signature and [`TypedResults`](/dotnet/api/microsoft.aspnetcore.http.typedresults) in the body of the handler, as shown in the following example: + + :::code language="csharp" source="~/../AspNetCore.Docs.Samples/fundamentals/minimal-apis/samples/MultipleResultTypes/Program.cs" id="snippet_multiple_result_types"::: + + The `Results` [union types](https://en.wikipedia.org/wiki/Union_type) declare that a route handler returns multiple `IResult`-implementing concrete types, and any of those types that implement `IEndpointMetadataProvider` will contribute to the endpoint’s metadata. + + The union types implement implicit cast operators. These operators enable the compiler to automatically convert the types specified in the generic arguments to an instance of the union type. This capability has the added benefit of providing compile-time checking that a route handler only returns the results that it declares it does. Attempting to return a type that isn't declared as one of the generic arguments to `Results` results in a compilation error. + ## Describe request body and parameters In addition to describing the types that are returned by an endpoint, OpenAPI also supports annotating the inputs that are consumed by an API. These inputs fall into two categories: