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 2 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,13 +1302,16 @@ 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 ###

To tweak the behavior, you can inject additional JavaScript files by adding them to your `wwwroot` folder and specifying the relative paths in the middleware options:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Swashbuckle.AspNetCore.SwaggerUI.ConfigObject.Plugins.get -> string[]
Swashbuckle.AspNetCore.SwaggerUI.ConfigObject.Plugins.set -> void
7 changes: 7 additions & 0 deletions src/Swashbuckle.AspNetCore.SwaggerUI/SwaggerUIOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ public class ConfigObject
[JsonPropertyName("validatorUrl")]
public string ValidatorUrl { get; set; } = null;


jvmlet marked this conversation as resolved.
Show resolved Hide resolved
/// <summary>
/// Custom plugins function names.
jvmlet marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
[JsonPropertyName("plugins")]
public string[] Plugins { get; set; } = null;
jvmlet marked this conversation as resolved.
Show resolved Hide resolved

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

configObject.plugins = configObject.plugins ? configObject.plugins.map(eval) : [];
jvmlet 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 = new[] { "customPlugin" };
jvmlet marked this conversation as resolved.
Show resolved Hide resolved

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

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 {

}
martincostello marked this conversation as resolved.
Show resolved Hide resolved
jvmlet marked this conversation as resolved.
Show resolved Hide resolved
}