예제 #1
0
        public static IServiceCollection AddSwagger(this IServiceCollection services, IConfiguration configuration)
        {
            var options = new SwaggerOption(); configuration.Bind("swagger", options);

            // add swagger doc for api
            services.AddSwaggerGen(c =>
            {
                var scheme = new OpenApiSecurityScheme
                {
                    In           = ParameterLocation.Header,
                    Description  = "JWT Authorization. To obtain a token, use the /Login. Then enter the token in the \"Value\" field in the format: \"Bearer {token}\"",
                    Name         = "Authorization",
                    Type         = SecuritySchemeType.ApiKey,
                    Scheme       = "bearer",
                    BearerFormat = "JWT",
                    Reference    = new OpenApiReference {
                        Type = ReferenceType.SecurityScheme, Id = "Bearer"
                    },
                };
                c.AddSecurityDefinition("Bearer", scheme);
                c.AddSecurityRequirement(new OpenApiSecurityRequirement {
                    { scheme, new string[0] }
                });

                c.SwaggerDoc(options.Version, new OpenApiInfo
                {
                    Title   = options.Title,
                    Version = options.Version,
                    Contact = new OpenApiContact
                    {
                        Name  = options.Name,
                        Email = options.Email,
                    },
                    Description = options.Description
                });
                c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
                c.DescribeAllEnumsAsStrings();

                //var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                //var xmlPath = Path.Combine(System.AppContext.BaseDirectory, xmlFile);
                //c.IncludeXmlComments(xmlPath);

                c.MapType <System.DateTime>(() => new OpenApiSchema {
                    Type = "string"
                });
                c.MapType <IResponseError>(() => new OpenApiSchema {
                    Type = "object"
                });

                c.OperationFilter <SwagerTagFilter>();
            });
            return(services);
        }
예제 #2
0
        public static IApplicationBuilder UseSwagger(this IApplicationBuilder app, IConfiguration configuration)
        {
            var options = new SwaggerOption(); configuration.Bind("Swagger", options);

            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint(options.UI.Json, options.Title);
                c.RoutePrefix = options.UI.RoutePrefix;
                //c.DefaultModelExpandDepth(2);
                //c.DefaultModelRendering(ModelRendering.Model);
                //c.DefaultModelsExpandDepth(-1);
                //c.DisplayOperationId();
                //c.DisplayRequestDuration();
                c.DocExpansion(DocExpansion.None);
                //c.EnableDeepLinking();
                c.EnableFilter();
                ////c.MaxDisplayedTags(5);
                c.ShowExtensions();
                c.EnableValidator();
                //c.SupportedSubmitMethods(SubmitMethod.Get, SubmitMethod.Head);
            });
            return(app);
        }