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

Adds a setting-option to add swagger plugins. #4922

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
3 changes: 3 additions & 0 deletions src/NSwag.AspNet.Owin/SwaggerUi/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

<script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>

{PluginScripts}
Comment on lines +20 to +21
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{PluginScripts}
{PluginScripts}

<script>
window.onload = function() {
var url = window.location.search.match(/url=([^&]+)/);
Expand Down Expand Up @@ -60,6 +62,7 @@
plugins: [
SwaggerUIBundle.plugins.DownloadUrl,
disableTryItOutPlugin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
disableTryItOutPlugin
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
6 changes: 5 additions & 1 deletion src/NSwag.AspNetCore/SwaggerUi/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

<script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>

{PluginScripts}

Comment on lines +20 to +22
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{PluginScripts}
{PluginScripts}

<script>
window.onload = function() {
var url = window.location.search.match(/url=([^&]+)/);
Expand Down Expand Up @@ -59,7 +62,8 @@
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl,
disableTryItOutPlugin
disableTryItOutPlugin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
disableTryItOutPlugin
disableTryItOutPlugin,

{AdditionalPlugins}
],
layout: "StandaloneLayout"
});
Expand Down
9 changes: 6 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 @@ -179,9 +179,12 @@ 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.Replace("{CustomScript}", GetCustomScriptHtml(CustomJavaScriptPath,request))
.Replace("{CustomScript}", GetCustomScriptHtml(CustomJavaScriptPath, request))

.Replace("{DocumentTitle}", DocumentTitle);
.Replace("{DocumentTitle}", DocumentTitle)
.Replace("{AdditionalPlugins}", GeneratePluginsList(AdditionalPlugins.Keys.ToArray()))
.Replace("{PluginScripts}", GetCustomScripts(AdditionalPlugins.Values.ToArray(), request));


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

return htmlBuilder.ToString();
}
Expand Down
57 changes: 49 additions & 8 deletions src/NSwag.AspNetCore/SwaggerUiSettingsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Runtime.CompilerServices;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

#if AspNetOwin
using Microsoft.Owin;

Expand Down Expand Up @@ -57,6 +59,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 @@ -99,26 +104,46 @@ 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.Count()==0))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ((scriptPaths == null )|| (scriptPaths.Count()==0))
if ((scriptPaths == null ) || (scriptPaths.Count() == 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();
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change


/// <summary>Generates the additional objects JavaScript code.</summary>
/// <param name="additionalSettings">The additional settings.</param>
/// <returns>The code.</returns>
Expand All @@ -132,5 +157,21 @@ protected string GenerateAdditionalSettings(IDictionary<string, object> addition

return code;
}

/// <summary>
/// Generates the JavaScript plugins object to inset into the html.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Generates the JavaScript plugins object to inset into the html.
/// Generates the JavaScript plugins object to inset into the HTML.

/// </summary>
/// <param name="pluginsList"></param>
/// <returns></returns>
Comment on lines +164 to +165
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// <param name="pluginsList"></param>
/// <returns></returns>

protected string GeneratePluginsList(string[] pluginsList)
{
var builder = new StringBuilder();
foreach (var plugin in pluginsList)
{
builder.Append(",\n ");
builder.Append(plugin);
}
return builder.ToString();
}
}
}
Loading