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
* Add ProducesResponseType Description documentation
The "What's new in .NET 10" release notes mention the new Description property I added, but the .NET 10 OpenAPI docs do not.
This commit adds consistency
* Updates
* Apply suggestions from code review
* Cross-link for the Minimal API content
---------
Co-authored-by: Luke Latham <[email protected]>
@@ -133,13 +135,14 @@ In controller-based apps, the operationId can be set using the [`[EndpointName]`
133
135
The following sample demonstrates how to set the operationId.
134
136
135
137
```csharp
136
-
[EndpointName("FromAttributes")]
137
-
[HttpGet("attributes")]
138
-
publicIResultAttributes()
139
-
{
140
-
returnResults.Ok("Hello world!");
141
-
}
138
+
[EndpointName("FromAttributes")]
139
+
[HttpGet("attributes")]
140
+
publicIResultAttributes()
141
+
{
142
+
returnResults.Ok("Hello world!");
143
+
}
142
144
```
145
+
143
146
---
144
147
145
148
### parameters
@@ -152,24 +155,31 @@ The [`[Description]`](xref:System.ComponentModel.DescriptionAttribute) attribute
152
155
153
156
#### [Minimal APIs](#tab/minimal-apis)
154
157
158
+
The [`[Description]`](xref:System.ComponentModel.DescriptionAttribute) attribute works in an MVC app but doesn't work in a Minimal API app at this time. For more information, see [`Description` parameter of `ProducesResponseTypeAttribute` does not work in minimal API app (`dotnet/aspnetcore`#60518)](https://github.com/dotnet/aspnetcore/issues/60518).
159
+
160
+
<!-- For activation when https://github.com/dotnet/aspnetcore/issues/60518 is resolved ...
161
+
155
162
The follow sample demonstrates how to set a description for a parameter.
Since `application/xml` is not a built-in content type, the `Todo` class must implement the <xref:Microsoft.AspNetCore.Http.IBindableFromHttpContext%601> interface to provide a custom binding for the request body. For example:
@@ -232,6 +242,7 @@ public class Todo : IBindableFromHttpContext<Todo>
If the endpoint doesn't define any parameters bound to the request body, use the <xref:Microsoft.AspNetCore.Http.OpenApiRouteHandlerBuilderExtensions.Accepts%2A> extension method to specify the content type that the endpoint accepts.
@@ -292,7 +303,7 @@ The <xref:Microsoft.AspNetCore.Http.OpenApiRouteHandlerBuilderExtensions.Produce
The [`[ProducesResponseType]`](xref:Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttribute) can be used to add response metadata to an endpoint. Note that the attribute is applied to the route handler method, not the method invocation to create the route, as shown in the following example:
@@ -303,6 +314,21 @@ app.MapGet("/todos",
303
314
async (TodoDbdb) =>awaitdb.Todos.ToListAsync());
304
315
```
305
316
317
+
:::moniker range=">= aspnetcore-10.0"
318
+
319
+
[`[ProducesResponseType]`](xref:Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttribute), [`[Produces]`](xref:Microsoft.AspNetCore.Mvc.ProducesAttribute), and [`[ProducesDefaultResponseType]`](xref:Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttribute) also support an optional string property called `Description` that can be used to describe the response. This is useful for explaining why or when clients can expect a specific response:
320
+
321
+
```csharp
322
+
app.MapGet("/todos/{id}",
323
+
[ProducesResponseType<Todo>(200,
324
+
Description="Returns the requested Todo item.")]
325
+
[ProducesResponseType(404, Description="Requested item not found.")]
326
+
[ProducesDefault(Description="Undocumented status code.")]
327
+
async (intid, TodoDbdb) =>/* Code here */);
328
+
```
329
+
330
+
:::moniker-end
331
+
306
332
Using <xref:Microsoft.AspNetCore.Http.TypedResults> in the implementation of an endpoint's route handler automatically includes the response type metadata for the endpoint. For example, the following code automatically annotates the endpoint with a response under the `200` status code with an `application/json` content type.
Only return types that implement <xref:Microsoft.AspNetCore.Http.Metadata.IEndpointMetadataProvider> create a `responses` entry in the OpenAPI document. The following is a partial list of some of the <xref:Microsoft.AspNetCore.Http.TypedResults> helper methods that produce a `responses` entry:
317
343
318
-
| TypedResults helper method | status code |
319
-
| -------------------------- | ----------- |
320
-
| Ok() | 200 |
321
-
| Created() | 201 |
322
-
| CreatedAtRoute() | 201 |
323
-
| Accepted() | 202 |
324
-
| AcceptedAtRoute() | 202 |
325
-
| NoContent() | 204 |
326
-
| BadRequest() | 400 |
327
-
| ValidationProblem() | 400 |
328
-
| NotFound() | 404 |
329
-
| Conflict() | 409 |
330
-
| UnprocessableEntity() | 422 |
344
+
|`TypedResults` helper method |Status code |
345
+
| ----------------------------| ----------- |
346
+
| Ok() | 200 |
347
+
| Created() | 201 |
348
+
| CreatedAtRoute() | 201 |
349
+
| Accepted() | 202 |
350
+
| AcceptedAtRoute() | 202 |
351
+
| NoContent() | 204 |
352
+
| BadRequest() | 400 |
353
+
| ValidationProblem() | 400 |
354
+
| NotFound() | 404 |
355
+
| Conflict() | 409 |
356
+
| UnprocessableEntity() | 422 |
331
357
332
358
All of these methods except `NoContent` have a generic overload that specifies the type of the response body.
333
359
@@ -372,12 +398,29 @@ Only one [`[Produces]`](xref:Microsoft.AspNetCore.Mvc.ProducesAttribute) or <xre
372
398
373
399
All of the above attributes can be applied to individual action methods or to the controller class where it applies to all action methods in the controller.
374
400
401
+
:::moniker range=">= aspnetcore-10.0"
402
+
403
+
[`[ProducesResponseType]`](xref:Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttribute), [`[Produces]`](xref:Microsoft.AspNetCore.Mvc.ProducesAttribute), and [`[ProducesDefaultResponseType]`](xref:Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttribute) also support an optional string property called `Description` that can be used to describe the response. This is useful for explaining why or when clients can expect a specific response:
* the status code for the response defaults to 200,
377
-
* the schema for the response body of 2xx responses may be inferred from the return type of the action method, e.g. from `T` in <xref:Microsoft.AspNetCore.Mvc.ActionResult%601>, but otherwise is considered to be not specified,
378
-
* the schema for the response body of 4xx responses is inferred to be a problem details object,
379
-
* the schema for the response body of 3xx and 5xx responses is considered to be not specified,
380
-
* the content-type for the response body can be inferred from the return type of the action method and the set of output formatters.
418
+
419
+
* The status code for the response defaults to 200.
420
+
* The schema for the response body of 2xx responses may be inferred from the return type of the action method, e.g. from `T` in <xref:Microsoft.AspNetCore.Mvc.ActionResult%601>, but otherwise is considered to be not specified.
421
+
* The schema for the response body of 4xx responses is inferred to be a problem details object.
422
+
* The schema for the response body of 3xx and 5xx responses is considered to be not specified.
423
+
* The content-type for the response body can be inferred from the return type of the action method and the set of output formatters.
381
424
382
425
By default, there are no compile-time checks to ensure that the response metadata specified with a [`[ProducesResponseType]` attribute](xref:Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttribute) is consistent with the actual behavior of the action method, which may return a different status code or response body type than specified by the metadata. To enable these checks, [enable Web API analyzers](xref:web-api/advanced/analyzers).
383
426
@@ -412,11 +455,11 @@ The following sample demonstrates the different strategies for excluding a given
@@ -426,12 +469,13 @@ In controller-based apps, the [`[ApiExplorerSettings]`](xref:Microsoft.AspNetCor
426
469
The following example demonstrates how to exclude an endpoint from the generated OpenAPI document:
427
470
428
471
```csharp
429
-
[HttpGet("/private")]
430
-
[ApiExplorerSettings(IgnoreApi = true)]
431
-
public IActionResult PrivateEndpoint() {
432
-
return Ok("This is a private endpoint");
433
-
}
472
+
[HttpGet("/private")]
473
+
[ApiExplorerSettings(IgnoreApi=true)]
474
+
publicIActionResultPrivateEndpoint() {
475
+
returnOk("This is a private endpoint");
476
+
}
434
477
```
478
+
435
479
---
436
480
437
481
## Include OpenAPI metadata for data types
@@ -484,12 +528,12 @@ The following table summarizes attributes from the `System.ComponentModel` names
484
528
485
529
| Attribute | Description |
486
530
| ---------------------------- | ----------- |
487
-
| [`[Description]`](xref:System.ComponentModel.DescriptionAttribute) | Sets the `description` of a property in the schema. |
488
-
| [`[Required]`](xref:System.ComponentModel.DataAnnotations.RequiredAttribute) | Marks a property as `required` in the schema. |
489
-
| [`[DefaultValue]`](xref:System.ComponentModel.DefaultValueAttribute) | Sets the `default` value of a property in the schema. |
490
-
| [`[Range]`](xref:System.ComponentModel.DataAnnotations.RangeAttribute) | Sets the `minimum` and `maximum` value of an integer or number. |
491
-
| [`[MinLength]`](xref:System.ComponentModel.DataAnnotations.MinLengthAttribute) | Sets the `minLength` of a string or `minItems` of an array. |
492
-
| [`[MaxLength]`](xref:System.ComponentModel.DataAnnotations.MaxLengthAttribute) | Sets the `maxLength` of a string or `maxItems` of an array. |
531
+
|[`[Description]`](xref:System.ComponentModel.DescriptionAttribute)| Sets the `description` of a property in the schema. |
532
+
|[`[Required]`](xref:System.ComponentModel.DataAnnotations.RequiredAttribute)| Marks a property as `required` in the schema. |
533
+
|[`[DefaultValue]`](xref:System.ComponentModel.DefaultValueAttribute)| Sets the `default` value of a property in the schema. |
534
+
|[`[Range]`](xref:System.ComponentModel.DataAnnotations.RangeAttribute)| Sets the `minimum` and `maximum` value of an integer or number. |
535
+
|[`[MinLength]`](xref:System.ComponentModel.DataAnnotations.MinLengthAttribute)| Sets the `minLength` of a string or `minItems` of an array. |
536
+
|[`[MaxLength]`](xref:System.ComponentModel.DataAnnotations.MaxLengthAttribute)| Sets the `maxLength` of a string or `maxItems` of an array. |
493
537
|[`[RegularExpression]`](xref:System.ComponentModel.DataAnnotations.RegularExpressionAttribute)| Sets the `pattern` of a string. |
494
538
495
539
Note that in controller-based apps, these attributes add filters to the operation to validate that any incoming data satisfies the constraints. In Minimal APIs, these attributes set the metadata in the generated schema but validation must be performed explicitly via an endpoint filter, in the route handler's logic, or via a third-party package.
0 commit comments