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

Add support for external plugins #2132

Merged
merged 2 commits into from
Jan 7, 2025
Merged
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
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
Loading