Exemplo n.º 1
0
        public static IServiceCollection AddSwagger(this IServiceCollection services, Action <SwaggerOptions> configure)
        {
            services.Configure(configure);
            var options = new SwaggerOptions();

            configure?.Invoke(options);

            services.AddSwaggerGen(setup =>
            {
                var xmlDocsFileName = $"{PlatformServices.Default.Application.ApplicationName}.xml";
                var xmlDocsFullPath = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, xmlDocsFileName);

                if (File.Exists(xmlDocsFullPath))
                {
                    setup.IncludeXmlComments(xmlDocsFileName, true);
                }

                foreach (var server in options.Servers)
                {
                    setup.AddServer(new OpenApiServer()
                    {
                        Url = server
                    });
                }

                setup.SwaggerDoc(_assemblyVersion, new OpenApiInfo
                {
                    Title   = _assemblyName,
                    Version = _assemblyVersion
                });

                setup.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
                {
                    Description = @"JWT Authorization header using the Bearer scheme. Example: 'Bearer eyJhbGciPSA...'",
                    Name        = "Authorization",
                    Scheme      = "Bearer",
                    In          = ParameterLocation.Header,
                    Type        = SecuritySchemeType.ApiKey
                });
            });

            return(services);
        }
Exemplo n.º 2
0
 public static IServiceCollection AddSwagger(this IServiceCollection services, SwaggerOptions options)
 {
     return(services.AddSwagger(configure =>
     {
         options.CopyTo(configure);
     }));
 }