Skip to content

Commit fc294f9

Browse files
authored
Merge pull request #34514 from dotnet/main
2 parents c63514e + b34d888 commit fc294f9

File tree

5 files changed

+77
-14
lines changed

5 files changed

+77
-14
lines changed

aspnetcore/blazor/javascript-interoperability/location-of-javascript.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Inline JavaScript isn't recommended for Blazor apps. We recommend using [JS coll
3939

4040
:::moniker range=">= aspnetcore-8.0"
4141

42-
Only place a `<script>` tag in a component file (`.razor`) if the component is guaranteed to adopt [static server-side rendering (static SSR)](xref:blazor/fundamentals/index#client-and-server-rendering-concepts) because the `<script>` tag can't be updated dynamically. Placing a `<script>` tag in a component file doesn't produce a compile-time warning or error, but script loading behavior might not match your expectations in components that don't adopt static SSR when the component is rendered.
42+
Only place a `<script>` tag in a component file (`.razor`) if the component is guaranteed to adopt [static server-side rendering (static SSR)](xref:blazor/fundamentals/index#client-and-server-rendering-concepts) because the `<script>` tag can't be updated dynamically. Placing a `<script>` tag in a component file doesn't produce a compile-time warning or error, but script loading behavior might not match your expectations in components that adopt an interactive render mode.
4343

4444
:::moniker-end
4545

aspnetcore/fundamentals/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ The request handling pipeline is composed as a series of middleware components.
7171

7272
By convention, a middleware component is added to the pipeline by invoking a `Use{Feature}` extension method. The use of methods named `Use{Feature}` to add middleware to an app is illustrated in the following code:
7373

74-
:::code language="csharp" source="~/fundamentals/index/samples/9.0/BlazorWebAppMovies/Program.cs" id="snippet_middleware" highlight="24-26,29,32":::
74+
:::code language="csharp" source="~/fundamentals/index/samples/9.0/BlazorWebAppMovies/Program.cs" id="snippet_middleware" highlight="26-28,30,32":::
7575

7676
For more information, see <xref:fundamentals/middleware/index>.
7777

aspnetcore/fundamentals/openapi/aspnetcore-openapi.md

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ dotnet add package Microsoft.AspNetCore.OpenApi
4545

4646
The following code:
4747

48-
* Adds OpenAPI services.
49-
* Enables the endpoint for viewing the OpenAPI document in JSON format.
48+
* Adds OpenAPI services using the <xref:Microsoft.Extensions.DependencyInjection.OpenApiServiceCollectionExtensions.AddOpenApi%2A> extension method on the app builder's service collection.
49+
* Maps an endpoint for viewing the OpenAPI document in JSON format with the <xref:Microsoft.AspNetCore.Builder.OpenApiEndpointRouteBuilderExtensions.MapOpenApi%2A> extension method on the app.
5050

5151
[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_first&highlight=3,7)]
5252

@@ -64,7 +64,7 @@ Each OpenAPI document in an app has a unique name. The default document name tha
6464
builder.Services.AddOpenApi(); // Document name is v1
6565
```
6666

67-
The document name can be modified by passing the name as a parameter to the `AddOpenApi` call.
67+
The document name can be modified by passing the name as a parameter to the <xref:Microsoft.Extensions.DependencyInjection.OpenApiServiceCollectionExtensions.AddOpenApi%2A> call.
6868

6969
```csharp
7070
builder.Services.AddOpenApi("internal"); // Document name is internal
@@ -116,6 +116,36 @@ The OpenAPI document is regenerated every time a request to the OpenAPI endpoint
116116

117117
[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_mapopenapiwithcaching)]
118118

119+
## Generate multiple OpenAPI documents
120+
121+
In some scenarios, it's helpful to generate multiple OpenAPI documents with different content from a single ASP.NET Core API app. These scenarios include:
122+
123+
* Generating OpenAPI documentation for different audiences, such as public and internal APIs.
124+
* Generating OpenAPI documentation for different versions of an API.
125+
* Generating OpenAPI documentation for different parts of an application, such as a frontend and backend API.
126+
127+
To generate multiple OpenAPI documents, call the <xref:Microsoft.Extensions.DependencyInjection.OpenApiServiceCollectionExtensions.AddOpenApi%2A> extension method once for each document, specifying a different document name in the first parameter each time.
128+
129+
```csharp
130+
builder.Services.AddOpenApi("v1");
131+
builder.Services.AddOpenApi("v2");
132+
```
133+
134+
Each invocation of <xref:Microsoft.Extensions.DependencyInjection.OpenApiServiceCollectionExtensions.AddOpenApi%2A> can specify its own set of options, so that you can choose to use the same or different customizations for each OpenAPI document.
135+
136+
The framework uses the <xref:Microsoft.AspNetCore.OpenApi.OpenApiOptions.ShouldInclude> delegate method of <xref:Microsoft.AspNetCore.OpenApi.OpenApiOptions> to determine which endpoints to include in each document.
137+
138+
For each document, the <xref:Microsoft.AspNetCore.OpenApi.OpenApiOptions.ShouldInclude> delegate method is called for each endpoint in the application, passing the <xref:Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescription> object for the endpoint. The method returns a boolean value indicating whether the endpoint should be included in the document. The <xref:Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescription> object contains information about the endpoint, such as the HTTP method, route, and response types, as well as metadata attached to the endpoint via attributes or extension methods.
139+
140+
The default implementation of this delegate uses the <xref:Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescription.GroupName> field of <xref:Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescription>, which is set on an endpoint using either the <xref:Microsoft.AspNetCore.Builder.RoutingEndpointConventionBuilderExtensions.WithGroupName%2A> extension method or the <xref:Microsoft.AspNetCore.Routing.EndpointGroupNameAttribute> attribute, to determine which endpoints to include in the document. Any endpoint that has not been assigned a group name is included all OpenAPI documents.
141+
142+
```csharp
143+
// Include endpoints without a group name or with a group name that matches the document name
144+
ShouldInclude = (description) => description.GroupName == null || description.GroupName == DocumentName;
145+
```
146+
147+
You can customize the <xref:Microsoft.AspNetCore.OpenApi.OpenApiOptions.ShouldInclude> delegate method to include or exclude endpoints based on any criteria you choose.
148+
119149
## Generate OpenAPI documents at build-time
120150

121151
In typical web applications, OpenAPI documents are generated at run-time and served via an HTTP request to the application server.

aspnetcore/fundamentals/openapi/customize-openapi.md

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ uid: fundamentals/openapi/customize-openapi
1414

1515
## OpenAPI document transformers
1616

17-
This section demonstrates how to customize OpenAPI documents with transformers.
18-
19-
### Customize OpenAPI documents with transformers
20-
2117
Transformers provide an API for modifying the OpenAPI document with user-defined customizations. Transformers are useful for scenarios like:
2218

2319
* Adding parameters to all operations in a document.
@@ -50,7 +46,7 @@ Transformers execute in first-in first-out order based on registration. In the f
5046

5147
[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_transInOut&highlight=3-9)]
5248

53-
### Use document transformers
49+
## Use document transformers
5450

5551
Document transformers have access to a context object that includes:
5652

@@ -73,7 +69,7 @@ Document transformers are unique to the document instance they're associated wit
7369

7470
[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_multidoc_operationtransformer1)]
7571

76-
### Use operation transformers
72+
## Use operation transformers
7773

7874
Operations are unique combinations of HTTP paths and methods in an OpenAPI document. Operation transformers are helpful when a modification:
7975

@@ -90,7 +86,7 @@ For example, the following operation transformer adds `500` as a response status
9086

9187
[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_operationtransformer1)]
9288

93-
### Use schema transformers
89+
## Use schema transformers
9490

9591
Schemas are the data models that are used in request and response bodies in an OpenAPI document. Schema transformers are useful when a modification:
9692

@@ -107,6 +103,43 @@ For example, the following schema transformer sets the `format` of decimal types
107103

108104
[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_schematransformer1)]
109105

106+
## Customize schema reuse
107+
108+
After all transformers have been applied, the framework makes a pass over the document to transfer certain schemas
109+
to the `components.schemas` section, replacing them with `$ref` references to the transferred schema.
110+
This reduces the size of the document and makes it easier to read.
111+
112+
The details of this processing are complicated and might change in future versions of .NET, but in general:
113+
114+
* Schemas for class/record/struct types are replaced with a `$ref` to a schema in `components.schemas`
115+
if they appear more than once in the document.
116+
* Schemas for primitive types and standard collections are left inline.
117+
* Schemas for enum types are always replaced with a `$ref` to a schema in components.schemas.
118+
119+
Typically the name of the schema in `components.schemas` is the name of the class/record/struct type,
120+
but in some circumstances a different name must be used.
121+
122+
ASP.NET Core lets you customize which schemas are replaced with a `$ref` to a schema in `components.schemas`
123+
using the <xref:Microsoft.AspNetCore.OpenApi.OpenApiOptions.CreateSchemaReferenceId> property of <xref:Microsoft.AspNetCore.OpenApi.OpenApiOptions>.
124+
This property is a delegate that takes a <xref:System.Text.Json.Serialization.Metadata.JsonTypeInfo> object and returns the name of the schema
125+
in `components.schemas` that should be used for that type.
126+
The framework provides a default implementation of this delegate, <xref:Microsoft.AspNetCore.OpenApi.OpenApiOptions.CreateDefaultSchemaReferenceId%2A>
127+
that uses the name of the type, but you can replace it with your own implementation.
128+
129+
As a simple example of this customization, you might choose to always inline enum schemas.
130+
This is done by setting <xref:Microsoft.AspNetCore.OpenApi.OpenApiOptions.CreateSchemaReferenceId> to a delegate
131+
that returns null for enum types, and otherwise returns the value from the default implementation.
132+
The following code shows how to do this:
133+
134+
```csharp
135+
builder.Services.AddOpenApi(options =>
136+
{
137+
// Always inline enum schemas
138+
options.CreateSchemaReferenceId = (type) =>
139+
type.Type.IsEnum ? null : OpenApiOptions.CreateDefaultSchemaReferenceId(type);
140+
});
141+
```
142+
110143
## Additional resources
111144

112145
* <xref:fundamentals/openapi/using-openapi-documents>

aspnetcore/fundamentals/openapi/include-metadata.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,8 +489,8 @@ The following table summarizes attributes from the `System.ComponentModel` names
489489
| [`[Required]`](xref:System.ComponentModel.DataAnnotations.RequiredAttribute) | Marks a property as `required` in the schema. |
490490
| [`[DefaultValue]`](xref:System.ComponentModel.DefaultValueAttribute) | Sets the `default` value of a property in the schema. |
491491
| [`[Range]`](xref:System.ComponentModel.DataAnnotations.RangeAttribute) | Sets the `minimum` and `maximum` value of an integer or number. |
492-
| [`[MinLength]`](xref:System.ComponentModel.DataAnnotations.MinLengthAttribute) | Sets the `minLength` of a string. |
493-
| [`[MaxLength]`](xref:System.ComponentModel.DataAnnotations.MaxLengthAttribute) | Sets the `maxLength` of a string. |
492+
| [`[MinLength]`](xref:System.ComponentModel.DataAnnotations.MinLengthAttribute) | Sets the `minLength` of a string or `minItems` of an array. |
493+
| [`[MaxLength]`](xref:System.ComponentModel.DataAnnotations.MaxLengthAttribute) | Sets the `maxLength` of a string or `maxItems` of an array. |
494494
| [`[RegularExpression]`](xref:System.ComponentModel.DataAnnotations.RegularExpressionAttribute) | Sets the `pattern` of a string. |
495495
496496
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

Comments
 (0)