Skip to content

Commit

Permalink
RESTWS-649 - Correct host property in OpenAPI Spec (openmrs#302)
Browse files Browse the repository at this point in the history
* Make host and basePath not mandatory in SwaggerSpecificationCreator
* Set host and basePath to correct values in  SwaggerSpecificationController
  • Loading branch information
gayanW authored and dkayiwa committed Sep 21, 2017
1 parent 4563334 commit bba5380
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ public class SwaggerSpecificationCreator {

private Swagger swagger = new Swagger();

private String host;

private String basePath;

private List<Scheme> schemes;

private String baseUrl;

private static List<SearchHandlerDoc> searchHandlerDocs;
Expand All @@ -87,10 +93,27 @@ public class SwaggerSpecificationCreator {

private Logger log = Logger.getLogger(this.getClass());

public SwaggerSpecificationCreator(String baseUrl) {
this.baseUrl = baseUrl;
List<SearchHandler> searchHandlers = Context.getService(RestService.class).getAllSearchHandlers();
searchHandlerDocs = fillSearchHandlers(searchHandlers, baseUrl);
public SwaggerSpecificationCreator() {
}

public SwaggerSpecificationCreator host(String host) {
this.host = host;
return this;
}

public SwaggerSpecificationCreator basePath(String basePath) {
this.basePath = basePath;
return this;
}

public SwaggerSpecificationCreator scheme(Scheme scheme) {
if (schemes == null) {
this.schemes = new ArrayList<Scheme>();
}
if (!schemes.contains(scheme)) {
this.schemes.add(scheme);
}
return this;
}

public String BuildJSON() {
Expand Down Expand Up @@ -179,20 +202,16 @@ private void initSwagger() {

this.swagger
.info(info)
.scheme(Scheme.HTTP)
.host(this.host)
.basePath(this.basePath)
.schemes(this.schemes)
.securityDefinition("basic_auth", new BasicAuthDefinition())
.security(new SecurityRequirement().requirement("basic_auth"))
.consumes("application/json")
.produces("application/json")
.externalDocs(new ExternalDocs()
.description("Find more info on REST Module Wiki")
.url("https://wiki.openmrs.org/display/docs/REST+Module"));

String host = baseUrl.split("/")[0];
String basePath = baseUrl.substring(baseUrl.indexOf("/")) + "/v1";
this.swagger
.host(host)
.basePath(basePath);
}

private List<ModuleVersion> getModuleVersions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
*/
package org.openmrs.module.webservices.rest.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.openmrs.api.context.Context;
import com.google.common.net.HttpHeaders;
import io.swagger.models.Scheme;
import org.openmrs.module.webservices.docs.swagger.SwaggerSpecificationCreator;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller("SwaggerSpecificationController")
@RequestMapping("/module/webservices/rest/swagger.json")
public class SwaggerSpecificationController {
Expand All @@ -27,45 +27,12 @@ public class SwaggerSpecificationController {
public @ResponseBody
String getSwaggerSpecification(HttpServletRequest request) throws Exception {

String swaggerSpecificationJSON = "";
StringBuilder baseUrl = new StringBuilder();
String scheme = request.getScheme();
int port = request.getServerPort();

baseUrl.append(scheme); // http, https
baseUrl.append("://");
baseUrl.append(request.getServerName());
if ((scheme.equals("http") && port != 80) || (scheme.equals("https") && port != 443)) {
baseUrl.append(':');
baseUrl.append(request.getServerPort());
}

baseUrl.append(request.getContextPath());

String resourcesUrl = Context.getAdministrationService().getGlobalProperty(
RestConstants.URI_PREFIX_GLOBAL_PROPERTY_NAME, baseUrl.toString());

if (!resourcesUrl.endsWith("/")) {
resourcesUrl += "/";
}

resourcesUrl += "ws/rest";

String urlWithoutScheme = "";

/* Swagger appends scheme to urls, so we should remove it */
if (scheme.equals("http"))
urlWithoutScheme = resourcesUrl.replace("http://", "");

else if (scheme.equals("https"))
urlWithoutScheme = resourcesUrl.replace("https://", "");

SwaggerSpecificationCreator creator = new SwaggerSpecificationCreator(urlWithoutScheme);

swaggerSpecificationJSON = creator.BuildJSON();

return swaggerSpecificationJSON;

return new SwaggerSpecificationCreator()
.host(request.getHeader(HttpHeaders.HOST))
.basePath(request.getContextPath() + "/ws/rest/v1")
.scheme(Scheme.forValue(request.getScheme()))

.BuildJSON();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,13 @@ public class SwaggerSpecificationCreatorTest extends BaseModuleWebContextSensiti

@Test
public void mainTest() {
String baseUrl = "host/openmrs/ws/rest";
String str = new SwaggerSpecificationCreator(baseUrl).BuildJSON();
String str = new SwaggerSpecificationCreator().BuildJSON();
assertNotNull(str);
}

@Test
public void hasSearchHandler() {
String baseUrl = "host/openmrs/ws/rest";
SwaggerSpecificationCreator creator = new SwaggerSpecificationCreator(baseUrl);
SwaggerSpecificationCreator creator = new SwaggerSpecificationCreator();

assertTrue(creator.hasSearchHandler("attribute", "location"));

Expand Down Expand Up @@ -124,7 +122,7 @@ public void init() throws Exception {

@Test
public void checkNoDatabaseChanges() throws Exception {
SwaggerSpecificationCreator ssc = new SwaggerSpecificationCreator("/v1/");
SwaggerSpecificationCreator ssc = new SwaggerSpecificationCreator();
ssc.BuildJSON();

Map<String, Integer> afterCounts = getRowCounts();
Expand Down Expand Up @@ -152,7 +150,7 @@ private boolean ensureCountsEqual(Map<String, Integer> beforeCounts, Map<String,
public void checkOperationIdsSet() throws Exception {
List<String> operationIds = new ArrayList<String>();

SwaggerSpecificationCreator ssc = new SwaggerSpecificationCreator("/v1/");
SwaggerSpecificationCreator ssc = new SwaggerSpecificationCreator();
ssc.BuildJSON();
Swagger spec = ssc.getSwagger();

Expand All @@ -167,7 +165,7 @@ public void checkOperationIdsSet() throws Exception {
// makes sure that every GET operation has the "v" parameter
@Test
public void checkRepresentationParamExists() throws Exception {
SwaggerSpecificationCreator ssc = new SwaggerSpecificationCreator("/v1/");
SwaggerSpecificationCreator ssc = new SwaggerSpecificationCreator();
ssc.BuildJSON();
Swagger spec = ssc.getSwagger();

Expand All @@ -194,7 +192,7 @@ private boolean operationHasRepresentationParam(Operation o) {
// make sure each operation that supports paging has the limit and startIndex parameters
@Test
public void checkPagingParamsExist() throws Exception {
SwaggerSpecificationCreator ssc = new SwaggerSpecificationCreator("/v1/");
SwaggerSpecificationCreator ssc = new SwaggerSpecificationCreator();
ssc.BuildJSON();
Swagger spec = ssc.getSwagger();

Expand Down

0 comments on commit bba5380

Please sign in to comment.