Skip to content

Commit 74dc6cb

Browse files
authored
Prep: WN .NET 10: OpenAPI: XML doc comments (#34986)
Prep only. Moved text from issue conversation to an include for WN .NET 10 Prev 2
1 parent 7b2039a commit 74dc6cb

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
### Populate XML doc comments into OpenAPI document
2+
3+
ASP.NET Core OpenAPI document generation wlll now include metadata from XML doc comments on on method, class, and member definitions in the OpenAPI document. You must enable XML doc comments in your project file to use this feature. You can do this by adding the following property to your project file:
4+
5+
```xml
6+
<PropertyGroup>
7+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
8+
</PropertyGroup>
9+
```
10+
11+
At build-time, the OpenAPI package will leverage a source generator to discover XML comments in the current application assembly and any project references and emit source code to insert them into the document via an OpenAPI document transformer.
12+
13+
Note that the C# build process does not capture XML doc comments placed on lamda expresions, so to use XML doc comments to add metadata to a minimal API endpoint, you must define the endpoint handler as a method, put the XML doc comments on the method, and then reference that method from the `MapXXX` method. For example, to use XML doc comments to add metadata to a minimal API endpoint originally defined as a lambda expression:
14+
15+
```csharp
16+
app.MapGet("/hello", (string name) =>$"Hello, {name}!");
17+
```
18+
19+
change the `MapGet` call to reference a method
20+
21+
```csharp
22+
app.MapGet("/hello", Hello);
23+
```
24+
25+
Then define the `Hello` method with XML doc comments:
26+
27+
```csharp
28+
static partial class Program
29+
{
30+
/// <summary>
31+
/// Sends a greeting.
32+
/// </summary>
33+
/// <remarks>
34+
/// Greeting a person by their name.
35+
/// </remarks>
36+
/// <param name="name">The name of the person to greet.</param>
37+
/// <returns>A greeting.</returns>
38+
public static string Hello(string name)
39+
{
40+
return $"Hello, {name}!";
41+
}
42+
}
43+
```
44+
45+
Here the `Hello` method is added to the `Program` class, but you can add it to any class in your project.
46+
47+
The example above illustrates the `<summary>`, `<remarks>`, and `<param>` XML doc comments.
48+
For more information about XML doc comments, including all the supported tags, see the [C# documentation](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/xmldoc/recommended-tags).
49+
50+
Since the core functionality is provided via a source generator, it can be disabled by adding the following MSBuild to your project file.
51+
52+
```
53+
<ItemGroup>
54+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0-preview.2.*" GeneratePathProperty="true" />
55+
</ItemGroup>
56+
57+
<Target Name="DisableCompileTimeOpenApiXmlGenerator" BeforeTargets="CoreCompile">
58+
<ItemGroup>
59+
<Analyzer Remove="$(PkgMicrosoft_AspNetCore_OpenApi)/analyzers/dotnet/cs/Microsoft.AspNetCore.OpenApi.SourceGenerators.dll" />
60+
</ItemGroup>
61+
</Target>
62+
```
63+
64+
The source generator process XML files included in the `AdditionalFiles` property. To add (or remove), sources modify the property as follows:
65+
66+
```
67+
<Target Name="AddXmlSources" BeforeTargets="CoreCompile">
68+
<ItemGroup>
69+
<AdditionalFiles Include="$(PkgSome_Package/lib/net10.0/Some.Package.xml" />
70+
</ItemGroup>
71+
</Target>
72+
```

0 commit comments

Comments
 (0)