Skip to content

Commit 661dc3e

Browse files
committed
doc(response): add content negotiation to README
1 parent fd64fd7 commit 661dc3e

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ app.MapOperations();
7070
app.Run();
7171
```
7272

73-
Examples:
73+
## Examples:
7474
- [OpenAPI 2.0](tests/Example.OpenApi20)
7575
- [OpenAPI 3.0](tests/Example.OpenApi30)
7676
- [OpenAPI 3.1](tests/Example.OpenApi31)
@@ -134,6 +134,39 @@ These handlers will not be generated in subsequent compilations as the generator
134134
</Target>
135135
```
136136

137+
## Content Negotiation
138+
Content is negotiated for both request and responses.
139+
140+
See the [examples](#examples) for more details.
141+
### Request Body Content
142+
Request body content is automatically mapped via the [Content-Type](https://datatracker.ietf.org/doc/html/rfc9110#field.content-type) header. The `Request.Body` property has content properties generated for all specified content which can be tested for nullability to figure out which one was sent.
143+
144+
If `Body` is optional, all content properties might be null.
145+
146+
If body is not defined for the request, there will be no `Body` property generated.
147+
148+
### Response Content
149+
Response content can be negotiated using the `TryMatchAcceptMediaType` method exposed by the `Request` class. Call it with the wanted response and it will return the best content matching the [Accept](https://datatracker.ietf.org/doc/html/rfc9110#name-accept) header.
150+
151+
This method can only be used with response that define content, and it is scoped to responses defined by the current operation.
152+
153+
Example:
154+
```dotnet
155+
switch (request.TryMatchAcceptMediaType<Response.OK200>(out var matchedMediaType))
156+
{
157+
// No match, the server decides what to do
158+
case false:
159+
// Matched any application content (application/*)
160+
case true when matchedMediaType == Response.OK200.AnyApplication.ContentMediaType:
161+
return Task.FromResult<Response>(new Response.OK200.AnyApplication(
162+
Components.Schemas.FooProperties.Create(name: request.Body.ApplicationJson?.Name),
163+
"application/json") { Headers = new Response.OK200.ResponseHeaders { Status = 2 } });
164+
// Matched content that has not been implemented yet by the operation handler (can be used to detect newly specified content that has not yet been implemented)
165+
default:
166+
throw new NotImplementedException($"Content media type {matchedMediaType} has not been implemented");
167+
}
168+
```
169+
137170
## Authentication and Authorization
138171
OpenAPI defines [security scheme objects](https://spec.openapis.org/oas/latest#security-scheme-object) for authentication and authorization mechanisms. The generator implement endpoint filters that corresponds to the security declaration of each operation. Do _not_ call `UseAuthentication` or similar when configuring the application.
139172

0 commit comments

Comments
 (0)