Skip to content

Commit

Permalink
RESTWS-562 Improve Resource Definition Documentation
Browse files Browse the repository at this point in the history
- adds swagger-core library
  * uses its Java API and model classes to construct and generate the swagger json.
- introudce new methods which model objects used in constructing the definitions section:
  * getGETModel(Representation) :  returns a object represting GET representation schema of resource
  * getCREATEModel(Representation) : returns a object representing CREATE representation shema of a resource
  * getUPDATEModel(Representation) : returns a object representing POST Update representation schema of a resource

ex: PersonGet, PersonCreate, PersonUpdate
- adds new test cases to: SwaggerSpecificationCreatorTest.java

- adds support for multiple representation types
GET schemas have support for representations: DEFAULT, REF, and FULL
CREATE schemas have support for representations: DEFAULT, and FULL
POST_UPDATE schemas have support for representations: DEFAULT

  * uses default schema for the shema of responses and parameters
  • Loading branch information
gayanW committed Aug 23, 2017
1 parent 7a39d69 commit b9a970e
Show file tree
Hide file tree
Showing 102 changed files with 3,697 additions and 708 deletions.
1 change: 1 addition & 0 deletions OpenMRSFormatter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -263,5 +263,6 @@
<setting id="org.eclipse.jdt.core.formatter.insert_space_before_opening_angle_bracket_in_type_arguments" value="do not insert"/>
<setting id="org.eclipse.jdt.core.formatter.never_indent_block_comments_on_first_column" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.keep_then_statement_on_same_line" value="false"/>
<setting id="org.eclipse.jdt.core.formatter.join_wrapped_lines" value="false"/>
</profile>
</profiles>
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
import java.util.List;
import java.util.regex.Pattern;

import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import org.openmrs.CareSetting;
import org.openmrs.api.context.Context;
import org.openmrs.module.webservices.docs.swagger.core.property.EnumProperty;
import org.openmrs.module.webservices.rest.web.RequestContext;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.annotation.Resource;
Expand Down Expand Up @@ -67,6 +70,16 @@ public DelegatingResourceDescription getCreatableProperties() {
throw new ResourceDoesNotSupportOperationException();
}

@Override
public Model getGETModel(Representation rep) {
ModelImpl model = (ModelImpl) super.getGETModel(rep);
if (rep instanceof DefaultRepresentation || rep instanceof FullRepresentation) {
model
.property("careSettingType", new EnumProperty(CareSetting.CareSettingType.class));
}
return model;
}

/**
* @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#newDelegate()
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@
import java.util.Date;
import java.util.List;

import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.properties.DateProperty;
import io.swagger.models.properties.RefProperty;
import io.swagger.models.properties.StringProperty;
import org.apache.commons.lang.StringUtils;
import org.openmrs.CareSetting;
import org.openmrs.Order;
import org.openmrs.Patient;
import org.openmrs.api.context.Context;
import org.openmrs.module.webservices.docs.swagger.SwaggerSpecificationCreator;
import org.openmrs.module.webservices.rest.web.ConversionUtil;
import org.openmrs.module.webservices.rest.web.RequestContext;
import org.openmrs.module.webservices.rest.web.RestConstants;
Expand Down Expand Up @@ -102,6 +108,43 @@ public DelegatingResourceDescription getRepresentationDescription(Representation
}
}

@Override
public Model getCREATEModel(Representation rep) {
ModelImpl model = new ModelImpl()
.property("encounter", new StringProperty().example("uuid"))
.property("action", new StringProperty()
._enum(SwaggerSpecificationCreator.getEnumsAsList(Order.Action.class)))
.property("accessionNumber", new StringProperty())
.property("dateActivated", new DateProperty())
.property("scheduledDate", new DateProperty())
.property("patient", new StringProperty().example("uuid"))
.property("concept", new StringProperty().example("uuid"))
.property("careSetting", new StringProperty().example("uuid"))
.property("dateStopped", new DateProperty())
.property("autoExpireDate", new DateProperty())
.property("orderer", new StringProperty().example("uuid"))
.property("previousOrder", new StringProperty().example("uuid"))
.property("urgency", new StringProperty()
._enum(SwaggerSpecificationCreator.getEnumsAsList(Order.Urgency.class)))
.property("orderReason", new StringProperty().example("uuid"))
.property("orderReasonNonCoded", new StringProperty())
.property("instructions", new StringProperty())
.property("commentToFulfiller", new StringProperty())

.required("orderType").required("patient").required("concept");
if (rep instanceof FullRepresentation) {
model
.property("encounter", new RefProperty("#/definitions/EncounterCreate"))
.property("patient", new RefProperty("#/definitions/PatientCreate"))
.property("concept", new RefProperty("#/definitions/ConceptCreate"))
.property("orderer", new RefProperty("#/definitions/UserCreate"))
.property("previousOrder", new RefProperty("#/definitions/OrderCreate"))
.property("orderReason", new RefProperty("#/definitions/ConceptCreate"));
}
//FIXME missing prop: type
return model;
}

/**
* @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getCreatableProperties()
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
import java.util.List;
import java.util.regex.Pattern;

import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.RefProperty;
import io.swagger.models.properties.StringProperty;
import org.openmrs.OrderType;
import org.openmrs.api.context.Context;
import org.openmrs.module.webservices.rest.web.RequestContext;
Expand Down Expand Up @@ -146,4 +151,33 @@ public DelegatingResourceDescription getCreatableProperties() {
d.addProperty("conceptClasses");
return d;
}

@Override
public Model getGETModel(Representation rep) {
ModelImpl model = (ModelImpl) super.getGETModel(rep);
if (rep instanceof DefaultRepresentation || rep instanceof FullRepresentation) {
model
.property("javaClassName", new StringProperty());
}
if (rep instanceof DefaultRepresentation) {
model
.property("conceptClasses", new ArrayProperty(new RefProperty("#/definitions/ConceptclassGetRef")))
.property("parent", new RefProperty("#/definitions/OrdertypeGetRef"));
} else if (rep instanceof FullRepresentation) {
model
.property("conceptClasses", new ArrayProperty(new RefProperty("#/definitions/ConceptclassGet")))
.property("parent", new RefProperty("#/definitions/OrdertypeGet"));
}
return model;
}

@Override
public Model getCREATEModel(Representation rep) {
return ((ModelImpl) super.getCREATEModel(rep))
.property("javaClassName", new StringProperty())
.property("parent", new StringProperty().example("uuid")) //FIXME type
.property("conceptClasses", new ArrayProperty(new StringProperty().example("uuid")))

.required("javaClassName");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.util.Collection;
import java.util.List;
import java.util.Locale;

import io.swagger.models.Model;
import org.openmrs.ConceptClass;
import org.openmrs.ConceptName;
import org.openmrs.ConceptSearchResult;
Expand Down Expand Up @@ -184,6 +186,11 @@ public DelegatingResourceDescription getRepresentationDescription(Representation
return description;
}

@Override
public Model getCREATEModel(Representation rep) {
return null;
}

@PropertyGetter("display")
public String getDisplayString(ConceptSearchResult csr) {
ConceptName cn = csr.getConcept().getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
*/
package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_10;

import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.properties.ArrayProperty;
import io.swagger.models.properties.BooleanProperty;
import io.swagger.models.properties.DateProperty;
import io.swagger.models.properties.RefProperty;
import org.openmrs.PatientProgram;
import org.openmrs.PatientState;
import org.openmrs.api.context.Context;
Expand Down Expand Up @@ -93,6 +99,32 @@ public DelegatingResourceDescription getCreatableProperties() {
return d;
}

@Override
public Model getGETModel(Representation rep) {
return super.getGETModel(rep);
}

@Override
public Model getCREATEModel(Representation rep) {
return ((ModelImpl) super.getCREATEModel(rep))
.property("states", new ArrayProperty(new RefProperty("#/definitions/ProgramenrollmentStateCreate")))
.property("outcome", new RefProperty("#/definitions/ConceptCreate"));
}

@Override
public Model getUPDATEModel(Representation rep) {
return new ModelImpl() //FIXME use super.
.property("dateEnrolled", new DateProperty())
.property("states", new ArrayProperty(new RefProperty("#/definitions/ProgramenrollmentStateCreate")))
.property("outcome", new RefProperty("#/definitions/ConceptCreate"))
.property("location", new RefProperty("#/definitions/LocationCreate"))
.property("voided", new BooleanProperty())
.property("dateCompleted", new DateProperty())

.required("dateEnrolled");

}

@Override
public DelegatingResourceDescription getUpdatableProperties() {
DelegatingResourceDescription d = new DelegatingResourceDescription();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
*/
package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_10;

import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.properties.RefProperty;
import io.swagger.models.properties.StringProperty;
import org.openmrs.Program;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.annotation.Resource;
Expand Down Expand Up @@ -60,6 +64,17 @@ public DelegatingResourceDescription getRepresentationDescription(Representation
return null;
}

@Override
public Model getCREATEModel(Representation rep) {
ModelImpl model = ((ModelImpl) super.getCREATEModel(rep))
.property("outcomesConcept", new StringProperty().example("uuid"));
if (rep instanceof FullRepresentation) {
model
.property("outcomesConcept", new RefProperty("#/definitions/ConceptCreate"));
}
return model;
}

@Override
public DelegatingResourceDescription getCreatableProperties() {
DelegatingResourceDescription description = new DelegatingResourceDescription();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.util.List;

import io.swagger.models.Model;
import org.apache.commons.lang.StringUtils;
import org.openmrs.CareSetting;
import org.openmrs.Order;
Expand Down Expand Up @@ -123,6 +124,21 @@ public DelegatingResourceDescription getUpdatableProperties() throws ResourceDoe
return orderResource.getUpdatableProperties();
}

@Override
public Model getGETModel(Representation representation) {
return null;
}

@Override
public Model getCREATEModel(Representation representation) {
return null;
}

@Override
public Model getUPDATEModel(Representation representation) {
return null;
}

public PageableResult getActiveOrders(Patient patient, RequestContext context) {
String careSettingUuid = context.getRequest().getParameter("careSetting");
String asOfDateString = context.getRequest().getParameter("asOfDate");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.ArrayList;
import java.util.List;

import io.swagger.models.Model;
import org.openmrs.Drug;
import org.openmrs.DrugIngredient;
import org.openmrs.api.context.Context;
Expand Down Expand Up @@ -80,6 +81,21 @@ public DelegatingResourceDescription getUpdatableProperties() {
return getCreatableProperties();
}

@Override
public Model getGETModel(Representation representation) {
return null;
}

@Override
public Model getCREATEModel(Representation representation) {
return null;
}

@Override
public Model getUPDATEModel(Representation representation) {
return null;
}

/**
* @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingSubResource#getParent(java.lang.Object)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_11;

import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.properties.StringProperty;
import org.openmrs.Obs;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.annotation.Resource;
Expand All @@ -35,6 +38,20 @@ public DelegatingResourceDescription getRepresentationDescription(Representation
return description;
}

@Override
public Model getGETModel(Representation rep) {
return ((ModelImpl) super.getGETModel(rep))
.property("formFieldPath", new StringProperty())
.property("formFieldNamespace", new StringProperty());
}

@Override
public Model getCREATEModel(Representation rep) {
return ((ModelImpl) super.getCREATEModel(rep))
.property("formFieldPath", new StringProperty())
.property("formFieldNamespace", new StringProperty());
}

/**
* @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getCreatableProperties()
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@
*/
package org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_11;

import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.properties.BooleanProperty;
import io.swagger.models.properties.DateTimeProperty;
import org.openmrs.Person;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.annotation.Resource;
import org.openmrs.module.webservices.rest.web.representation.DefaultRepresentation;
import org.openmrs.module.webservices.rest.web.representation.FullRepresentation;
import org.openmrs.module.webservices.rest.web.representation.Representation;
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource;
import org.openmrs.module.webservices.rest.web.resource.impl.DelegatingResourceDescription;
Expand Down Expand Up @@ -61,6 +67,30 @@ public DelegatingResourceDescription getUpdatableProperties() throws ResourceDoe
return description;
}

@Override
public Model getGETModel(Representation rep) {
return addNewProperties(super.getGETModel(rep), rep);
}

@Override
public Model getCREATEModel(Representation rep) {
return addNewProperties(super.getCREATEModel(rep), rep);
}

@Override
public Model getUPDATEModel(Representation rep) {
return addNewProperties(super.getUPDATEModel(rep), rep);
}

private Model addNewProperties(Model model, Representation rep) {
if (rep instanceof DefaultRepresentation || rep instanceof FullRepresentation) {
((ModelImpl) model)
.property("deathdateEstimated", new BooleanProperty()._default(false))
.property("birthtime", new DateTimeProperty());
}
return model;
}

/**
* @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getResourceVersion()
*/
Expand Down
Loading

0 comments on commit b9a970e

Please sign in to comment.