示例#1
0
        public static void AddSwaggerServices(this IServiceCollection services, SwaggerApiDefinition swagger, Installers installers)
        {
            if (!swagger.Options.IsEnabled)
            {
                return;
            }

            services.AddSwaggerGen(c =>
            {
                foreach (var document in swagger.Options.Documents)
                {
                    c.SwaggerDoc(document.Name, new Info {
                        Title = document.Title ?? document.Name, Version = document.Version
                    });
                }

                c.DescribeAllEnumsAsStrings();
                c.DescribeStringEnumsInCamelCase();

                ApplyGroupingBy(swagger.Options.GroupActionsBy, c);

                foreach (var commentsFile in swagger.Options.XmlCommentsFiles)
                {
                    var filePath = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, commentsFile);
                    c.IncludeXmlComments(filePath);
                }


                if (swagger.Options.SchemaId != SchemaIdType.Default)
                {
                    ConfigureCustomSchemaId(swagger.Options.SchemaId, c);
                }
            });
        }
示例#2
0
        public static SwaggerInstaller FromConfiguration(Installers installers, IConfiguration configuration, Type startupType)
        {
            var options = configuration.GetSection(SectionName)?.Get <ApiDefinitionOptions>() ?? new ApiDefinitionOptions();

            if (!options.IsEnabled)
            {
                return(null);
            }

            var definition = new SwaggerApiDefinition(options, startupType);

            return(new SwaggerInstaller(installers, definition, startupType));
        }
示例#3
0
        public static void UseSwaggerUi(this IApplicationBuilder app, SwaggerApiDefinition swagger)
        {
            if (!swagger.Options.IsEnabled)
            {
                return;
            }

            app.UseSwagger();

            app.UseSwaggerUI(c =>
            {
                foreach (var document in swagger.Options.Documents)
                {
                    c.SwaggerEndpoint($"/swagger/{document.Name}/swagger.json", $"{document.Title} {document.Version}");
                }
            });
        }
示例#4
0
        public static void AddSwaggerDocumentGroupingConvention(this IMvcBuilder mvcBuilder, SwaggerApiDefinition swagger)
        {
            if (!swagger.Options.IsEnabled)
            {
                return;
            }

            if (swagger.Options.DocumentGroupingPolicy == DocumentGroupingPolicyType.ByNamespaceSuffix)
            {
                mvcBuilder.AddMvcOptions(o => o.Conventions.Add(new ApiExplorerGroupingByNamespaceSuffixConvention(swagger.Options.Documents)));
            }
        }
示例#5
0
 public SwaggerInstaller(Installers installers, SwaggerApiDefinition definition, Type startupType)
 {
     _installers = installers;
     Definition  = definition ?? throw new ArgumentNullException(nameof(definition));
     StartupType = startupType;
 }