Merge pull request #2893 from hapifhir/issue-2883-unable-to-load-OpenAPI-docs

Issue 2883 unable to load open api docs
This commit is contained in:
Tadgh 2021-08-16 15:59:57 -04:00 committed by GitHub
commit 7ee22a44c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 3 deletions

View File

@ -0,0 +1,5 @@
---
type: fix
issue: 2883
jira: SMILE-1107
title: "Open API docs failed to load if a custom context path is set. This has been corrected."

View File

@ -267,10 +267,17 @@ public class OpenApiInterceptor {
return false; return false;
} }
public String removeTrailingSlash(String theUrl) {
while(theUrl != null && theUrl.endsWith("/")) {
theUrl = theUrl.substring(0, theUrl.length() - 1);
}
return theUrl;
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private void serveSwaggerUiHtml(ServletRequestDetails theRequestDetails, HttpServletResponse theResponse) throws IOException { private void serveSwaggerUiHtml(ServletRequestDetails theRequestDetails, HttpServletResponse theResponse) throws IOException {
CapabilityStatement cs = getCapabilityStatement(theRequestDetails); CapabilityStatement cs = getCapabilityStatement(theRequestDetails);
String baseUrl = removeTrailingSlash(cs.getImplementation().getUrl());
theResponse.setStatus(200); theResponse.setStatus(200);
theResponse.setContentType(Constants.CT_HTML); theResponse.setContentType(Constants.CT_HTML);
@ -283,7 +290,7 @@ public class OpenApiInterceptor {
context.setVariable("SERVER_VERSION", cs.getSoftware().getVersion()); context.setVariable("SERVER_VERSION", cs.getSoftware().getVersion());
context.setVariable("BASE_URL", cs.getImplementation().getUrl()); context.setVariable("BASE_URL", cs.getImplementation().getUrl());
context.setVariable("BANNER_IMAGE_URL", getBannerImage()); context.setVariable("BANNER_IMAGE_URL", getBannerImage());
context.setVariable("OPENAPI_DOCS", cs.getImplementation().getUrl() + "/api-docs"); context.setVariable("OPENAPI_DOCS", baseUrl + "/api-docs");
context.setVariable("FHIR_VERSION", cs.getFhirVersion().toCode()); context.setVariable("FHIR_VERSION", cs.getFhirVersion().toCode());
context.setVariable("FHIR_VERSION_CODENAME", FhirVersionEnum.forVersionString(cs.getFhirVersion().toCode()).name()); context.setVariable("FHIR_VERSION_CODENAME", FhirVersionEnum.forVersionString(cs.getFhirVersion().toCode()).name());

View File

@ -45,7 +45,7 @@
window.onload = function() { window.onload = function() {
// Begin Swagger UI call region // Begin Swagger UI call region
const ui = SwaggerUIBundle({ const ui = SwaggerUIBundle({
url: "[[@{/api-docs(page=${PAGE})}]]", url: "[[${OPENAPI_DOCS} + '?page=' + ${PAGE}]]",
dom_id: '#swagger-ui', dom_id: '#swagger-ui',
deepLinking: true, deepLinking: true,
presets: [ presets: [

View File

@ -195,6 +195,25 @@ public class OpenApiInterceptorTest {
assertThat(buttonTexts.toString(), buttonTexts, Matchers.contains("All", "System Level Operations", "OperationDefinition 1", "Observation", "Patient")); assertThat(buttonTexts.toString(), buttonTexts, Matchers.contains("All", "System Level Operations", "OperationDefinition 1", "Observation", "Patient"));
} }
@Test
public void testRemoveTrailingSlash() {
OpenApiInterceptor interceptor = new OpenApiInterceptor();
String url1 = interceptor.removeTrailingSlash("http://localhost:8000");
String url2 = interceptor.removeTrailingSlash("http://localhost:8000/");
String url3 = interceptor.removeTrailingSlash("http://localhost:8000//");
String expect = "http://localhost:8000";
assertEquals(expect, url1);
assertEquals(expect, url2);
assertEquals(expect, url3);
}
@Test
public void testRemoveTrailingSlashWithNullUrl() {
OpenApiInterceptor interceptor = new OpenApiInterceptor();
String url = interceptor.removeTrailingSlash(null);
assertEquals(null, url);
}
private String fetchSwaggerUi(String url) throws IOException { private String fetchSwaggerUi(String url) throws IOException {
String resp; String resp;
HttpGet get = new HttpGet(url); HttpGet get = new HttpGet(url);