-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathNSwagServiceCollectionExtensions.cs
134 lines (117 loc) · 6.93 KB
/
NSwagServiceCollectionExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//-----------------------------------------------------------------------
// <copyright file="NSwagServiceCollectionExtensions.cs" company="NSwag">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/RicoSuter/NSwag/blob/master/LICENSE.md</license>
// <author>Rico Suter, [email protected]</author>
//-----------------------------------------------------------------------
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.ApiDescriptions;
using Microsoft.Extensions.Options;
using NJsonSchema;
using NJsonSchema.Generation;
using NJsonSchema.NewtonsoftJson.Generation;
using NSwag.AspNetCore;
using NSwag.Generation;
using NSwag.Generation.AspNetCore;
using NSwag.Generation.Processors;
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>NSwag extensions for <see cref="IServiceCollection"/>.</summary>
public static class NSwagServiceCollectionExtensions
{
/// <summary>Adds services required for OpenAPI 3.0 generation (change document settings to generate Swagger 2.0).</summary>
/// <param name="serviceCollection">The <see cref="IServiceCollection"/>.</param>
/// <param name="configure">Configure the document.</param>
public static IServiceCollection AddOpenApiDocument(this IServiceCollection serviceCollection, Action<AspNetCoreOpenApiDocumentGeneratorSettings> configure)
{
return AddOpenApiDocument(serviceCollection, (settings, services) =>
{
configure?.Invoke(settings);
});
}
/// <summary>Adds services required for OpenAPI 3.0 generation (change document settings to generate Swagger 2.0).</summary>
/// <param name="serviceCollection">The <see cref="IServiceCollection"/>.</param>
/// <param name="configure">Configure the document.</param>
public static IServiceCollection AddOpenApiDocument(this IServiceCollection serviceCollection, Action<AspNetCoreOpenApiDocumentGeneratorSettings, IServiceProvider> configure = null)
{
return AddSwaggerDocument(serviceCollection, (settings, services) =>
{
settings.SchemaSettings.SchemaType = SchemaType.OpenApi3;
configure?.Invoke(settings, services);
});
}
/// <summary>Adds services required for Swagger 2.0 generation (change document settings to generate OpenAPI 3.0).</summary>
/// <param name="serviceCollection">The <see cref="IServiceCollection"/>.</param>
/// <param name="configure">Configure the document.</param>
public static IServiceCollection AddSwaggerDocument(this IServiceCollection serviceCollection, Action<AspNetCoreOpenApiDocumentGeneratorSettings> configure)
{
return AddSwaggerDocument(serviceCollection, (settings, services) =>
{
configure?.Invoke(settings);
});
}
/// <summary>Adds services required for Swagger 2.0 generation (change document settings to generate OpenAPI 3.0).</summary>
/// <param name="serviceCollection">The <see cref="IServiceCollection"/>.</param>
/// <param name="configure">Configure the document.</param>
public static IServiceCollection AddSwaggerDocument(this IServiceCollection serviceCollection, Action<AspNetCoreOpenApiDocumentGeneratorSettings, IServiceProvider> configure = null)
{
serviceCollection.AddSingleton(services =>
{
var settings = new AspNetCoreOpenApiDocumentGeneratorSettings();
var mvcOptions = services.GetRequiredService<IOptions<MvcOptions>>();
var hasSystemTextJsonOutputFormatter = mvcOptions.Value.OutputFormatters
.Any(f => f.GetType().Name == "SystemTextJsonOutputFormatter");
var newtonsoftSettings = AspNetCoreOpenApiDocumentGenerator.GetJsonSerializerSettings(services);
var systemTextJsonOptions = hasSystemTextJsonOutputFormatter
? AspNetCoreOpenApiDocumentGenerator.GetSystemTextJsonSettings(services)
#if NET6_0_OR_GREATER
: services.GetService<IOptions<AspNetCore.Http.Json.JsonOptions>>()?.Value.SerializerOptions;
#else
: null;
#endif
if (newtonsoftSettings != null && !hasSystemTextJsonOutputFormatter)
{
settings.ApplySettings(new NewtonsoftJsonSchemaGeneratorSettings { SerializerSettings = newtonsoftSettings }, mvcOptions.Value);
}
else if (systemTextJsonOptions != null)
{
settings.ApplySettings(new SystemTextJsonSchemaGeneratorSettings { SerializerOptions = systemTextJsonOptions }, mvcOptions.Value);
}
else
{
settings.ApplySettings(new SystemTextJsonSchemaGeneratorSettings(), mvcOptions.Value);
}
settings.SchemaSettings.SchemaType = SchemaType.Swagger2;
configure?.Invoke(settings, services);
foreach (var documentProcessor in services.GetRequiredService<IEnumerable<IDocumentProcessor>>())
{
settings.DocumentProcessors.Add(documentProcessor);
}
foreach (var operationProcessor in services.GetRequiredService<IEnumerable<IOperationProcessor>>())
{
settings.OperationProcessors.Add(operationProcessor);
}
return new OpenApiDocumentRegistration(settings.DocumentName, settings);
});
var descriptor = serviceCollection.SingleOrDefault(d => d.ServiceType == typeof(OpenApiDocumentProvider));
if (descriptor == null)
{
serviceCollection.AddSingleton<OpenApiDocumentProvider>();
serviceCollection.AddSingleton<IConfigureOptions<MvcOptions>, OpenApiConfigureMvcOptions>();
serviceCollection.AddSingleton<IOpenApiDocumentGenerator>(s => s.GetRequiredService<OpenApiDocumentProvider>());
// Used by the <c>dotnet-getdocument</c> tool from the Microsoft.Extensions.ApiDescription.Server package.
serviceCollection.AddSingleton<IDocumentProvider>(s => s.GetRequiredService<OpenApiDocumentProvider>());
}
return serviceCollection;
}
/// <summary>Adds services required for Swagger 2.0 generation (change document settings to generate OpenAPI 3.0).</summary>
/// <param name="serviceCollection">The <see cref="IServiceCollection"/>.</param>
/// <param name="configure">Configure the document.</param>
[Obsolete("Use " + nameof(AddSwaggerDocument) + "() instead.")]
public static IServiceCollection AddSwagger(this IServiceCollection serviceCollection, Action<AspNetCoreOpenApiDocumentGeneratorSettings> configure = null)
{
return AddSwaggerDocument(serviceCollection, configure);
}
}
}