From 70882a497f75205e353b437f18a2d330b3b43ec7 Mon Sep 17 00:00:00 2001 From: Gayan Weerakutti Date: Mon, 29 Jul 2024 18:45:37 +0900 Subject: [PATCH] RESTWS-904 Support custom representation for Map objects (#602) --- ...atientIdentifierTypeController1_8Test.java | 17 +++++++++++++ .../webservices/rest/web/ConversionUtil.java | 21 ++++++++++++++++ .../rest/web/ConversionUtilTest.java | 25 ++++++++++++++++++- 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/PatientIdentifierTypeController1_8Test.java b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/PatientIdentifierTypeController1_8Test.java index 9ab4a6c8e..664122971 100644 --- a/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/PatientIdentifierTypeController1_8Test.java +++ b/omod-1.8/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_8/PatientIdentifierTypeController1_8Test.java @@ -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")); + } } diff --git a/omod-common/src/main/java/org/openmrs/module/webservices/rest/web/ConversionUtil.java b/omod-common/src/main/java/org/openmrs/module/webservices/rest/web/ConversionUtil.java index 1d47c9336..044d8c341 100644 --- a/omod-common/src/main/java/org/openmrs/module/webservices/rest/web/ConversionUtil.java +++ b/omod-common/src/main/java/org/openmrs/module/webservices/rest/web/ConversionUtil.java @@ -389,6 +389,9 @@ public static 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(), @@ -414,6 +417,24 @@ public static 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. * diff --git a/omod-common/src/test/java/org/openmrs/module/webservices/rest/web/ConversionUtilTest.java b/omod-common/src/test/java/org/openmrs/module/webservices/rest/web/ConversionUtilTest.java index ea4dd901b..71b3fae12 100644 --- a/omod-common/src/test/java/org/openmrs/module/webservices/rest/web/ConversionUtilTest.java +++ b/omod-common/src/test/java/org/openmrs/module/webservices/rest/web/ConversionUtilTest.java @@ -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; @@ -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; @@ -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)); }