You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+34-1Lines changed: 34 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -70,7 +70,7 @@ app.MapOperations();
70
70
app.Run();
71
71
```
72
72
73
-
Examples:
73
+
## Examples:
74
74
-[OpenAPI 2.0](tests/Example.OpenApi20)
75
75
-[OpenAPI 3.0](tests/Example.OpenApi30)
76
76
-[OpenAPI 3.1](tests/Example.OpenApi31)
@@ -134,6 +134,39 @@ These handlers will not be generated in subsequent compilations as the generator
134
134
</Target>
135
135
```
136
136
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:
"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
+
137
170
## Authentication and Authorization
138
171
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.
0 commit comments