Ejemplo n.º 1
0
        public static void AddVersionedSwagger(this IServiceCollection services)
        {
            services.AddSwaggerGen(
                c =>
            {
                var serviceProvider = services.BuildServiceProvider();
                var provider        = serviceProvider.GetRequiredService <IApiVersionDescriptionProvider>();
                var environment     = serviceProvider.GetRequiredService <IWebHostEnvironment>();

                // Add a swagger document for each discovered API version
                foreach (var apiVersion in provider.ApiVersionDescriptions)
                {
                    c.SwaggerDoc(apiVersion.GroupName, SwaggerExtensions.GenerateSwaggerVersionInfo(apiVersion, environment));
                }

                // TODO: Dodaj filtry

                foreach (var assembly in Application.GetApplicationAssemblies())
                {
                    var xmlFile = $"{assembly.GetName().Name}.xml";
                    var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                    if (File.Exists(xmlPath))
                    {
                        c.IncludeXmlComments(xmlPath);
                    }
                }

                c.CustomSchemaIds(SwaggerExtensions.GetSchemaId);
            });
        }
Ejemplo n.º 2
0
        private static string GetSchemaId(Type type)
        {
            if (type.DeclaringType == null)
            {
                return(type.Name);
            }

            return(SwaggerExtensions.GetSchemaId(type.DeclaringType) + "." + type.Name);
        }
Ejemplo n.º 3
0
 private static OpenApiInfo GenerateSwaggerVersionInfo(ApiVersionDescription api, IWebHostEnvironment environment)
 {
     return(new OpenApiInfo
     {
         Version = api.ApiVersion.ToString(),
         Title = "Synergy sample API",
         Description = SwaggerExtensions.GetApiVersionDescription(api, environment),
         //TermsOfService = new Uri("https://github.com/synergy-software/net-api-best-practices/blob/master/LICENSE"),
         Contact = new OpenApiContact
         {
             Name = "Synergy software",
             Email = "*****@*****.**",
             Url = new Uri("https://github.com/synergy-software")
         },
         License = new OpenApiLicense
         {
             Name = "Use under MIT License",
             Url = new Uri("https://github.com/synergy-software/net-api-best-practices/blob/master/LICENSE")
         }
     });
 }