Skip to content

Commit

Permalink
Merge pull request #1640 from domaindrivendev/config-method-for-addin…
Browse files Browse the repository at this point in the history
…g-servers

Add config method to simplify adding server(s) information
  • Loading branch information
domaindrivendev authored Apr 28, 2020
2 parents 6bda7fb + 8c0aa52 commit b043143
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Swashbuckle.AspNetCore.Swagger/SwaggerMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<OpenApiServer>(source.Servers);
target.SecuritySchemes = new Dictionary<string, OpenApiSecurityScheme>(source.SecuritySchemes);
target.SecurityRequirements = new List<OpenApiSecurityRequirement>(source.SecurityRequirements);
target.ParameterFilters = new List<IParameterFilter>(source.ParameterFilters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ public static void DescribeAllParametersInCamelCase(this SwaggerGenOptions swagg
swaggerGenOptions.SwaggerGeneratorOptions.DescribeAllParametersInCamelCase = true;
}


/// <summary>
/// Provide specific server information to include in the generated Swagger document
/// </summary>
/// <param name="swaggerGenOptions"></param>
/// <param name="server">A description of the server</param>
public static void AddServer(this SwaggerGenOptions swaggerGenOptions, OpenApiServer server)
{
swaggerGenOptions.SwaggerGeneratorOptions.Servers.Add(server);
}

/// <summary>
/// Add one or more "securityDefinitions", describing how your API is protected, to the generated Swagger
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public OpenApiDocument GetSwagger(string documentName, string host = null, strin

private IList<OpenApiServer> GenerateServers(string host, string basePath)
{
if (_options.Servers.Any())
{
return new List<OpenApiServer>(_options.Servers);
}

return (host == null && basePath == null)
? new List<OpenApiServer>()
: new List<OpenApiServer> { new OpenApiServer { Url = $"{host}{basePath}" } };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public SwaggerGeneratorOptions()
OperationIdSelector = DefaultOperationIdSelector;
TagsSelector = DefaultTagsSelector;
SortKeySelector = DefaultSortKeySelector;
Servers = new List<OpenApiServer>();
SecuritySchemes = new Dictionary<string, OpenApiSecurityScheme>();
SecurityRequirements = new List<OpenApiSecurityRequirement>();
ParameterFilters = new List<IParameterFilter>();
Expand All @@ -39,6 +40,8 @@ public SwaggerGeneratorOptions()

public bool DescribeAllParametersInCamelCase { get; set; }

public List<OpenApiServer> Servers { get; set; }

public IDictionary<string, OpenApiSecurityScheme> SecuritySchemes { get; set; }

public IList<OpenApiSecurityRequirement> SecurityRequirements { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, OpenApiInfo>
{
["v1"] = new OpenApiInfo { Version = "V1", Title = "Test API" }
},
Servers = new List<OpenApiServer>
{
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()
{
Expand Down
3 changes: 2 additions & 1 deletion test/WebSites/NetCore21/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit b043143

Please sign in to comment.