Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RoutePrefix for Swagger UI and Swagger docs. #1256

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 41 additions & 20 deletions Swashbuckle.Core/Application/HttpConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,24 @@ public static class HttpConfigurationExtensions
{
private static readonly string DefaultRouteTemplate = "swagger/docs/{apiVersion}";

public static SwaggerEnabledConfiguration EnableSwagger(
this HttpConfiguration httpConfig,
Action<SwaggerDocsConfig> configure = null)
public static SwaggerEnabledConfiguration EnableSwagger(this HttpConfiguration httpConfig, Action<SwaggerDocsConfig> configure = null)
{
return EnableSwagger(httpConfig, DefaultRouteTemplate, configure);
}

public static SwaggerEnabledConfiguration EnableSwagger(
this HttpConfiguration httpConfig,
string routeTemplate,
Action<SwaggerDocsConfig> configure = null)
public static SwaggerEnabledConfiguration EnableSwagger(this HttpConfiguration httpConfig, string routeTemplate, Action<SwaggerDocsConfig> configure = null)
{
var config = new SwaggerDocsConfig();
if (configure != null) configure(config);
var config = new SwaggerDocsConfig();

if (configure != null)
{
configure(config);
}

if (!string.IsNullOrWhiteSpace(config.RoutePrefix))
{
routeTemplate = config.RoutePrefix + "/" + routeTemplate;
}

httpConfig.Routes.MapHttpRoute(
name: "swagger_docs" + routeTemplate,
Expand Down Expand Up @@ -76,20 +80,26 @@ public void EnableSwaggerUi(Action<SwaggerUiConfig> configure = null)
EnableSwaggerUi(DefaultRouteTemplate, configure);
}

public void EnableSwaggerUi(
string routeTemplate,
Action<SwaggerUiConfig> configure = null)
public void EnableSwaggerUi(string routeTemplate, Action<SwaggerUiConfig> configure = null)
{
var config = new SwaggerUiConfig(_discoveryPaths, _rootUrlResolver);
if (configure != null) configure(config);

_httpConfig.Routes.MapHttpRoute(
name: "swagger_ui" + routeTemplate,
routeTemplate: routeTemplate,
defaults: null,
constraints: new { assetPath = @".+" },
handler: new SwaggerUiHandler(config)
);
if (configure != null)
{
configure(config);
}

if (!string.IsNullOrWhiteSpace(config.RoutePrefix))
{
routeTemplate = config.RoutePrefix + "/" + routeTemplate;
}

_httpConfig.Routes.MapHttpRoute(
name: "swagger_ui" + routeTemplate,
routeTemplate: routeTemplate,
defaults: null,
constraints: new { assetPath = @".+" },
handler: new SwaggerUiHandler(config));

if (routeTemplate == DefaultRouteTemplate)
{
Expand All @@ -100,6 +110,17 @@ public void EnableSwaggerUi(
constraints: new { uriResolution = new HttpRouteDirectionConstraint(HttpRouteDirection.UriResolution) },
handler: new RedirectHandler(_rootUrlResolver, "swagger/ui/index"));
}

if (routeTemplate == config.RoutePrefix + "/" + DefaultRouteTemplate)
{
_httpConfig.Routes.MapHttpRoute(
name: "swagger_ui_shortcut" + routeTemplate,
routeTemplate: config.RoutePrefix + "/" + "swagger",
defaults: null,
constraints: new { uriResolution = new HttpRouteDirectionConstraint(HttpRouteDirection.UriResolution) },
handler: new RedirectHandler(_rootUrlResolver, config.RoutePrefix + "/" + "swagger/ui/index"));
}

}
}
}
3 changes: 3 additions & 0 deletions Swashbuckle.Core/Application/SwaggerDocsConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public class SwaggerDocsConfig

private Func<ISwaggerProvider, ISwaggerProvider> _customProviderFactory;

// Routing Prefix to the Swagger Docs
public string RoutePrefix { set; get; }

public SwaggerDocsConfig()
{
_versionInfoBuilder = new VersionInfoBuilder();
Expand Down
17 changes: 11 additions & 6 deletions Swashbuckle.Core/Application/SwaggerUiConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public class SwaggerUiConfig
private readonly Dictionary<string, string> _templateParams;
private readonly Func<HttpRequestMessage, string> _rootUrlResolver;

// Routing Prefix to the Swagger UI
public string RoutePrefix { set; get; }


public SwaggerUiConfig(IEnumerable<string> discoveryPaths, Func<HttpRequestMessage, string> rootUrlResolver)
{
_pathToAssetMap = new Dictionary<string, EmbeddedAssetDescriptor>();
Expand All @@ -34,8 +38,8 @@ public SwaggerUiConfig(IEnumerable<string> discoveryPaths, Func<HttpRequestMessa
{ "%(OAuth2AppName)", "" },
{ "%(OAuth2ScopeSeperator)", " " },
{ "%(OAuth2AdditionalQueryStringParams)", "{}" },
{ "%(ApiKeyName)", "api_key" },
{ "%(ApiKeyIn)", "query" }
{ "%(ApiKeyName)", "api_key" },
{ "%(ApiKeyIn)", "query" }
};
_rootUrlResolver = rootUrlResolver;

Expand Down Expand Up @@ -133,10 +137,11 @@ public void EnableOAuth2Support(
_templateParams["%(OAuth2AdditionalQueryStringParams)"] = JsonConvert.SerializeObject(additionalQueryStringParams);
}

public void EnableApiKeySupport(string name, string apiKeyIn) {
_templateParams["%(ApiKeyName)"] = name;
_templateParams["%(ApiKeyIn)"] = apiKeyIn;
}
public void EnableApiKeySupport(string name, string apiKeyIn)
{
_templateParams["%(ApiKeyName)"] = name;
_templateParams["%(ApiKeyIn)"] = apiKeyIn;
}

internal IAssetProvider GetSwaggerUiProvider()
{
Expand Down