From 1013c7b57286f304012907d3f22db3f58b62ac62 Mon Sep 17 00:00:00 2001 From: Phillip Kruger Date: Tue, 7 Jan 2025 14:12:14 +1100 Subject: [PATCH] Add support for external plugins Signed-off-by: Phillip Kruger --- .../smallrye/openapi/ui/IndexHtmlCreator.java | 22 +++++++++++++++++-- .../java/io/smallrye/openapi/ui/Option.java | 2 ++ .../src/main/resources/template/index.html | 1 + .../smallrye/openapi/ui/IndexCreatorTest.java | 16 ++++++++++++++ 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/ui/open-api-ui/src/main/java/io/smallrye/openapi/ui/IndexHtmlCreator.java b/ui/open-api-ui/src/main/java/io/smallrye/openapi/ui/IndexHtmlCreator.java index da228ca06..f421f3d8a 100644 --- a/ui/open-api-ui/src/main/java/io/smallrye/openapi/ui/IndexHtmlCreator.java +++ b/ui/open-api-ui/src/main/java/io/smallrye/openapi/ui/IndexHtmlCreator.java @@ -41,6 +41,8 @@ public static byte[] createIndexHtml(Map 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"); @@ -232,6 +234,22 @@ private static void addOAuthSection(Map options) { } } + private static void addScripts(Map 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("\n"); + } + String scriptsSection = sw.toString(); + + options.put(Option.scriptsSection, scriptsSection); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + } + private static void addUrlSection(Map urls, String urlsPrimaryName, Map options) { // If you added a urlSection, we assume you know what you are doing if (!options.containsKey(Option.urlSection)) { @@ -284,12 +302,12 @@ private static void addUrlSection(Map urls, String urlsPrimaryNa private static final String VAR_END = "}"; private static final Map 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"); diff --git a/ui/open-api-ui/src/main/java/io/smallrye/openapi/ui/Option.java b/ui/open-api-ui/src/main/java/io/smallrye/openapi/ui/Option.java index 11d6aefa0..804c8145a 100644 --- a/ui/open-api-ui/src/main/java/io/smallrye/openapi/ui/Option.java +++ b/ui/open-api-ui/src/main/java/io/smallrye/openapi/ui/Option.java @@ -6,6 +6,8 @@ * @author Phillip Kruger (phillip.kruger@redhat.com) */ public enum Option { + scriptsSection, + scripts, url, urlSection, title, diff --git a/ui/open-api-ui/src/main/resources/template/index.html b/ui/open-api-ui/src/main/resources/template/index.html index d6e1261cb..9a26a27bb 100644 --- a/ui/open-api-ui/src/main/resources/template/index.html +++ b/ui/open-api-ui/src/main/resources/template/index.html @@ -16,6 +16,7 @@
${footer}
+ ${scriptsSection} ")); + assertTrue(s.contains("plugins: [SwaggerUIBundle.plugins.DownloadUrl,HierarchicalTagsPlugin]")); + } + @Test void testCreateWithPreauthorizeBasic() throws IOException { Map options = new HashMap<>();