Skip to content

Commit

Permalink
RESTWS-663 - Fix SwaggerSpecificationCreator#hasSearchHandler(resName)
Browse files Browse the repository at this point in the history
* Fix invalid logic in SwaggerSpecificationCreator#hasSearchHandler(resourceName)
* Remove undeclared methods
* Remove unnecessary test case SwaggerSpecificationCreatorTest#swaggerSerializeTest()
  • Loading branch information
gayanW committed Sep 7, 2017
1 parent d47bf26 commit 91607d0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.openmrs.module.Module;
import org.openmrs.module.ModuleFactory;
import org.openmrs.module.webservices.docs.SearchHandlerDoc;
import org.openmrs.module.webservices.docs.SearchQueryDoc;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.web.RequestContext;
import org.openmrs.module.webservices.rest.web.RestConstants;
Expand Down Expand Up @@ -77,8 +76,6 @@ public class SwaggerSpecificationCreator {

private String baseUrl;

private static List<SearchHandlerDoc> searchHandlerDocs;

PrintStream originalErr;

PrintStream originalOut;
Expand All @@ -89,8 +86,6 @@ public class SwaggerSpecificationCreator {

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

public String BuildJSON() {
Expand Down Expand Up @@ -590,7 +585,7 @@ private void addSearchOperations(DelegatingResourceHandler<?> resourceHandler, S
return;
}
boolean hasDoSearch = testOperationImplemented(OperationEnum.getWithDoSearch, resourceHandler);
boolean hasSearchHandler = hasSearchHandler(resourceName);
boolean hasSearchHandler = hasSearchHandler(resourceName, resourceParentName);
boolean wasNew = false;

if (hasSearchHandler || hasDoSearch) {
Expand Down Expand Up @@ -806,38 +801,6 @@ private void addSubclassOperations() {
}
}

@Deprecated
private List<org.openmrs.module.webservices.docs.swagger.Parameter> getParametersListForSearchHandlers(
String resourceName, String searchHandlerId, int queryIndex) {
List<org.openmrs.module.webservices.docs.swagger.Parameter> parameters = new ArrayList<org.openmrs.module.webservices.docs.swagger.Parameter>();
String resourceURL = getResourceUrl(getBaseUrl(), resourceName);
for (SearchHandlerDoc searchDoc : searchHandlerDocs) {
if (searchDoc.getSearchHandlerId().equals(searchHandlerId) && searchDoc.getResourceURL().equals(resourceURL)) {
SearchQueryDoc queryDoc = searchDoc.getSearchQueriesDoc().get(queryIndex);
for (SearchParameter requiredParameter : queryDoc.getRequiredParameters()) {
org.openmrs.module.webservices.docs.swagger.Parameter parameter = new org.openmrs.module.webservices.docs.swagger.Parameter();
parameter.setName(requiredParameter.getName());
parameter.setIn("query");
parameter.setDescription("");
parameter.setRequired(true);
parameters.add(parameter);
}
for (SearchParameter optionalParameter : queryDoc.getOptionalParameters()) {
org.openmrs.module.webservices.docs.swagger.Parameter parameter = new org.openmrs.module.webservices.docs.swagger.Parameter();
parameter.setName(optionalParameter.getName());
parameter.setIn("query");
parameter.setDescription("");
parameter.setRequired(false);
parameters.add(parameter);
}

break;
}
}
return parameters;

}

private String createJSON() {
return Json.pretty(swagger);
}
Expand Down Expand Up @@ -1164,24 +1127,22 @@ private static List<SearchHandlerDoc> fillSearchHandlers(List<SearchHandler> sea
return searchHandlerDocList;
}

private String getResourceUrl(String baseUrl, String resourceName) {
//Set the root url.
return baseUrl + "/v1/" + resourceName;
}

private boolean hasSearchHandler(String resourceName) {
for (SearchHandlerDoc doc : searchHandlerDocs) {
if (doc.getResourceURL().contains(resourceName)) {
private boolean hasSearchHandler(String resourceName, String resourceParentName) {
if (resourceParentName != null) {
resourceName = RestConstants.VERSION_1 + "/" + resourceParentName + "/" + resourceName;
} else {
resourceName = RestConstants.VERSION_1 + "/" + resourceName;
}

List<SearchHandler> searchHandlers = Context.getService(RestService.class).getAllSearchHandlers();
for (SearchHandler searchHandler : searchHandlers) {
if (searchHandler.getSearchConfig().getSupportedResource().equals(resourceName)) {
return true;
}
}
return false;
}

public String getBaseUrl() {
return baseUrl;
}

public Swagger getSwagger() {
return swagger;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,14 @@
*/
package org.openmrs.module.webservices.rest.doc;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.converter.ModelConverterContextImpl;
import io.swagger.converter.ModelConverters;
import io.swagger.jackson.ModelResolver;
import io.swagger.models.Info;
import io.swagger.models.Model;
import io.swagger.models.Operation;
import io.swagger.models.Path;
import io.swagger.models.Scheme;
import io.swagger.models.Swagger;
import io.swagger.models.auth.BasicAuthDefinition;
import io.swagger.models.parameters.Parameter;
import io.swagger.util.Json;
import org.dbunit.database.DatabaseConnection;
import org.junit.Assert;
import org.junit.Before;
Expand All @@ -47,6 +41,8 @@

public class SwaggerSpecificationCreatorTest extends BaseModuleWebContextSensitiveTest {

Map<String, Integer> beforeCounts;

@Test
public void mainTest() {
String baseUrl = "host/openmrs/ws/rest";
Expand All @@ -62,22 +58,6 @@ public void modelResolveTest() {
assertNotNull(model);
}

@Test
public void swaggerSerializeTest() throws JsonProcessingException {
final Info info = new Info().version("1.0.0").title("Swagger WebServices REST");

Swagger swagger = new Swagger().info(info).securityDefinition("basicAuth", new BasicAuthDefinition())
.scheme(Scheme.HTTP).consumes("application/json").produces("application/json");

final Model patientModel = ModelConverters.getInstance().read(Patient.class).get("Patient");
swagger.addDefinition("Patient", patientModel);

final String swaggerJson = Json.pretty(swagger);
assertNotNull(swaggerJson);
}

Map<String, Integer> beforeCounts;

public Map<String, Integer> getRowCounts() throws Exception {
Map<String, Integer> ret = new HashMap<String, Integer>();

Expand Down

0 comments on commit 91607d0

Please sign in to comment.