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
# OpenAPI XML documentation comment support in ASP.NET Core
13
13
14
-
ASP.NET Core will include metadata from XML doc comments in the generated OpenAPI document. No changes are required in app code to enable XML documentation comment support, but the project must be configured to generate the XML documentation file. ASP.NET Core automatically detects XML documentation comments in the application assembly and any referenced assemblies that have XML documentation enabled.
14
+
OpenAPI XML documentation extracts code comments automatically to populate API documentation, ensuring the code and documentation remain synchronized. Metadata from XML documentation comments is included in the generated OpenAPI document without requiring changes to the app code, as long as the project is configured to generate the XML documentation file. XML documentation comments are automatically detected in the application assembly and referenced assemblies with XML documentation enabled.
15
15
16
16
ASP.NET Core processes [XML documentation tags](https://learn.microsoft.com/dotnet/csharp/language-reference/xmldoc/recommended-tags) like: `<c>`, `<code>`, `<list>`, `<para>`, `<paramref>`, `<typeparamref>`, `<see>`, and `<seealso>`.
17
17
For XML documentation tags that use references to other elements, like `<see cref="SomeOtherType">`, the implementation strips out the XML tag and maps the reference to plain text for inclusion in the OpenAPI document.
@@ -52,7 +52,7 @@ To turn off XML documentation integration, remove the source generator from the
52
52
53
53
## Source generator implementation notes
54
54
55
-
The source generator implementation is open-source and can be found in the [ASP.NET Core repository](https://github.com/dotnet/aspnetcore/tree/main/src/OpenApi/gen).
55
+
56
56
57
57
The XML documentation feature is implemented as a source generator. The source generator analyzes XML documentation comments at compile time and injects code that translates these comments into OpenAPI metadata. The [`XmlCommentGenerator`](https://source.dot.net/#Microsoft.AspNetCore.OpenApi.SourceGenerators/XmlCommentGenerator.cs,30eb0aa73ef6306a) extracts XML comments from two sources:
58
58
@@ -68,27 +68,28 @@ XML comments are parsed into structured `XmlComment` objects with:
68
68
* Response documentation with status codes and descriptions.
69
69
* Support for examples and deprecated markers.
70
70
71
+
### `<inheritdoc/>`
71
72
72
-
### Support for `<inheritdoc/>`
73
+
The generator supports [`<inheritdoc>`](/dotnet/csharp/language-reference/xmldoc/recommended-tags#inheritdoc) tags, which inherit documentation *as long as they exist in the compilation assembly*. `<inheritdoc/>` tags indicate that comments must be resolved from:
73
74
74
-
<!-- original - review my change
75
-
`<inheritdoc />` tags present an important opportunity because they indicate that comments must be resolved from a base class or implemented interface.
76
-
The source generator uses its knowledge of the symbol's present in the compilation to discover base classes and interfaces associated with the symbol a given `<inheritdoc />` is placed on and supports resolving them automatically
77
-
-->
75
+
* Base classes
76
+
* Implemented interfaces
77
+
* Base methods for overrides
78
78
79
-
`<inheritdoc />` tags indicate that comments must be resolved from a base class or implemented interface. When `<inheritdoc />` is placed on a symbol, The source generator:
79
+
When `<inheritdoc />` is placed on a symbol, The source generator:
80
80
81
81
* Uses its knowledge of the symbol in the compilation to discover base classes and interfaces associated with the symbol.
82
82
* Supports resolving them automatically
83
+
* Substitutes generic type parameters in inherited documentation comments, preserving type references across inheritance boundaries.
83
84
84
85
The automatic resolution behavior is currently available for XML documentation comments that exist in the assembly under compilation, and ***not*** XML documentation tags that are in referenced projects or packages. In the later scenario, XML documentation comments:
85
86
86
87
* Are only presented as text.
87
-
*There's no trivial strategy for:
88
+
*Don't provide a trivial strategy for:
88
89
* Associating the text content to compilation symbols.
89
90
* Developing an understanding of the inheritance hierarchy associated with the types.
90
91
91
-
### Member Identification
92
+
<!--### Member identification - `MemberKey` removed in preview 3.
92
93
93
94
The source generator discovers XML comments statically and emits code that applies them to the document dynamically at runtime. The [`MemberKey`](https://source.dot.net/#Microsoft.AspNetCore.OpenApi.SourceGenerators/XmlComments/MemberKey.cs,d182ed147edb11d2) class acts as a bridge between compile-time and runtime representations of the same concept. It's a unique identifier for types, methods, and properties that works across:
94
95
@@ -105,17 +106,13 @@ internal sealed record MemberKey(
105
106
string? Name,
106
107
string? ReturnType,
107
108
string[]? Parameters) : IEquatable<MemberKey>
108
-
```
109
+
```-->
109
110
110
111
### Code Generation
111
112
112
113
The generator emits code that contains:
113
114
114
115
* A cache of XML comments mapped to member identifiers:
*[`XmlCommentOperationTransformer`](https://github.com/dotnet/aspnetcore/blob/main/src/OpenApi/gen/XmlCommentGenerator.Emitter.cs#L229): Applies comments to API operations (methods).
121
118
*[`XmlCommentSchemaTransformer`](https://github.com/dotnet/aspnetcore/blob/main/src/OpenApi/gen/XmlCommentGenerator.Emitter.cs#L308): Applies comments to data models (types).
@@ -159,11 +156,11 @@ When the generated code runs:
159
156
160
157
The generator processes standard XML code comments and adds them to the generated OpenAPI documentation.
161
158
162
-
## Frequently Asked Questions
159
+
<!--## Frequently Asked Questions
163
160
164
-
### What is the OpenAPI XML documentation support feature?
161
+
Moved to intro ### What is the OpenAPI XML documentation support feature?
165
162
166
-
The feature automatically extracts XML documentation comments from code and uses them to populate OpenAPI documentation. API documentation is generated directly from code comments, keeping them in sync.
163
+
The feature automatically extracts XML documentation comments from code and uses them to populate OpenAPI documentation. API documentation is generated directly from code comments, keeping them in sync.-->
167
164
168
165
### How does the XML documentation support work?
169
166
@@ -226,21 +223,11 @@ To add examples to documentation, use the [`<example>`](/dotnet/csharp/language-
The generator supports [`<inheritdoc>`](/dotnet/csharp/language-reference/xmldoc/recommended-tags#inheritdoc) tags, which inherit documentation from *as long as they exist in the compilation assembly*:
232
-
233
-
* Base classes
234
-
* Implemented interfaces
235
-
* Base methods for overrides
236
-
237
-
### How are generic type parameters handled when inheriting documentation?
238
-
239
-
The source generator substitutes generic type parameters in inherited documentation comments, preserving type references across inheritance boundaries.
226
+
<!-- move FAQ to topic -->
240
227
241
-
### How does the source generator identify and track API members?
228
+
### How does the source generator identify and track API members? zzz move to impl
242
229
243
-
It uses the [`MemberKey`](https://github.com/dotnet/aspnetcore/blob/main/src/OpenApi/gen/XmlComments/MemberKey.cs) class to create a unique identifier for each API member that encodes:
230
+
The source generator uses an [`XmlComment Cache`](https://github.com/dotnet/aspnetcore/blob/main/src/OpenApi/gen/XmlCommentGenerator.Emitter.cs#L84-L96) class to identify and track API members. It create a unique identifier for each API member that encodes:
244
231
245
232
* Declaring and property types.
246
233
* Method names.
@@ -303,7 +290,8 @@ Navigate to [http://localhost:5052/](http://localhost:5052/) to view the Scalar
0 commit comments