-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathIServiceCollectionExtensions.cs
More file actions
262 lines (230 loc) · 8.74 KB
/
IServiceCollectionExtensions.cs
File metadata and controls
262 lines (230 loc) · 8.74 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
using System.Reflection;
using Microsoft.OpenApi;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace DocumentationSnippets;
public static class IServiceCollectionExtensions
{
public static void Configure(this IServiceCollection services)
{
// begin-snippet: README-Newtonsoft.Json
services.AddMvc();
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
});
services.AddSwaggerGenNewtonsoftSupport();
// end-snippet
// begin-snippet: README-MvcCore
services.AddMvcCore()
.AddApiExplorer();
// end-snippet
// begin-snippet: Annotations-Enable
services.AddSwaggerGen(options =>
{
// Other setup, then...
options.EnableAnnotations();
});
// end-snippet
// begin-snippet: Annotations-EnablePolymorphism
services.AddSwaggerGen(options =>
{
options.EnableAnnotations(enableAnnotationsForInheritance: true, enableAnnotationsForPolymorphism: true);
});
// end-snippet
// begin-snippet: Swagger-CustomSerializerServices
services.ConfigureSwagger(options =>
{
options.SetCustomDocumentSerializer<CustomDocumentSerializer>();
});
// end-snippet
// begin-snippet: SwaggerGen-CustomNamingStrategyConfiguration
services.AddSwaggerGen(options =>
{
// Other configuration...
// Use method name as operationId
options.CustomOperationIds(apiDescription =>
{
return apiDescription.TryGetMethodInfo(out MethodInfo methodInfo) ? methodInfo.Name : null;
});
});
// end-snippet
// begin-snippet: SwaggerGen-ConfigureXmlDocumentation
services.AddSwaggerGen(options =>
{
options.SwaggerDoc(
"v1",
new OpenApiInfo
{
Title = "My API - V1",
Version = "v1"
}
);
options.IncludeXmlComments(Assembly.GetExecutingAssembly());
// or options.IncludeXmlComments(typeof(MyController).Assembly));
});
// end-snippet
// begin-snippet: SwaggerGen-GlobalMetadata
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1",
new OpenApiInfo
{
Title = "My API - V1",
Version = "v1",
Description = "A sample API to demo Swashbuckle",
TermsOfService = new Uri("http://tempuri.org/terms"),
Contact = new OpenApiContact
{
Name = "Joe Developer",
Email = "joe.developer@tempuri.org"
},
License = new OpenApiLicense
{
Name = "Apache 2.0",
Url = new Uri("https://www.apache.org/licenses/LICENSE-2.0.html")
}
}
);
});
// end-snippet
// begin-snippet: SwaggerGen-MultipleDocuments
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo { Title = "My API - V1", Version = "v1" });
options.SwaggerDoc("v2", new OpenApiInfo { Title = "My API - V2", Version = "v2" });
});
// end-snippet
// begin-snippet: SwaggerGen-ConfigureControllerModelConvention
services.AddMvc(options =>
options.Conventions.Add(new ApiExplorerGroupPerVersionConvention())
);
// end-snippet
// begin-snippet: SwaggerGen-IgnoreObsoleteActions
services.AddSwaggerGen(options =>
{
options.IgnoreObsoleteActions();
});
// end-snippet
// begin-snippet: SwaggerGen-IgnoreObsoleteProperties
services.AddSwaggerGen(options =>
{
options.IgnoreObsoleteProperties();
});
// end-snippet
// begin-snippet: SwaggerGen-HiddenByConventionConfiguration
services.AddMvc(options =>
options.Conventions.Add(new ApiExplorerGetsOnlyConvention())
);
// end-snippet
// begin-snippet: SwaggerGen-CustomTags
services.AddSwaggerGen(options =>
{
options.TagActionsBy(api => [api.HttpMethod]);
});
// end-snippet
// begin-snippet: SwaggerGen-CustomSorting
services.AddSwaggerGen(options =>
{
options.OrderActionsBy((apiDesc) => $"{apiDesc.ActionDescriptor.RouteValues["controller"]}_{apiDesc.HttpMethod}");
});
// end-snippet
// begin-snippet: SwaggerGen-CustomSchemaIds
services.AddSwaggerGen(options =>
{
options.CustomSchemaIds((type) => type.FullName);
});
// end-snippet
// begin-snippet: SwaggerGen-CustomSchemaMapping
services.AddSwaggerGen(options =>
{
options.MapType<PhoneNumber>(() => new OpenApiSchema { Type = JsonSchemaType.String });
});
// end-snippet
// begin-snippet: SwaggerGen-ConfigureOperationFilter
services.AddSwaggerGen(options =>
{
options.OperationFilter<AuthResponsesOperationFilter>();
});
// end-snippet
// begin-snippet: SwaggerGen-ConfigureSchemaFilter
services.AddSwaggerGen(options =>
{
options.SchemaFilter<AutoRestSchemaFilter>();
});
// end-snippet
// begin-snippet: SwaggerGen-ConfigureSchemaFilterForEnumDictionaryEnum
services.AddSwaggerGen(options =>
{
// These will be replaced by DictionaryTKeyEnumTValueSchemaFilter, but are needed to avoid
// an error. You will need one for every kind of Dictionary<,> you have.
options.MapType<Dictionary<MyEnum, List<string>>>(() => new OpenApiSchema());
options.SchemaFilter<DictionaryTKeyEnumTValueSchemaFilter>();
});
// end-snippet
// begin-snippet: SwaggerGen-AddSecurityDefinition
services.AddSwaggerGen(options =>
{
// Define the OAuth2.0 scheme that's in use (i.e. Implicit Flow)
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows
{
Implicit = new OpenApiOAuthFlow
{
AuthorizationUrl = new Uri("/auth-server/connect/authorize", UriKind.Relative),
Scopes = new Dictionary<string, string>
{
["readAccess"] = "Access read operations",
["writeAccess"] = "Access write operations"
}
}
}
});
});
// end-snippet
// begin-snippet: SwaggerGen-AddSecurityRequirement
services.AddSwaggerGen(options =>
{
options.AddSecurityRequirement((document) => new OpenApiSecurityRequirement()
{
[new OpenApiSecuritySchemeReference("oauth2", document)] = ["readAccess", "writeAccess"]
});
});
// end-snippet
// begin-snippet: SwaggerGen-BearerAuthentication
services.AddSwaggerGen(options =>
{
options.AddSecurityDefinition("bearer", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.Http,
Scheme = "bearer",
BearerFormat = "JWT",
Description = "JWT Authorization header using the Bearer scheme."
});
options.AddSecurityRequirement(document => new OpenApiSecurityRequirement
{
[new OpenApiSecuritySchemeReference("bearer", document)] = []
});
});
// end-snippet
// begin-snippet: SwaggerGen-DetectSubtypes
services.AddSwaggerGen(options =>
{
options.UseAllOfForInheritance();
options.SelectSubTypesUsing(baseType =>
{
return typeof(Program).Assembly.GetTypes().Where(type => type.IsSubclassOf(baseType));
});
});
// end-snippet
// begin-snippet: SwaggerGen-UseAllOfForInheritance
services.AddSwaggerGen(options =>
{
options.UseAllOfForInheritance();
options.SelectDiscriminatorNameUsing((baseType) => "TypeName");
options.SelectDiscriminatorValueUsing((subType) => subType.Name);
});
// end-snippet
}
}