You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
17 KiB

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