Skip to content

Commit

Permalink
Add support for external plugins (#2132)
Browse files Browse the repository at this point in the history
Signed-off-by: Phillip Kruger <[email protected]>
  • Loading branch information
phillip-kruger authored Jan 7, 2025
1 parent 733206c commit 1f49df8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public static byte[] createIndexHtml(Map<String, String> urls, String urlsPrimar
addOAuthSection(options);
// Add Preauth section
addPreauthorizeSection(options);
// Add any custom scripts
addScripts(options);

try (InputStream input = IndexHtmlCreator.class.getClassLoader()
.getResourceAsStream("META-INF/resources/template/index.html");
Expand Down Expand Up @@ -232,6 +234,22 @@ private static void addOAuthSection(Map<Option, String> options) {
}
}

private static void addScripts(Map<Option, String> options) {
if (options.containsKey(Option.scripts)) {
String[] scripts = options.get(Option.scripts).split(COMMA);
try (StringWriter sw = new StringWriter()) {
for (String script : scripts) {
sw.write("<script src=\"" + script.trim() + "\"></script>\n");
}
String scriptsSection = sw.toString();

options.put(Option.scriptsSection, scriptsSection);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}

private static void addUrlSection(Map<String, String> urls, String urlsPrimaryName, Map<Option, String> options) {
// If you added a urlSection, we assume you know what you are doing
if (!options.containsKey(Option.urlSection)) {
Expand Down Expand Up @@ -284,12 +302,12 @@ private static void addUrlSection(Map<String, String> urls, String urlsPrimaryNa
private static final String VAR_END = "}";

private static final Map<Option, String> DEFAULT_OPTIONS = new HashMap<>();
private static final String DEFAULT_URLS_PRIMARY_NAME = "Default";
private static final String COMMA = ",";
private static final String URL_FORMAT = "url: '%s'";
private static final String URLS_ENTRY_FORMAT = "{url: \"%s\", name: \"%s\"}";

static {

DEFAULT_OPTIONS.put(Option.scriptsSection, null);
DEFAULT_OPTIONS.put(Option.url, "/openapi");
DEFAULT_OPTIONS.put(Option.title, "SmallRye OpenAPI UI");
DEFAULT_OPTIONS.put(Option.selfHref, "/openapi-ui");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* @author Phillip Kruger ([email protected])
*/
public enum Option {
scriptsSection,
scripts,
url,
urlSection,
title,
Expand Down
1 change: 1 addition & 0 deletions ui/open-api-ui/src/main/resources/template/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<footer id="footer">${footer}</footer>
<script src="swagger-ui-bundle.js" charset="UTF-8"></script>
<script src="swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
${scriptsSection}
<script>

window.onload = function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,22 @@ void testCreateWithInitOAuth() throws IOException {
assertTrue(s.contains("usePkceWithAuthorizationCodeGrant: true"));
}

@Test
void testCreateWithCustomeScript() throws IOException {
Map<Option, String> options = new HashMap<>();
options.put(Option.scripts, "https://unpkg.com/swagger-ui-plugin-hierarchical-tags");
options.put(Option.plugins, "[SwaggerUIBundle.plugins.DownloadUrl,HierarchicalTagsPlugin]");

byte[] indexHtml = IndexHtmlCreator.createIndexHtml(options);
assertNotNull(indexHtml);

String s = new String(indexHtml);

assertTrue(s.contains("<title>SmallRye OpenAPI UI</title>"));
assertTrue(s.contains("<script src=\"https://unpkg.com/swagger-ui-plugin-hierarchical-tags\"></script>"));
assertTrue(s.contains("plugins: [SwaggerUIBundle.plugins.DownloadUrl,HierarchicalTagsPlugin]"));
}

@Test
void testCreateWithPreauthorizeBasic() throws IOException {
Map<Option, String> options = new HashMap<>();
Expand Down

0 comments on commit 1f49df8

Please sign in to comment.