/// <summary> /// Startup constructor for RunLog API. /// </summary> /// <param name="configuration">IConfiguration</param> public Startup(IConfiguration configuration) { Configuration = configuration; // Get settings from config file _swaggerConfig = Configuration.GetSection <SwaggerConfig>(); }
/// <summary> /// Use swagger in application /// </summary> /// <param name="app"></param> /// <param name="config"></param> /// <param name="info"></param> public static void UseSwagger(this IApplicationBuilder app, ISwaggerConfig config, Info info) { if (info == null) { throw new ArgumentNullException(nameof(info)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } // Enable swagger and swagger ui app.UseSwagger(options => { options.PreSerializeFilters.Add((doc, request) => { if (request.Headers.TryGetValue(HttpHeader.Location, out var values) && values.Count > 0) { doc.BasePath = "/" + values[0]; } doc.Schemes = new List <string> { "https" }; if (config.WithHttpScheme) { doc.Schemes.Add("http"); } }); options.RouteTemplate = "{documentName}/swagger.json"; }); if (!config.UIEnabled) { return; } app.UseSwaggerUI(options => { if (config.WithAuth) { options.OAuthAppName(info.Title); options.OAuthClientId(config.SwaggerAppId); if (!string.IsNullOrEmpty(config.SwaggerAppSecret)) { options.OAuthClientSecret(config.SwaggerAppSecret); } var resource = config as IAuthConfig; if (!string.IsNullOrEmpty(resource?.Audience)) { options.OAuthAdditionalQueryStringParams( new Dictionary <string, string> { ["resource"] = resource.Audience }); } } options.RoutePrefix = ""; options.SwaggerEndpoint($"{info.Version}/swagger.json", info.Version); }); }
/// <summary> /// Configure swagger /// </summary> /// <param name="services"></param> /// <param name="config"></param> /// <param name="info"></param> public static void AddSwagger(this IServiceCollection services, ISwaggerConfig config, Info info) { if (info == null) { throw new ArgumentNullException(nameof(info)); } if (config == null) { throw new ArgumentNullException(nameof(config)); } // Generate swagger documentation services.AddSwaggerGen(options => { // Generate doc for version options.SwaggerDoc(info.Version, info); // Add annotations options.EnableAnnotations(); // Add help options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, config.GetType().Assembly.GetName().Name + ".xml"), true); // If auth enabled, need to have bearer token to access any api if (config.WithAuth) { var resource = config as IClientConfig; if (string.IsNullOrEmpty(config.SwaggerAppId) || resource == null) { options.AddSecurityDefinition("bearer", new ApiKeyScheme { Description = "Authorization token in the form of 'bearer <token>'", Name = "Authorization", In = "header" }); } else { options.AddSecurityDefinition("oauth2", new OAuth2Scheme { Type = "oauth2", Flow = "implicit", AuthorizationUrl = resource.GetAuthorityUrl() + "/oauth2/authorize", Scopes = services.GetRequiredScopes() .ToDictionary(k => k, k => $"Access {k} operations"), TokenUrl = resource.GetAuthorityUrl() + "/oauth2/token" }); options.OperationFilter <SecurityRequirementsOperationFilter>(); } } }); }
public static void Register(HttpConfiguration httpConfig, ISwaggerConfig config) { var thisAssembly = typeof(SwaggerInstall).Assembly; Config = config; httpConfig .EnableSwagger(c => { c.SingleApiVersion("v1", string.Empty); c.UseFullTypeNameInSchemaIds(); GetXmlCommentsPath().ForEach(c.IncludeXmlComments); }) .EnableSwaggerUi(c => { c.InjectStylesheet(thisAssembly, "InfoScreen.WebApi.Content.swagger.custom-swagger.css"); c.CustomAsset("index", thisAssembly, "InfoScreen.WebApi.Content.swagger.index.html"); c.DisableValidator(); }); }
/// <summary> /// 初始化 ISwaggerConfig /// </summary> /// <param name="swaggerConfig"></param> public static void InitSwaggerConfig(ISwaggerConfig swaggerConfig) { SwaggerConfig = swaggerConfig; }
/// <summary> /// Init Config /// AddSwaggerGen /// Add HiddenApiAttribute And HiddenSwaggerFilter /// </summary> /// <param name="services"></param> /// <param name="title"></param> /// <param name="configuration"></param> /// <param name="swaggerConfig"></param> /// <param name="apiVersion"></param> /// <param name="swaggerGenOptionsAction"></param> /// <remarks> /// add AddSwaggerGen with DocumentFilter /// 1、 <see cref="System.Attributes.HiddenApiAttribute" /> /// 2、 <see cref="System.Attributes.HiddenSwaggerFilter" /> /// add all files like *.xmls in AppContext.BaseDirectory /// </remarks> /// <returns></returns> public static IServiceCollection AddSwaggerConfig(this IServiceCollection services, string title, IConfiguration configuration, ISwaggerConfig swaggerConfig = null, string apiVersion = "v1", Action <SwaggerGenOptions> swaggerGenOptionsAction = null) { if (swaggerConfig != null) { ConfigItems.InitSwaggerConfig(swaggerConfig); } ConfigItems.InitConfiguration(configuration); services.AddSwaggerGen(c => { c.DescribeAllParametersInCamelCase(); c.DocumentFilter <HiddenApiAttribute>(); c.DocumentFilter <HiddenSwaggerFilter>(); c.SwaggerDoc(apiVersion, new OpenApiInfo { Title = title, Version = apiVersion, }); var path = AppContext.BaseDirectory; foreach (var file in Directory.GetFiles(path)) { if (".xml".Equals(Path.GetExtension(file))) { c.IncludeXmlComments(Path.GetFullPath(file)); } } swaggerGenOptionsAction?.Invoke(c); }); return(services); }
public ApiConfiguration(ISwaggerConfig swaggerConfig, IHttpControllerActivator httpControllerActivator) { _swaggerConfig = swaggerConfig; _httpControllerActivator = httpControllerActivator; }