Skip to content

Commit 97c6f0d

Browse files
Add examples to AddOpenApi extension method/overloads (#58808)
* Add examples to AddOpenApi extension method/overloads * Commit suggestions from PR review Co-authored-by: Safia Abdalla <[email protected]>
1 parent 5f935fe commit 97c6f0d

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

Diff for: src/OpenApi/src/Extensions/OpenApiServiceCollectionExtensions.cs

+39
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using Microsoft.AspNetCore.Builder;
45
using Microsoft.AspNetCore.Http.Json;
56
using Microsoft.AspNetCore.OpenApi;
67
using Microsoft.Extensions.ApiDescriptions;
@@ -19,6 +20,14 @@ public static class OpenApiServiceCollectionExtensions
1920
/// </summary>
2021
/// <param name="services">The <see cref="IServiceCollection"/> to register services onto.</param>
2122
/// <param name="documentName">The name of the OpenAPI document associated with registered services.</param>
23+
/// <example>
24+
/// This method is commonly used to add OpenAPI services to the <see cref="WebApplicationBuilder.Services"/>
25+
/// of a <see cref="WebApplicationBuilder"/>, as shown in the following example:
26+
/// <code>
27+
/// var builder = WebApplication.CreateBuilder(args);
28+
/// builder.Services.AddOpenApi("MyWebApi");
29+
/// </code>
30+
/// </example>
2231
public static IServiceCollection AddOpenApi(this IServiceCollection services, string documentName)
2332
{
2433
ArgumentNullException.ThrowIfNull(services);
@@ -32,6 +41,17 @@ public static IServiceCollection AddOpenApi(this IServiceCollection services, st
3241
/// <param name="services">The <see cref="IServiceCollection"/> to register services onto.</param>
3342
/// <param name="documentName">The name of the OpenAPI document associated with registered services.</param>
3443
/// <param name="configureOptions">A delegate used to configure the target <see cref="OpenApiOptions"/>.</param>
44+
/// <example>
45+
/// This method is commonly used to add OpenAPI services to the <see cref="WebApplicationBuilder.Services"/>
46+
/// of a <see cref="WebApplicationBuilder"/>, as shown in the following example:
47+
/// <code>
48+
/// var builder = WebApplication.CreateBuilder(args);
49+
/// builder.Services.AddOpenApi("MyWebApi", options => {
50+
/// // Add a custom schema transformer for decimal types
51+
/// options.AddSchemaTransformer(DecimalTransformer.TransformAsync);
52+
/// });
53+
/// </code>
54+
/// </example>
3555
public static IServiceCollection AddOpenApi(this IServiceCollection services, string documentName, Action<OpenApiOptions> configureOptions)
3656
{
3757
ArgumentNullException.ThrowIfNull(services);
@@ -51,13 +71,32 @@ public static IServiceCollection AddOpenApi(this IServiceCollection services, st
5171
/// </summary>
5272
/// <param name="services">The <see cref="IServiceCollection"/> to register services onto.</param>
5373
/// <param name="configureOptions">A delegate used to configure the target <see cref="OpenApiOptions"/>.</param>
74+
/// <example>
75+
/// This method is commonly used to add OpenAPI services to the <see cref="WebApplicationBuilder.Services"/>
76+
/// of a <see cref="WebApplicationBuilder"/>, as shown in the following example:
77+
/// <code>
78+
/// var builder = WebApplication.CreateBuilder(args);
79+
/// builder.Services.AddOpenApi(options => {
80+
/// // Add a custom schema transformer for decimal types
81+
/// options.AddSchemaTransformer(DecimalTransformer.TransformAsync);
82+
/// });
83+
/// </code>
84+
/// </example>
5485
public static IServiceCollection AddOpenApi(this IServiceCollection services, Action<OpenApiOptions> configureOptions)
5586
=> services.AddOpenApi(OpenApiConstants.DefaultDocumentName, configureOptions);
5687

5788
/// <summary>
5889
/// Adds OpenAPI services related to the default document to the specified <see cref="IServiceCollection"/>.
5990
/// </summary>
6091
/// <param name="services">The <see cref="IServiceCollection"/> to register services onto.</param>
92+
/// <example>
93+
/// This method is commonly used to add OpenAPI services to the <see cref="WebApplicationBuilder.Services"/>
94+
/// of a <see cref="WebApplicationBuilder"/>, as shown in the following example:
95+
/// <code>
96+
/// var builder = WebApplication.CreateBuilder(args);
97+
/// builder.Services.AddOpenApi();
98+
/// </code>
99+
/// </example>
61100
public static IServiceCollection AddOpenApi(this IServiceCollection services)
62101
=> services.AddOpenApi(OpenApiConstants.DefaultDocumentName);
63102

Diff for: src/OpenApi/src/Microsoft.AspNetCore.OpenApi.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<Reference Include="Microsoft.AspNetCore.Routing" />
2121
<Reference Include="Microsoft.AspNetCore.Mvc.Core" />
2222
<Reference Include="Microsoft.AspNetCore.Mvc.ApiExplorer" />
23+
<Reference Include="Microsoft.AspNetCore" />
2324
</ItemGroup>
2425

2526
<ItemGroup>

0 commit comments

Comments
 (0)