Skip to content

Commit 7c325da

Browse files
committed
openapi: add custom context path for redoc/swagger ui
- fix #3574
1 parent 0ada8cc commit 7c325da

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

jooby/src/main/java/io/jooby/OpenAPIModule.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public static Format from(@NonNull String filePath) {
122122
private String redocPath = "/redoc";
123123
private EnumSet<Format> format = EnumSet.of(Format.JSON, Format.YAML);
124124
private final Set<String> customFiles = new LinkedHashSet<>();
125+
private String contextPath;
125126

126127
/**
127128
* Creates an OpenAPI module. The path is used to route the open API files. For example:
@@ -153,11 +154,23 @@ public OpenAPIModule() {
153154
* @param path Path.
154155
* @return This module.
155156
*/
156-
public @NonNull OpenAPIModule file(String path) {
157+
public @NonNull OpenAPIModule file(@NonNull String path) {
157158
customFiles.add(path);
158159
return this;
159160
}
160161

162+
/**
163+
* Set/override context path of OpenAPI resource. Helps when it runs behind a proxy on a different
164+
* context path or path prefix.
165+
*
166+
* @param contextPath Context path/Path prefix.
167+
* @return This module.
168+
*/
169+
public @NonNull OpenAPIModule setContextPath(@NonNull String contextPath) {
170+
this.contextPath = contextPath;
171+
return this;
172+
}
173+
161174
/**
162175
* Customize the swagger-ui path. Defaults is <code>/swagger</code>.
163176
*
@@ -251,8 +264,7 @@ private void configureUI(Jooby application) {
251264

252265
private void redoc(Jooby application, AssetSource source) throws Exception {
253266

254-
String openAPIJSON =
255-
fullPath(fullPath(application.getContextPath(), openAPIPath), "/openapi.json");
267+
String openAPIJSON = fullPath(fullPath(contextPath(application), openAPIPath), "/openapi.json");
256268

257269
AssetSource customSource =
258270
new OpenAPISource()
@@ -265,14 +277,13 @@ private void redoc(Jooby application, AssetSource source) throws Exception {
265277
"${openAPIPath}",
266278
openAPIJSON,
267279
"${redocPath}",
268-
fullPath(application.getContextPath(), redocPath)));
280+
fullPath(contextPath(application), redocPath)));
269281

270282
application.assets(redocPath + "/?*", customSource, source);
271283
}
272284

273285
private void swaggerUI(Jooby application, AssetSource source) throws Exception {
274-
String openAPIJSON =
275-
fullPath(fullPath(application.getContextPath(), openAPIPath), "/openapi.json");
286+
String openAPIJSON = fullPath(fullPath(contextPath(application), openAPIPath), "/openapi.json");
276287

277288
AssetSource customSource =
278289
new OpenAPISource()
@@ -283,7 +294,7 @@ private void swaggerUI(Jooby application, AssetSource source) throws Exception {
283294
MediaType.html,
284295
"index.html",
285296
"${swaggerPath}",
286-
fullPath(application.getContextPath(), swaggerUIPath)))
297+
fullPath(contextPath(application), swaggerUIPath)))
287298
.put(
288299
"swagger-initializer.js",
289300
processAsset(
@@ -311,4 +322,8 @@ private static Asset processAsset(
311322
private static String fullPath(String contextPath, String path) {
312323
return Router.noTrailingSlash(Router.normalizePath(contextPath + path));
313324
}
325+
326+
private String contextPath(Jooby application) {
327+
return contextPath == null ? application.getContextPath() : contextPath;
328+
}
314329
}

modules/jooby-redoc/src/test/java/io/jooby/redoc/RedocResourceTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package io.jooby.redoc;
77

8+
import static java.util.Objects.requireNonNull;
89
import static org.junit.jupiter.api.Assertions.assertNotNull;
910
import static org.junit.jupiter.api.Assertions.assertTrue;
1011

@@ -29,6 +30,7 @@ public void shouldCheckBundle() throws IOException {
2930
}
3031

3132
private String asset(String resource) throws IOException {
32-
return IOUtils.toString(getClass().getResource("/redoc/" + resource), StandardCharsets.UTF_8);
33+
return IOUtils.toString(
34+
requireNonNull(getClass().getResource("/redoc/" + resource)), StandardCharsets.UTF_8);
3335
}
3436
}

modules/jooby-swagger-ui/src/test/java/io.jooby.swagger/SwaggerResourceTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import java.io.IOException;
1111
import java.nio.charset.StandardCharsets;
12+
import java.util.Objects;
1213

1314
import org.apache.commons.io.IOUtils;
1415
import org.junit.jupiter.api.Test;
@@ -30,6 +31,7 @@ public void shouldCheckSwaggerInitializer() throws IOException {
3031

3132
private String asset(String resource) throws IOException {
3233
return IOUtils.toString(
33-
getClass().getResource("/swagger-ui/" + resource), StandardCharsets.UTF_8);
34+
Objects.requireNonNull(getClass().getResource("/swagger-ui/" + resource)),
35+
StandardCharsets.UTF_8);
3436
}
3537
}

0 commit comments

Comments
 (0)