Skip to content

Commit

Permalink
RESTWS-904 Support custom representation for Map objects (#602)
Browse files Browse the repository at this point in the history
  • Loading branch information
gayanW authored Jul 29, 2024
1 parent 96a8cb0 commit 70882a4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,21 @@ public void shouldReturnTheAuditInfoForTheFullRepresentation() throws Exception

assertNotNull(PropertyUtils.getProperty(result, "auditInfo"));
}

@Test
public void shouldReturnCustomRepresentationForAuditInfo() throws Exception {
SimpleObject result = deserialize(
handle(newGetRequest(getURI() + "/" + getUuid(),
new Parameter("v", "custom:(auditInfo:(creator:(uuid),dateCreated))"))));

assertNotNull(PropertyUtils.getProperty(result, "auditInfo"));
assertNotNull(PropertyUtils.getProperty(result, "auditInfo.creator"));
assertNotNull(PropertyUtils.getProperty(result, "auditInfo.creator.uuid"));
assertNotNull(PropertyUtils.getProperty(result, "auditInfo.dateCreated"));

assertNull(PropertyUtils.getProperty(result, "name"));
assertNull(PropertyUtils.getProperty(result, "links"));
assertNull(PropertyUtils.getProperty(result, "auditInfo.creator.display"));
assertNull(PropertyUtils.getProperty(result, "auditInfo.creator.links"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ public static <S> Object convertToRepresentation(S o, Representation rep, Conver
}
return ret;
} else if (o instanceof Map) {
if (rep instanceof CustomRepresentation) {
return convertToCustomRepresentation(o, (CustomRepresentation) rep);
}
SimpleObject ret = new SimpleObject();
for (Map.Entry<?, ?> entry : ((Map<?, ?>) o).entrySet()) {
ret.put(entry.getKey().toString(),
Expand All @@ -414,6 +417,24 @@ public static <S> Object convertToRepresentation(S o, Representation rep, Conver
}
}

/**
* Converts an object to its custom representation
* This could be used to convert any domain objects that does not have any specific converter associated with them
* such as SimpleObject's, Map's, etc
*/
private static SimpleObject convertToCustomRepresentation(Object o, CustomRepresentation rep) {
DelegatingResourceDescription drd = ConversionUtil.getCustomRepresentationDescription(rep);

SimpleObject result = new SimpleObject();
for (String propertyName : drd.getProperties().keySet()) {
DelegatingResourceDescription.Property property = drd.getProperties().get(propertyName);
Object propertyValue = ConversionUtil.getPropertyWithRepresentation(o, propertyName, property.getRep());
result.add(propertyName, propertyValue);
}

return result;
}

/**
* Gets the type for the specified generic type variable.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
package org.openmrs.module.webservices.rest.web;

import static org.hamcrest.core.Is.is;
import org.junit.Assert;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

import java.lang.reflect.Method;
Expand All @@ -25,8 +25,11 @@
import java.util.TimeZone;

import org.apache.commons.beanutils.PropertyUtils;
import org.junit.Assert;
import org.junit.Test;
import org.openmrs.api.ConceptNameType;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.web.representation.CustomRepresentation;
import org.openmrs.module.webservices.rest.web.representation.Representation;
import org.openmrs.web.test.BaseModuleWebContextSensitiveTest;

Expand Down Expand Up @@ -145,6 +148,26 @@ public void convert_shouldConvertToAClass() throws Exception {
Assert.assertTrue(converted.isAssignableFrom(String.class));
}

@Test
public void convert_shouldConvertSimpleObjectToCustomRepresentation() throws Exception {

SimpleObject child = new SimpleObject();
child.put("child_key_1", "child_val_1");
child.put("child_key_2", "child_val_2");
SimpleObject parent = new SimpleObject();
parent.put("parent_key_1", child);
parent.put("parent_key_2", "parent_val_2");

Object o = ConversionUtil.convertToRepresentation(parent, new CustomRepresentation("parent_key_1:(child_key_1)"));

SimpleObject expectedChild = new SimpleObject();
expectedChild.put("child_key_1", "child_val_1");
SimpleObject expectedParent = new SimpleObject();
expectedParent.put("parent_key_1", expectedChild);

assertEquals(expectedParent, o);
}

public void convert_shouldConvertIntToDouble() throws Exception {
assertThat((Double) ConversionUtil.convert(5, Double.class), is(5d));
}
Expand Down

0 comments on commit 70882a4

Please sign in to comment.