Skip to content

Commit b33f8b7

Browse files
authored
Merge pull request #5230 from machi1990/fix/swagger-ui-reload
make sure that cachedOpenApiPath is always resolved against http rootpath
2 parents 375bba0 + 463928f commit b33f8b7

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

extensions/swagger-ui/deployment/src/main/java/io/quarkus/swaggerui/deployment/SwaggerUiProcessor.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ public void registerSwaggerUiServletExtension(SwaggerUiRecorder recorder,
8686
"quarkus.swagger-ui.path was set to \"/\", this is not allowed as it blocks the application from serving anything else.");
8787
}
8888

89+
String openApiPath = httpRootPathBuildItem.adjustPath(openapi.path);
8990
if (launch.getLaunchMode().isDevOrTest()) {
9091
CachedSwaggerUI cached = liveReloadBuildItem.getContextObject(CachedSwaggerUI.class);
9192

9293
boolean extractionNeeded = cached == null;
93-
if (cached != null && !cached.cachedOpenAPIPath.equals(openapi.path)) {
94+
if (cached != null && !cached.cachedOpenAPIPath.equals(openApiPath)) {
9495
try {
9596
FileUtil.deleteDirectory(Paths.get(cached.cachedDirectory));
9697
} catch (IOException e) {
@@ -108,9 +109,9 @@ public void registerSwaggerUiServletExtension(SwaggerUiRecorder recorder,
108109
ResolvedArtifact artifact = getSwaggerUiArtifact();
109110
Path tempDir = Files.createTempDirectory(TEMP_DIR_PREFIX).toRealPath();
110111
extractSwaggerUi(artifact, tempDir);
111-
updateApiUrl(tempDir.resolve("index.html"), httpRootPathBuildItem);
112+
updateApiUrl(tempDir.resolve("index.html"), openApiPath);
112113
cached.cachedDirectory = tempDir.toAbsolutePath().toString();
113-
cached.cachedOpenAPIPath = openapi.path;
114+
cached.cachedOpenAPIPath = openApiPath;
114115
} catch (IOException e) {
115116
throw new RuntimeException(e);
116117
}
@@ -134,7 +135,7 @@ public void registerSwaggerUiServletExtension(SwaggerUiRecorder recorder,
134135
String filename = entry.getName().replace(versionedSwaggerUiWebjarPrefix, "");
135136
byte[] content = FileUtil.readFileContents(inputStream);
136137
if (entry.getName().endsWith("index.html")) {
137-
content = updateApiUrl(new String(content, StandardCharsets.UTF_8), httpRootPathBuildItem)
138+
content = updateApiUrl(new String(content, StandardCharsets.UTF_8), openApiPath)
138139
.getBytes(StandardCharsets.UTF_8);
139140
}
140141
String fileName = SWAGGER_UI_FINAL_DESTINATION + "/" + filename;
@@ -177,19 +178,19 @@ private void extractSwaggerUi(ResolvedArtifact artifact, Path resourceDir) throw
177178
}
178179
}
179180

180-
private void updateApiUrl(Path indexHtml, HttpRootPathBuildItem httpRoot) throws IOException {
181+
private void updateApiUrl(Path indexHtml, String openApiPath) throws IOException {
181182
String content = new String(Files.readAllBytes(indexHtml), StandardCharsets.UTF_8);
182-
String result = updateApiUrl(content, httpRoot);
183+
String result = updateApiUrl(content, openApiPath);
183184
if (result != null) {
184185
Files.write(indexHtml, result.getBytes(StandardCharsets.UTF_8));
185186
}
186187
}
187188

188-
public String updateApiUrl(String original, HttpRootPathBuildItem httpRoot) {
189+
public String updateApiUrl(String original, String openApiPath) {
189190

190191
Matcher uriMatcher = SWAGGER_UI_DEFAULT_API_URL_PATTERN.matcher(original);
191192
if (uriMatcher.matches()) {
192-
return uriMatcher.replaceFirst("$1" + httpRoot.adjustPath(openapi.path) + "$3");
193+
return uriMatcher.replaceFirst("$1" + openApiPath + "$3");
193194
} else {
194195
log.warn("Unable to replace the default URL of Swagger UI");
195196
return null;

0 commit comments

Comments
 (0)