-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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} | ||||||
<script> | ||||||
window.onload = function() { | ||||||
var url = window.location.search.match(/url=([^&]+)/); | ||||||
|
@@ -60,6 +62,7 @@ | |||||
plugins: [ | ||||||
SwaggerUIBundle.plugins.DownloadUrl, | ||||||
disableTryItOutPlugin | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{AdditionalPlugins} | ||||||
], | ||||||
layout: "StandaloneLayout" | ||||||
}); | ||||||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
<script> | ||||||||
window.onload = function() { | ||||||||
var url = window.location.search.match(/url=([^&]+)/); | ||||||||
|
@@ -59,7 +62,8 @@ | |||||||
], | ||||||||
plugins: [ | ||||||||
SwaggerUIBundle.plugins.DownloadUrl, | ||||||||
disableTryItOutPlugin | ||||||||
disableTryItOutPlugin | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
{AdditionalPlugins} | ||||||||
], | ||||||||
layout: "StandaloneLayout" | ||||||||
}); | ||||||||
|
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> | ||||||
|
@@ -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) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
.Replace("{DocumentTitle}", DocumentTitle); | ||||||
.Replace("{DocumentTitle}", DocumentTitle) | ||||||
.Replace("{AdditionalPlugins}", GeneratePluginsList(AdditionalPlugins.Keys.ToArray())) | ||||||
.Replace("{PluginScripts}", GetCustomScripts(AdditionalPlugins.Values.ToArray(), request)); | ||||||
|
||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return htmlBuilder.ToString(); | ||||||
} | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -16,6 +16,8 @@ | |||||
using System.Text; | ||||||
using System.Threading.Tasks; | ||||||
using System.Threading; | ||||||
using System.Runtime.CompilerServices; | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
#if AspNetOwin | ||||||
using Microsoft.Owin; | ||||||
|
||||||
|
@@ -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; } | ||||||
|
||||||
|
@@ -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)) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
return string.Empty; | ||||||
} | ||||||
|
||||||
var builder = new StringBuilder(); | ||||||
foreach (var path in scriptPaths) | ||||||
{ | ||||||
var scriptTag = GetCustomScriptHtml(path, request); | ||||||
builder.Append(scriptTag + "\n "); | ||||||
} | ||||||
|
||||||
return builder.ToString(); | ||||||
} | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||||||
|
@@ -132,5 +157,21 @@ protected string GenerateAdditionalSettings(IDictionary<string, object> addition | |||||
|
||||||
return code; | ||||||
} | ||||||
|
||||||
/// <summary> | ||||||
/// Generates the JavaScript plugins object to inset into the html. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
/// </summary> | ||||||
/// <param name="pluginsList"></param> | ||||||
/// <returns></returns> | ||||||
Comment on lines
+164
to
+165
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
protected string GeneratePluginsList(string[] pluginsList) | ||||||
{ | ||||||
var builder = new StringBuilder(); | ||||||
foreach (var plugin in pluginsList) | ||||||
{ | ||||||
builder.Append(",\n "); | ||||||
builder.Append(plugin); | ||||||
} | ||||||
return builder.ToString(); | ||||||
} | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.