Skip to content
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
5 changes: 4 additions & 1 deletion src/NSwag.AspNet.Owin/SwaggerUi/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
{PluginScripts}
<script>
window.onload = function() {
var url = window.location.search.match(/url=([^&]+)/);
Expand All @@ -42,7 +43,9 @@
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
SwaggerUIBundle.plugins.DownloadUrl,
disableTryItOutPlugin,
{AdditionalPlugins}
],
layout: "StandaloneLayout"
});
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.AspNetCore/ReDocSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ internal override Task<string> TransformHtmlAsync(string html, HttpRequest reque
{
html = html.Replace("{AdditionalSettings}", GenerateAdditionalSettings(AdditionalSettings));
html = html.Replace("{CustomStyle}", GetCustomStyleHtml(request));
html = html.Replace("{CustomScript}", GetCustomScriptHtml(request));
html = html.Replace("{CustomScript}", GetCustomScriptHtml(CustomJavaScriptPath, request));
html = html.Replace("{DocumentTitle}", DocumentTitle);
return Task.FromResult(html);
}
Expand Down
5 changes: 4 additions & 1 deletion src/NSwag.AspNetCore/SwaggerUi/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
{PluginScripts}
<script>
window.onload = function() {
var url = window.location.search.match(/url=([^&]+)/);
Expand All @@ -42,7 +43,9 @@
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
SwaggerUIBundle.plugins.DownloadUrl,
disableTryItOutPlugin,
{AdditionalPlugins}
],
layout: "StandaloneLayout"
});
Expand Down
8 changes: 5 additions & 3 deletions src/NSwag.AspNetCore/SwaggerUiSettings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// <copyright file="SwaggerUiOwinSettings.cs" company="NSwag">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
Expand Down Expand Up @@ -178,9 +178,11 @@ internal override async Task<string> TransformHtmlAsync(string html, HttpRequest
"/oauth2-redirect.html\""
: "\"" + ServerUrl + TransformToExternalPath(Path, request) + "/oauth2-redirect.html\"")
.Replace("{CustomStyle}", GetCustomStyleHtml(request))
.Replace("{CustomScript}", GetCustomScriptHtml(request))
.Replace("{CustomScript}", GetCustomScriptHtml(CustomJavaScriptPath, request))
.Replace("{CustomHeadContent}", CustomHeadContent)
.Replace("{DocumentTitle}", DocumentTitle);
.Replace("{DocumentTitle}", DocumentTitle)
.Replace("{AdditionalPlugins}", GeneratePluginsList(AdditionalPlugins.Keys.ToArray()))
.Replace("{PluginScripts}", GetCustomScripts(AdditionalPlugins.Values.ToArray(), request));

Comment thread
jarleli marked this conversation as resolved.
return htmlBuilder.ToString();
}
Expand Down
52 changes: 44 additions & 8 deletions src/NSwag.AspNetCore/SwaggerUiSettingsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public SwaggerUiSettingsBase()
/// <summary>Gets or sets a URI to load a custom JavaScript file into the index.html.</summary>
public string CustomJavaScriptPath { get; set; }

/// <summary>Gets the additional Swagger UI plugins settings. Add key-value-paris that represent the plugin object name as key and plugin sript path as value.</summary>
public Dictionary<string,string> AdditionalPlugins { get; } = new Dictionary<string, string>();

/// <summary>Gets or sets a flag that indicates to use or not type="module" in a custom script tag (default: false).</summary>
public bool UseModuleTypeForCustomJavaScript { get; set; }

Expand Down Expand Up @@ -96,26 +99,45 @@ protected string GetCustomStyleHtml(HttpRequest request)
/// Gets an HTML snippet for including custom JavaScript in swagger UI.
/// </summary>
#if AspNetOwin
protected string GetCustomScriptHtml(IOwinRequest request)
protected string GetCustomScriptHtml(string scriptPath, IOwinRequest request)
#else
protected string GetCustomScriptHtml(HttpRequest request)
protected string GetCustomScriptHtml(string scriptPath, HttpRequest request)
#endif
{
if (CustomJavaScriptPath == null)
{
return string.Empty;
}

var scriptType = string.Empty;
if (UseModuleTypeForCustomJavaScript)
{
scriptType = "type=\"module\"";
}

var uriString = System.Net.WebUtility.HtmlEncode(TransformToExternalPath(CustomJavaScriptPath, request));
var uriString = System.Net.WebUtility.HtmlEncode(TransformToExternalPath(scriptPath, request));
return $"<script {scriptType} src=\"{uriString}\"></script>";
}

/// <summary>
/// Gets an HTML snippet for including custom JavaScript in swagger UI.
/// </summary>
#if AspNetOwin
protected string GetCustomScripts(string[] scriptPaths, IOwinRequest request)
#else
protected string GetCustomScripts(string[] scriptPaths, HttpRequest request)
#endif
{
if ((scriptPaths == null ) || (scriptPaths.Length == 0))
{
return string.Empty;
}

var builder = new StringBuilder();
foreach (var path in scriptPaths)
{
var scriptTag = GetCustomScriptHtml(path, request);
builder.Append(scriptTag + "\n ");
}

return builder.ToString();
}

Comment thread
jarleli marked this conversation as resolved.
/// <summary>Generates the additional objects JavaScript code.</summary>
/// <param name="additionalSettings">The additional settings.</param>
/// <returns>The code.</returns>
Expand All @@ -129,5 +151,19 @@ protected static string GenerateAdditionalSettings(IDictionary<string, object> a

return code;
}

/// <summary>
/// Generates the JavaScript plugins object to inset into the HTML.
/// </summary>
protected static string GeneratePluginsList(string[] pluginsList)
{
var builder = new StringBuilder();
foreach (var plugin in pluginsList)
{
builder.Append(",\n ");
builder.Append(plugin);
}
return builder.ToString();
}
}
}
Loading