· 5 years ago · Jan 07, 2021, 10:52 PM
1using System.Web.Http;
2using WebActivatorEx;
3using Solutoria.Api;
4using Swashbuckle.Application;
5using System.Web;
6using PreApplicationStartMethodAttribute = WebActivatorEx.PreApplicationStartMethodAttribute;
7using Swashbuckle.Examples;
8
9[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
10
11namespace Solutoria.Api
12{
13 /// <summary>
14 ///
15 /// </summary>
16 public class SwaggerConfig
17 {
18 /// <summary>
19 ///
20 /// </summary>
21 public static void Register()
22 {
23 var thisAssembly = typeof(SwaggerConfig).Assembly;
24
25 GlobalConfiguration.Configuration
26 .EnableSwagger(c =>
27 {
28 // By default, the service root url is inferred from the request used to access the docs.
29 // However, there may be situations (e.g. proxy and load-balanced environments) where this does not
30 // resolve correctly. You can workaround this by providing your own code to determine the root URL.
31 //
32 //c.RootUrl(req => GetRootUrlFromAppConfig());
33
34 // If schemes are not explicitly provided in a Swagger 2.0 document, then the scheme used to access
35 // the docs is taken as the default. If your API supports multiple schemes and you want to be explicit
36 // about them, you can use the "Schemes" option as shown below.
37 //
38 //c.Schemes(new[] { "http", "https" });
39
40 // Use "SingleApiVersion" to describe a single version API. Swagger 2.0 includes an "Info" object to
41 // hold additional metadata for an API. Version and title are required but you can also provide
42 // additional fields by chaining methods off SingleApiVersion.
43 //
44 c.SingleApiVersion("v1", "Solutoria API Endpoint");
45 /*.Description("A sample API for testing and prototyping Swashbuckle features")
46
47 .TermsOfService("Some terms")
48 .Contact(cc => cc
49 .Name("Some contact")
50 .Url("http://tempuri.org/contact")
51 .Email("some.contact@tempuri.org"))
52 .License(lc => lc
53 .Name("Some License")
54 .Url("http://tempuri.org/license"));
55 */
56 // If you want the output Swagger docs to be indented properly, enable the "PrettyPrint" option.
57 //
58 //c.PrettyPrint();
59
60 // If your API has multiple versions, use "MultipleApiVersions" instead of "SingleApiVersion".
61 // In this case, you must provide a lambda that tells Swashbuckle which actions should be
62 // included in the docs for a given API version. Like "SingleApiVersion", each call to "Version"
63 // returns an "Info" builder so you can provide additional metadata per API version.
64 //
65 //c.MultipleApiVersions(
66 // (apiDesc, targetApiVersion) => ResolveVersionSupportByRouteConstraint(apiDesc, targetApiVersion),
67 // (vc) =>
68 // {
69 // vc.Version("v2", "Swashbuckle Dummy API V2");
70 // vc.Version("v1", "Swashbuckle Dummy API V1");
71 // });
72
73 // You can use "BasicAuth", "ApiKey" or "OAuth2" options to describe security schemes for the API.
74 // See https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md for more details.
75 // NOTE: These only define the schemes and need to be coupled with a corresponding "security" property
76 // at the document or operation level to indicate which schemes are required for an operation. To do this,
77 // you'll need to implement a custom IDocumentFilter and/or IOperationFilter to set these properties
78 // according to your specific authorization implementation
79 //
80 //c.BasicAuth("basic")
81 // .Description("Basic HTTP Authentication");
82 //
83 // NOTE: You must also configure 'EnableApiKeySupport' below in the SwaggerUI section
84 //c.ApiKey("apiKey")
85 // .Description("API Key Authentication")
86 // .Name("apiKey")
87 // .In("header");
88 //
89 //c.OAuth2("oauth2")
90 // .Description("OAuth2 Implicit Grant")
91 // .Flow("implicit")
92 // .AuthorizationUrl("http://petstore.swagger.wordnik.com/api/oauth/dialog")
93 // //.TokenUrl("https://tempuri.org/token")
94 // .Scopes(scopes =>
95 // {
96 // scopes.Add("read", "Read access to protected resources");
97 // scopes.Add("write", "Write access to protected resources");
98 // });
99
100 // Set this flag to omit descriptions for any actions decorated with the Obsolete attribute
101 //c.IgnoreObsoleteActions();
102
103 // Each operation be assigned one or more tags which are then used by consumers for various reasons.
104 // For example, the swagger-ui groups operations according to the first tag of each operation.
105 // By default, this will be controller name but you can use the "GroupActionsBy" option to
106 // override with any value.
107 //
108 //c.GroupActionsBy(apiDesc => apiDesc.HttpMethod.ToString());
109
110 // You can also specify a custom sort order for groups (as defined by "GroupActionsBy") to dictate
111 // the order in which operations are listed. For example, if the default grouping is in place
112 // (controller name) and you specify a descending alphabetic sort order, then actions from a
113 // ProductsController will be listed before those from a CustomersController. This is typically
114 // used to customize the order of groupings in the swagger-ui.
115 //
116 //c.OrderActionGroupsBy(new DescendingAlphabeticComparer());
117
118 // If you annotate Controllers and API Types with
119 // Xml comments (http://msdn.microsoft.com/en-us/library/b2s063f7(v=vs.110).aspx), you can incorporate
120 // those comments into the generated docs and UI. You can enable this by providing the path to one or
121 // more Xml comment files.
122 //
123 //c.IncludeXmlComments(GetXmlCommentsPath());
124 c.IncludeXmlComments(string.Format(@"{0}\App_Data\Documentation.xml", System.AppDomain.CurrentDomain.BaseDirectory));
125 c.IncludeXmlComments(string.Format(@"{0}\App_Data\DocumentationApp.xml", System.AppDomain.CurrentDomain.BaseDirectory));
126
127 // Swashbuckle makes a best attempt at generating Swagger compliant JSON schemas for the various types
128 // exposed in your API. However, there may be occasions when more control of the output is needed.
129 // This is supported through the "MapType" and "SchemaFilter" options:
130 //
131 // Use the "MapType" option to override the Schema generation for a specific type.
132 // It should be noted that the resulting Schema will be placed "inline" for any applicable Operations.
133 // While Swagger 2.0 supports inline definitions for "all" Schema types, the swagger-ui tool does not.
134 // It expects "complex" Schemas to be defined separately and referenced. For this reason, you should only
135 // use the "MapType" option when the resulting Schema is a primitive or array type. If you need to alter a
136 // complex Schema, use a Schema filter.
137 //
138 //c.MapType<ProductType>(() => new Schema { type = "integer", format = "int32" });
139
140 // If you want to post-modify "complex" Schemas once they've been generated, across the board or for a
141 // specific type, you can wire up one or more Schema filters.
142 //
143 //c.SchemaFilter<ApplySchemaVendorExtensions>();
144
145 // In a Swagger 2.0 document, complex types are typically declared globally and referenced by unique
146 // Schema Id. By default, Swashbuckle does NOT use the full type name in Schema Ids. In most cases, this
147 // works well because it prevents the "implementation detail" of type namespaces from leaking into your
148 // Swagger docs and UI. However, if you have multiple types in your API with the same class name, you'll
149 // need to opt out of this behavior to avoid Schema Id conflicts.
150 //
151 //c.UseFullTypeNameInSchemaIds();
152
153 // Alternatively, you can provide your own custom strategy for inferring SchemaId's for
154 // describing "complex" types in your API.
155 //
156 //c.SchemaId(t => t.FullName.Contains('`') ? t.FullName.Substring(0, t.FullName.IndexOf('`')) : t.FullName);
157
158 // Set this flag to omit schema property descriptions for any type properties decorated with the
159 // Obsolete attribute
160 //c.IgnoreObsoleteProperties();
161
162 // In accordance with the built in JsonSerializer, Swashbuckle will, by default, describe enums as integers.
163 // You can change the serializer behavior by configuring the StringToEnumConverter globally or for a given
164 // enum type. Swashbuckle will honor this change out-of-the-box. However, if you use a different
165 // approach to serialize enums as strings, you can also force Swashbuckle to describe them as strings.
166 //
167 //c.DescribeAllEnumsAsStrings();
168 c.OperationFilter<ExamplesOperationFilter>();
169 // Similar to Schema filters, Swashbuckle also supports Operation and Document filters:
170 //
171 // Post-modify Operation descriptions once they've been generated by wiring up one or more
172 // Operation filters.
173 //
174 //c.OperationFilter<AddDefaultResponse>();
175 //
176 // If you've defined an OAuth2 flow as described above, you could use a custom filter
177 // to inspect some attribute on each action and infer which (if any) OAuth2 scopes are required
178 // to execute the operation
179 //
180 //c.OperationFilter<AssignOAuth2SecurityRequirements>();
181
182 // Post-modify the entire Swagger document by wiring up one or more Document filters.
183 // This gives full control to modify the final SwaggerDocument. You should have a good understanding of
184 // the Swagger 2.0 spec. - https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md
185 // before using this option.
186 //
187 //c.DocumentFilter<ApplyDocumentVendorExtensions>();
188
189 // In contrast to WebApi, Swagger 2.0 does not include the query string component when mapping a URL
190 // to an action. As a result, Swashbuckle will raise an exception if it encounters multiple actions
191 // with the same path (sans query string) and HTTP method. You can workaround this by providing a
192 // custom strategy to pick a winner or merge the descriptions for the purposes of the Swagger docs
193 //
194 //c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
195
196 // Wrap the default SwaggerGenerator with additional behavior (e.g. caching) or provide an
197 // alternative implementation for ISwaggerProvider with the CustomProvider option.
198 //
199 //c.CustomProvider((defaultProvider) => new CachingSwaggerProvider(defaultProvider));
200 })
201 .EnableSwaggerUi(c =>
202 {
203 // Use the "DocumentTitle" option to change the Document title.
204 // Very helpful when you have multiple Swagger pages open, to tell them apart.
205 //
206 c.DocumentTitle("Solutoria API");
207
208 // Use the "InjectStylesheet" option to enrich the UI with one or more additional CSS stylesheets.
209 // The file must be included in your project as an "Embedded Resource", and then the resource's
210 // "Logical Name" is passed to the method as shown below.
211 //
212 //c.InjectStylesheet(thisAssembly, "Solutoria.ApiCoreSoluto.2.DistributedServices.Solutoria.Api.Content.theme-material.css");
213 //c.CustomAsset("index", thisAssembly, "Solutoria.ApiCoreSoluto.2.DistributedServices.Solutoria.Api.Content.index.html");
214
215 // Use the "InjectJavaScript" option to invoke one or more custom JavaScripts after the swagger-ui
216 // has loaded. The file must be included in your project as an "Embedded Resource", and then the resource's
217 // "Logical Name" is passed to the method as shown above.
218 //
219 //c.InjectJavaScript(thisAssembly, "Swashbuckle.Dummy.SwaggerExtensions.testScript1.js");
220
221 // The swagger-ui renders boolean data types as a dropdown. By default, it provides "true" and "false"
222 // strings as the possible choices. You can use this option to change these to something else,
223 // for example 0 and 1.
224 //
225 //c.BooleanValues(new[] { "0", "1" });
226
227 // By default, swagger-ui will validate specs against swagger.io's online validator and display the result
228 // in a badge at the bottom of the page. Use these options to set a different validator URL or to disable the
229 // feature entirely.
230 //c.SetValidatorUrl("http://localhost/validator");
231 //c.DisableValidator();
232
233 // Use this option to control how the Operation listing is displayed.
234 // It can be set to "None" (default), "List" (shows operations for each resource),
235 // or "Full" (fully expanded: shows operations and their details).
236 //
237 //c.DocExpansion(DocExpansion.List);
238
239 // Specify which HTTP operations will have the 'Try it out!' option. An empty paramter list disables
240 // it for all operations.
241 //
242 //c.SupportedSubmitMethods("GET", "HEAD");
243
244 // Use the CustomAsset option to provide your own version of assets used in the swagger-ui.
245 // It's typically used to instruct Swashbuckle to return your version instead of the default
246 // when a request is made for "index.html". As with all custom content, the file must be included
247 // in your project as an "Embedded Resource", and then the resource's "Logical Name" is passed to
248 // the method as shown below.
249 //
250 //c.CustomAsset("index", containingAssembly, "YourWebApiProject.SwaggerExtensions.index.html");
251
252 // If your API has multiple versions and you've applied the MultipleApiVersions setting
253 // as described above, you can also enable a select box in the swagger-ui, that displays
254 // a discovery URL for each version. This provides a convenient way for users to browse documentation
255 // for different API versions.
256 //
257 //c.EnableDiscoveryUrlSelector();
258
259 // If your API supports the OAuth2 Implicit flow, and you've described it correctly, according to
260 // the Swagger 2.0 specification, you can enable UI support as shown below.
261 //
262 //c.EnableOAuth2Support(
263 // clientId: "test-client-id",
264 // clientSecret: null,
265 // realm: "test-realm",
266 // appName: "Swagger UI"
267 // //additionalQueryStringParams: new Dictionary<string, string>() { { "foo", "bar" } }
268 //);
269
270 // If your API supports ApiKey, you can override the default values.
271 // "apiKeyIn" can either be "query" or "header"
272 //
273 c.EnableApiKeySupport("Authorization", "header");
274
275 //c.CustomAsset("index", thisAssembly, "Solutoria.Api.Content.index.html");
276 //c.InjectStylesheet(thisAssembly, "Solutoria.Api.Content.material-theme.css");
277
278 /*
279 c.AddSecurityDefinition("Bearer", new ApiKeyScheme()
280 {
281 Description = "JWT Authorization header {token}",
282 Name = "Authorization",
283 In = "header",
284 Type = "apiKey"
285 });
286 */
287
288 });
289 }
290 }
291}
292