@@ -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}
0 commit comments