From 8c0aa52cab44e6d9b7f5f129f174450f5c631cf9 Mon Sep 17 00:00:00 2001 From: rmorris Date: Mon, 27 Apr 2020 22:35:21 +0100 Subject: [PATCH] Add config method to simplify adding server(s) information --- .../SwaggerMiddleware.cs | 6 ++--- .../ConfigureSwaggerGeneratorOptions.cs | 1 + .../SwaggerGenOptionsExtensions.cs | 11 +++++++++ .../SwaggerGenerator/SwaggerGenerator.cs | 5 ++++ .../SwaggerGeneratorOptions.cs | 3 +++ .../SwaggerGenerator/SwaggerGeneratorTests.cs | 24 +++++++++++++++++++ test/WebSites/NetCore21/Startup.cs | 3 ++- 7 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/Swashbuckle.AspNetCore.Swagger/SwaggerMiddleware.cs b/src/Swashbuckle.AspNetCore.Swagger/SwaggerMiddleware.cs index 92e28748ca..c53058930d 100644 --- a/src/Swashbuckle.AspNetCore.Swagger/SwaggerMiddleware.cs +++ b/src/Swashbuckle.AspNetCore.Swagger/SwaggerMiddleware.cs @@ -33,9 +33,9 @@ public async Task Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvid return; } - var basePath = string.IsNullOrEmpty(httpContext.Request.PathBase) - ? null - : httpContext.Request.PathBase.ToString(); + var basePath = !string.IsNullOrEmpty(httpContext.Request.PathBase) + ? httpContext.Request.PathBase.ToString() + : null; try { diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/DependencyInjection/ConfigureSwaggerGeneratorOptions.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/DependencyInjection/ConfigureSwaggerGeneratorOptions.cs index 9320ca6c81..84e6fcb3a0 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/DependencyInjection/ConfigureSwaggerGeneratorOptions.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/DependencyInjection/ConfigureSwaggerGeneratorOptions.cs @@ -62,6 +62,7 @@ public void DeepCopy(SwaggerGeneratorOptions source, SwaggerGeneratorOptions tar target.TagsSelector = source.TagsSelector; target.SortKeySelector = source.SortKeySelector; target.DescribeAllParametersInCamelCase = source.DescribeAllParametersInCamelCase; + target.Servers = new List(source.Servers); target.SecuritySchemes = new Dictionary(source.SecuritySchemes); target.SecurityRequirements = new List(source.SecurityRequirements); target.ParameterFilters = new List(source.ParameterFilters); diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/DependencyInjection/SwaggerGenOptionsExtensions.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/DependencyInjection/SwaggerGenOptionsExtensions.cs index 7d16071904..dca5960461 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/DependencyInjection/SwaggerGenOptionsExtensions.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/DependencyInjection/SwaggerGenOptionsExtensions.cs @@ -124,6 +124,17 @@ public static void DescribeAllParametersInCamelCase(this SwaggerGenOptions swagg swaggerGenOptions.SwaggerGeneratorOptions.DescribeAllParametersInCamelCase = true; } + + /// + /// Provide specific server information to include in the generated Swagger document + /// + /// + /// A description of the server + public static void AddServer(this SwaggerGenOptions swaggerGenOptions, OpenApiServer server) + { + swaggerGenOptions.SwaggerGeneratorOptions.Servers.Add(server); + } + /// /// Add one or more "securityDefinitions", describing how your API is protected, to the generated Swagger /// diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs index 50ab918afd..a532968a0e 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs @@ -64,6 +64,11 @@ public OpenApiDocument GetSwagger(string documentName, string host = null, strin private IList GenerateServers(string host, string basePath) { + if (_options.Servers.Any()) + { + return new List(_options.Servers); + } + return (host == null && basePath == null) ? new List() : new List { new OpenApiServer { Url = $"{host}{basePath}" } }; diff --git a/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGeneratorOptions.cs b/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGeneratorOptions.cs index d0efab5fe0..642b1251d1 100644 --- a/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGeneratorOptions.cs +++ b/src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGeneratorOptions.cs @@ -15,6 +15,7 @@ public SwaggerGeneratorOptions() OperationIdSelector = DefaultOperationIdSelector; TagsSelector = DefaultTagsSelector; SortKeySelector = DefaultSortKeySelector; + Servers = new List(); SecuritySchemes = new Dictionary(); SecurityRequirements = new List(); ParameterFilters = new List(); @@ -39,6 +40,8 @@ public SwaggerGeneratorOptions() public bool DescribeAllParametersInCamelCase { get; set; } + public List Servers { get; set; } + public IDictionary SecuritySchemes { get; set; } public IList SecurityRequirements { get; set; } diff --git a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/SwaggerGeneratorTests.cs b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/SwaggerGeneratorTests.cs index 14b1e46eb9..810d37b283 100644 --- a/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/SwaggerGeneratorTests.cs +++ b/test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/SwaggerGeneratorTests.cs @@ -705,6 +705,30 @@ public void GetSwagger_SupportsOption_DescribeAllParametersInCamelCase() Assert.Equal("someParam", operation.Parameters.First().Name); } + [Fact] + public void GetSwagger_SupportsOption_Servers() + { + var subject = Subject( + apiDescriptions: new ApiDescription[] { }, + options: new SwaggerGeneratorOptions + { + SwaggerDocs = new Dictionary + { + ["v1"] = new OpenApiInfo { Version = "V1", Title = "Test API" } + }, + Servers = new List + { + new OpenApiServer { Url = "http://tempuri.org/api" } + } + } + ); + + var document = subject.GetSwagger("v1"); + + Assert.Equal(1, document.Servers.Count); + Assert.Equal("http://tempuri.org/api", document.Servers.First().Url); + } + [Fact] public void GetSwagger_SupportsOption_SecuritySchemes() { diff --git a/test/WebSites/NetCore21/Startup.cs b/test/WebSites/NetCore21/Startup.cs index 51ddf5ed04..7a8b5fced8 100644 --- a/test/WebSites/NetCore21/Startup.cs +++ b/test/WebSites/NetCore21/Startup.cs @@ -31,7 +31,8 @@ public void ConfigureServices(IServiceCollection services) services.AddSwaggerGen(c => { - //c.SwaggerDoc("v1", new OpenApiInfo { Title = "Test API", Version = "1" }); + c.SwaggerDoc("v1", new OpenApiInfo { Title = "Test API", Version = "1" }); + c.UseInlineDefinitionsForEnums(); }); services.AddSwaggerGenNewtonsoftSupport();