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

Swagger plugins support #3056

Merged
merged 10 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1302,12 +1302,15 @@ app.UseSwaggerUI(c =>
c.MaxDisplayedTags(5);
c.ShowExtensions();
c.ShowCommonExtensions();
c.Plugins = new string[]{"myCustomPlugin"}
martincostello marked this conversation as resolved.
Show resolved Hide resolved
c.EnableValidator();
c.SupportedSubmitMethods(SubmitMethod.Get, SubmitMethod.Head);
c.UseRequestInterceptor("(request) => { return request; }");
c.UseResponseInterceptor("(response) => { return response; }");
});
```
[!NOTE]
When adding custom plugins, make sure you added custom `js` file that defines the plugin function.
jvmlet marked this conversation as resolved.
Show resolved Hide resolved

### Inject Custom JavaScript ###

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Swashbuckle.AspNetCore.SwaggerUI.ConfigObject.Plugins.get -> System.Collections.Generic.IList<string>
Swashbuckle.AspNetCore.SwaggerUI.ConfigObject.Plugins.set -> void
6 changes: 6 additions & 0 deletions src/Swashbuckle.AspNetCore.SwaggerUI/SwaggerUIOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ public class ConfigObject
[JsonPropertyName("validatorUrl")]
public string ValidatorUrl { get; set; } = null;

/// <summary>
/// Any custom plugins' function names.
/// </summary>
[JsonPropertyName("plugins")]
public IList<string> Plugins { get; set; } = null;

[JsonExtensionData]
public Dictionary<string, object> AdditionalItems { get; set; } = [];
}
Expand Down
4 changes: 4 additions & 0 deletions src/Swashbuckle.AspNetCore.SwaggerUI/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ window.onload = function () {
if (interceptors.ResponseInterceptorFunction)
configObject.responseInterceptor = parseFunction(interceptors.ResponseInterceptorFunction);

if(configObject.plugins) {
jvmlet marked this conversation as resolved.
Show resolved Hide resolved
configObject.plugins = configObject.plugins.map(eval);
}
martincostello marked this conversation as resolved.
Show resolved Hide resolved

// Begin Swagger UI call region

const ui = SwaggerUIBundle(configObject);
Expand Down
5 changes: 3 additions & 2 deletions test/WebSites/CustomUIConfig/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,15 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
// Other
c.DocumentTitle = "CustomUIConfig";
c.StylesPath = "/ext/custom-stylesheet.css";
c.ScriptBundlePath = "/ext/custom-javascript.js";
c.ScriptPresetsPath = "/ext/custom-javascript.js";
jvmlet marked this conversation as resolved.
Show resolved Hide resolved
c.InjectStylesheet("/ext/custom-stylesheet.css");
c.InjectJavascript("/ext/custom-javascript.js");
c.InjectJavascript("/ext/custom-plugin.js");
c.UseRequestInterceptor("(req) => { req.headers['x-my-custom-header'] = 'MyCustomValue'; return req; }");
c.UseResponseInterceptor("(res) => { console.log('Custom interceptor intercepted response from:', res.url); return res; }");
c.EnablePersistAuthorization();

c.ConfigObject.Plugins = ["customPlugin"];

c.ConfigObject.AdditionalItems.Add("syntaxHighlight", false);
c.ConfigObject.AdditionalItems.Add("charProperty", 'c');
c.ConfigObject.AdditionalItems.Add("stringProperty", "value");
Expand Down
10 changes: 10 additions & 0 deletions test/WebSites/CustomUIConfig/wwwroot/ext/custom-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

jvmlet marked this conversation as resolved.
Show resolved Hide resolved
const customPlugin = function (system) {
var elem = document.createElement("div");
elem.innerHTML =
"<div style=\"text-align: center; font-family: Titillium Web,sans-serif; margin: 16px;\">This text was injected via /ext/custom-plugin.js, using the SwaggerUIOptions.Plugins method.</div>";

document.body.insertBefore(elem, document.body.firstChild);
return {
}
jvmlet marked this conversation as resolved.
Show resolved Hide resolved
}