Skip to content

Commit

Permalink
Merge pull request #5230 from machi1990/fix/swagger-ui-reload
Browse files Browse the repository at this point in the history
 make sure that cachedOpenApiPath is always resolved against http rootpath
  • Loading branch information
gwenneg authored Nov 5, 2019
2 parents 375bba0 + 463928f commit b33f8b7
Showing 1 changed file with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,12 @@ public void registerSwaggerUiServletExtension(SwaggerUiRecorder recorder,
"quarkus.swagger-ui.path was set to \"/\", this is not allowed as it blocks the application from serving anything else.");
}

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

boolean extractionNeeded = cached == null;
if (cached != null && !cached.cachedOpenAPIPath.equals(openapi.path)) {
if (cached != null && !cached.cachedOpenAPIPath.equals(openApiPath)) {
try {
FileUtil.deleteDirectory(Paths.get(cached.cachedDirectory));
} catch (IOException e) {
Expand All @@ -108,9 +109,9 @@ public void registerSwaggerUiServletExtension(SwaggerUiRecorder recorder,
ResolvedArtifact artifact = getSwaggerUiArtifact();
Path tempDir = Files.createTempDirectory(TEMP_DIR_PREFIX).toRealPath();
extractSwaggerUi(artifact, tempDir);
updateApiUrl(tempDir.resolve("index.html"), httpRootPathBuildItem);
updateApiUrl(tempDir.resolve("index.html"), openApiPath);
cached.cachedDirectory = tempDir.toAbsolutePath().toString();
cached.cachedOpenAPIPath = openapi.path;
cached.cachedOpenAPIPath = openApiPath;
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -134,7 +135,7 @@ public void registerSwaggerUiServletExtension(SwaggerUiRecorder recorder,
String filename = entry.getName().replace(versionedSwaggerUiWebjarPrefix, "");
byte[] content = FileUtil.readFileContents(inputStream);
if (entry.getName().endsWith("index.html")) {
content = updateApiUrl(new String(content, StandardCharsets.UTF_8), httpRootPathBuildItem)
content = updateApiUrl(new String(content, StandardCharsets.UTF_8), openApiPath)
.getBytes(StandardCharsets.UTF_8);
}
String fileName = SWAGGER_UI_FINAL_DESTINATION + "/" + filename;
Expand Down Expand Up @@ -177,19 +178,19 @@ private void extractSwaggerUi(ResolvedArtifact artifact, Path resourceDir) throw
}
}

private void updateApiUrl(Path indexHtml, HttpRootPathBuildItem httpRoot) throws IOException {
private void updateApiUrl(Path indexHtml, String openApiPath) throws IOException {
String content = new String(Files.readAllBytes(indexHtml), StandardCharsets.UTF_8);
String result = updateApiUrl(content, httpRoot);
String result = updateApiUrl(content, openApiPath);
if (result != null) {
Files.write(indexHtml, result.getBytes(StandardCharsets.UTF_8));
}
}

public String updateApiUrl(String original, HttpRootPathBuildItem httpRoot) {
public String updateApiUrl(String original, String openApiPath) {

Matcher uriMatcher = SWAGGER_UI_DEFAULT_API_URL_PATTERN.matcher(original);
if (uriMatcher.matches()) {
return uriMatcher.replaceFirst("$1" + httpRoot.adjustPath(openapi.path) + "$3");
return uriMatcher.replaceFirst("$1" + openApiPath + "$3");
} else {
log.warn("Unable to replace the default URL of Swagger UI");
return null;
Expand Down

0 comments on commit b33f8b7

Please sign in to comment.