/// <summary>
 /// Visits <see cref="OpenApiContact"/>
 /// </summary>
 public virtual void Visit(OpenApiContact contact)
 {
 }
        public static IServiceCollection AddSwaggerGeneration(this IServiceCollection services, OpenApiContact apiContact, string swaggerTitle, Type callerType)
        {
            return(services.AddSwaggerGen(options =>
            {
                // Resolve the temporary IApiVersionDescriptionProvider service
                var provider = services.BuildServiceProvider().GetRequiredService <IApiVersionDescriptionProvider>();

                // Add a swagger document for each discovered API version
                foreach (var description in provider.ApiVersionDescriptions)
                {
                    options.SwaggerDoc(description.GroupName, new OpenApiInfo
                    {
                        Title = swaggerTitle + $" {description.ApiVersion}",
                        Version = description.ApiVersion.ToString(),
                        Contact = apiContact
                    });
                }

                // Add a custom filter for setting the default values
                options.OperationFilter <SwaggerDefaultValues>();

                // Tells swagger to pick up the output XML document file
                options.IncludeXmlComments(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), $"{callerType.Assembly.GetName().Name}.xml"));
            }));
        }
Example #3
0
        public static IServiceCollection AddSwaggerConfiguration(this IServiceCollection services)
        {
            var contact = new OpenApiContact()
            {
                Name  = "Ever Orellana",
                Email = "*****@*****.**",
                Url   = new Uri("https://github.com/ever1509")
            };

            var license = new OpenApiLicense()
            {
                Name = "Expenses Manager App",
                Url  = new Uri("https://www.expensesmgr.com")
            };

            var info = new OpenApiInfo()
            {
                Version        = "v1",
                Title          = "Expenses Manager API",
                Description    = "API which has functionality to manage expenses",
                TermsOfService = new Uri("https://www.expensesmgr.com/terms/"),
                Contact        = contact,
                License        = license
            };

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("ExpensesMgrAPI", info);
                c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());

                //Adding configuration of jwt for swagger UI ----------------------------------

                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
                {
                    Description = @"JWT Authorization header using the Bearer scheme. 
                      Enter 'Bearer' [space] and then your token in the text input below.
                      Example: 'Bearer 12345abcdef'",
                    Name        = "Authorization",
                    In          = ParameterLocation.Header,
                    Type        = SecuritySchemeType.ApiKey,
                    Scheme      = "Bearer"
                });
                c.AddSecurityRequirement(new OpenApiSecurityRequirement()
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id   = "Bearer"
                            }
                        },
                        new List <string>()
                    }
                });
                //----------------------------------------
            });

            return(services);
        }
        /// <summary>
        /// Adds Swagger services and configures the Swagger services.
        /// </summary>
        /// <param name="services">The services collection or IoC container.</param>
        /// <param name="configuration">Gets or sets the application configuration, where key value pair settings are stored.</param>
        public static IServiceCollection AddSwagger(this IServiceCollection services, IConfiguration configuration)
        {
            var webApiEnviroment = configuration.GetSection(WebApiValues.WebApiEnvirormentConfiguration).Get <EnvirormentSettings>();

            services.AddSwaggerGen(
                options =>
            {
                //var assembly = typeof(Startup).GetTypeInfo().Assembly;
                //var strTitle = assembly.GetCustomAttribute<AssemblyProductAttribute>().Product;
                //var strDescription = assembly.GetCustomAttribute<AssemblyDescriptionAttribute>().Description;

                var strAppContact = new OpenApiContact()
                {
                    Email = webApiEnviroment.WebApiContactMail,
                    Name  = webApiEnviroment.WebApiContactName,
                    Url   = new System.Uri(webApiEnviroment.WebApiContactUrl)
                };

                var strAppLicence = new OpenApiLicense()
                {
                    Name = webApiEnviroment.WebApiLicenseName,
                    Url  = new System.Uri(webApiEnviroment.WebApiLicenseUrl)
                };

                options.EnableAnnotations();

                //// Add the XML comment file for this assembly, so it's contents can be displayed.
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    // Use windows path with \\
                    options.IncludeXmlComments(webApiEnviroment.SwaggerXmlComments);
                }
                else
                {
                    // Use linux path with \
                    options.IncludeXmlComments(webApiEnviroment.SwaggerXmlComments.Replace(@"\", @"/"));
                }

                //// Show an example model for JsonPatchDocument<T>.
                //options.SchemaFilter<JsonPatchDocumentSchemaFilter>();
                //// Show an example model for ModelStateDictionary.
                //options.SchemaFilter<ModelStateDictionarySchemaFilter>();

                options.SwaggerDoc(
                    "v1",
                    new OpenApiInfo()
                {
                    Version        = webApiEnviroment.WebApiVersion,
                    Title          = webApiEnviroment.WebApiTitle,
                    Description    = webApiEnviroment.WebApiDescription,
                    Contact        = strAppContact,
                    License        = strAppLicence,
                    TermsOfService = new System.Uri(webApiEnviroment.WebApiLicenseUrl)         // webApiEnviroment.WebApiTermOfService,
                });


                options.CustomSchemaIds(x => x.FullName);
            });


            return(services);
        }