Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit 7f045e5

Browse files
committed
apitool: swagger doesn't load when is set fix jooby-project#992
1 parent d886bbb commit 7f045e5

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

modules/jooby-apitool/src/main/java/org/jooby/apitool/ApiTool.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -665,11 +665,12 @@ public ApiTool() {
665665

666666
binder.bind(ApiParser.class).toInstance(parser);
667667

668+
String contextPath = conf.getString("application.path");
668669
if (swaggerOptions != null) {
669-
swagger(env.router(), swaggerOptions, swagger);
670+
swagger(contextPath, env.router(), swaggerOptions, swagger);
670671
}
671672
if (ramlOptions != null) {
672-
raml(env.router(), ramlOptions, raml);
673+
raml(contextPath, env.router(), ramlOptions, raml);
673674
}
674675
}
675676

@@ -837,7 +838,7 @@ public ApiTool modify(final Predicate<RouteMethod> matcher,
837838
return this;
838839
}
839840

840-
private static void raml(Router router, Options options, Consumer<Raml> configurer)
841+
private static void raml(String contextPath, Router router, Options options, Consumer<Raml> configurer)
841842
throws IOException {
842843
String api = options.path + "/api.raml";
843844
/** /api.raml: */
@@ -855,13 +856,15 @@ private static void raml(Router router, Options options, Consumer<Raml> configur
855856
if (options.showUI) {
856857
router.assets(options.path + "/static/**", RAML_STATIC + "{0}");
857858

859+
String staticPath = Route.normalize(contextPath + options.path);
860+
String ramlPath = Route.normalize(contextPath + api);
858861
String index = readFile(RAML_STATIC + "index.html")
859-
.replace("styles/", options.path + "/static/styles/")
860-
.replace("scripts/", options.path + "/static/scripts/")
862+
.replace("styles/", staticPath + "/static/styles/")
863+
.replace("scripts/", staticPath + "/static/scripts/")
861864
.replace("<raml-initializer></raml-initializer>",
862865
"<raml-console-loader options=\"{ disableRamlClientGenerator: true, disableThemeSwitcher: true, disableTryIt: "
863866
+ (!options.tryIt) + " }\" src=\""
864-
+ api
867+
+ ramlPath
865868
+ "\"></raml-console-loader>");
866869
/** API console: */
867870
router.get(options.path, req -> {
@@ -874,7 +877,7 @@ private static void raml(Router router, Options options, Consumer<Raml> configur
874877
}
875878
}
876879

877-
private static void swagger(Router router, Options options, Consumer<Swagger> configurer)
880+
private static void swagger(String contextPath, Router router, Options options, Consumer<Swagger> configurer)
878881
throws IOException {
879882
/** /swagger.json or /swagger.raml: */
880883
router.get(options.path + "/swagger.json", options.path + "/swagger.yml", req -> {
@@ -897,17 +900,19 @@ private static void swagger(Router router, Options options, Consumer<Swagger> co
897900
router.assets(staticPath + "**", SWAGGER_STATIC + "{0}");
898901
router.assets(staticPath + "**", SWAGGER_THEME + "{0}");
899902

903+
String fullStaticPath = Route.normalize(contextPath + staticPath) + "/";
904+
String swaggerJsonPath = Route.normalize(contextPath + options.path) + "/swagger.json";
900905
String index = readFile(SWAGGER_STATIC + "index.html")
901-
.replace("./", staticPath)
906+
.replace("./", fullStaticPath)
902907
.replace("http://petstore.swagger.io/v2/swagger.json\",",
903-
options.path + "/swagger.json\", validatorUrl: null,")
908+
swaggerJsonPath + "\", validatorUrl: null,")
904909
.replace("</head>",
905910
options.tryIt ? "</head>" : "<style> .try-out {display: none;}</style></head>");
906911

907912
router.get(options.path, req -> {
908913
String page = Optional.ofNullable(req.param("theme").value(options.theme))
909914
.map(theme -> index.replace("<style>", "<link rel=\"stylesheet\" "
910-
+ "type=\"text/css\" href=\"" + staticPath + "theme-" + theme.toLowerCase()
915+
+ "type=\"text/css\" href=\"" + fullStaticPath + "theme-" + theme.toLowerCase()
911916
+ ".css\">\n<style>"))
912917
.orElse(index);
913918
return Results.ok(page).type(MediaType.html);

modules/jooby-apitool/src/test/java/apps/ApiDemo.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,6 @@ public class ApiDemo extends Jooby {
123123
});
124124

125125
use(new ApiTool()
126-
.modify(r -> r.pattern().startsWith("/api"), it-> {
127-
System.out.println(it);
128-
})
129126
.raml("/raml")
130127
.swagger("/swagger"));
131128
}

0 commit comments

Comments
 (0)