From 3bb3c3a38344329dcd62f198fc4a91ee64b2325b Mon Sep 17 00:00:00 2001 From: basecade <47134946+basecade@users.noreply.github.com> Date: Wed, 22 May 2019 23:11:03 +0200 Subject: [PATCH] Preserve contained resource meta when encoding to dao (#1311) * Preserve contained resource meta when encoding with: parser.setDontEncodeElements(ResourceMetaParams.EXCLUDE_ELEMENTS_IN_ENCODED) * - Correct so security, lastUpdated and versionId is not encoded - Added test --- .../java/ca/uhn/fhir/parser/BaseParser.java | 10 +++-- .../uhn/fhir/parser/JsonParserDstu3Test.java | 42 ++++++++++++++++++- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/BaseParser.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/BaseParser.java index ca11d69bf8c..776a1d1a151 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/BaseParser.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/BaseParser.java @@ -47,6 +47,8 @@ public abstract class BaseParser implements IParser { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(BaseParser.class); + private static final Set notEncodeForContainedResource = new HashSet<>(Arrays.asList("security", "versionId", "lastUpdated")); + private ContainedResources myContainedResources; private boolean myEncodeElementsAppliesToChildResourcesOnly; private FhirContext myContext; @@ -161,7 +163,7 @@ public abstract class BaseParser implements IParser { */ if (myNext.getDef().getElementName().equals("id")) { myNext = null; - } else if (!myNext.shouldBeEncoded()) { + } else if (!myNext.shouldBeEncoded(theContainedResource)) { myNext = null; } else if (isSummaryMode() && !myNext.getDef().isSummary()) { myNext = null; @@ -176,7 +178,6 @@ public abstract class BaseParser implements IParser { myNext = null; } } - } while (myNext == null); myHasNext = true; @@ -1157,7 +1158,7 @@ public abstract class BaseParser implements IParser { return myParent; } - public boolean shouldBeEncoded() { + public boolean shouldBeEncoded(boolean theContainedResource) { boolean retVal = true; if (myEncodeElements != null) { retVal = checkIfParentShouldBeEncodedAndBuildPath(); @@ -1165,6 +1166,9 @@ public abstract class BaseParser implements IParser { if (retVal && myDontEncodeElements != null) { retVal = !checkIfParentShouldNotBeEncodedAndBuildPath(); } + if (theContainedResource) { + retVal = !notEncodeForContainedResource.contains(myDef.getElementName()); + } return retVal; } diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/JsonParserDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/JsonParserDstu3Test.java index d8db06a4294..808bd102918 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/JsonParserDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/JsonParserDstu3Test.java @@ -24,8 +24,8 @@ import org.hl7.fhir.dstu3.model.*; import org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent; import org.hl7.fhir.dstu3.model.Bundle.BundleType; import org.hl7.fhir.dstu3.model.CapabilityStatement.UnknownContentCode; -import org.hl7.fhir.dstu3.model.Condition.ConditionVerificationStatus; import org.hl7.fhir.dstu3.model.Enumeration; +import org.hl7.fhir.dstu3.model.Condition.ConditionVerificationStatus; import org.hl7.fhir.dstu3.model.Enumerations.AdministrativeGender; import org.hl7.fhir.dstu3.model.Identifier.IdentifierUse; import org.hl7.fhir.dstu3.model.Observation.ObservationStatus; @@ -48,6 +48,7 @@ import static org.junit.Assert.*; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Matchers.isNull; +import static org.mockito.Mockito.nullable; import static org.mockito.Mockito.*; public class JsonParserDstu3Test { @@ -2384,6 +2385,45 @@ public class JsonParserDstu3Test { assertTrue(result.isSuccessful()); } + @Test + public void encodeResourceToString_withEXCLUDE_ELEMENTS_IN_ENCODED_metaKeptInContainedResource() { + // Arrange + Organization containedOrganization = new Organization(); + containedOrganization.getMeta().addProfile(UUID.randomUUID().toString()); + containedOrganization.getMeta().setLastUpdated(new Date()); + containedOrganization.getMeta().setVersionId(UUID.randomUUID().toString()); + containedOrganization.getMeta().setSecurity(Arrays.asList(new Coding(UUID.randomUUID().toString(), UUID.randomUUID().toString(), UUID.randomUUID().toString()))); + containedOrganization.getMeta().setTag(Arrays.asList(new Coding(UUID.randomUUID().toString(), UUID.randomUUID().toString(), UUID.randomUUID().toString()))); + + Patient patient = new Patient(); + patient.setId(UUID.randomUUID().toString()); + patient.getMeta().addProfile(UUID.randomUUID().toString()); + patient.setGeneralPractitioner(Arrays.asList(new Reference(containedOrganization))); + + HashSet excludeElementsInEncoded = new HashSet<>(); // ResourceMetaParams.EXCLUDE_ELEMENTS_IN_ENCODED + excludeElementsInEncoded.add("id"); + excludeElementsInEncoded.add("*.meta"); + + IParser parser = ourCtx.newJsonParser(); + parser.setDontEncodeElements(excludeElementsInEncoded); + + // Act + String encodedPatient = parser.encodeResourceToString(patient); + + // Assert + Patient parsedPatient = (Patient) parser.parseResource(encodedPatient); + assertNull(parsedPatient.getId()); + assertTrue(parsedPatient.getMeta().isEmpty()); + + Resource containedResource = parsedPatient.getContained().get(0); + assertNotNull(containedResource.getMeta()); + assertNull(containedResource.getMeta().getVersionId()); + assertNull(containedResource.getMeta().getLastUpdated()); + assertTrue(containedResource.getMeta().getSecurity().isEmpty()); + assertEquals(1, containedResource.getMeta().getProfile().size()); + assertEquals(1, containedResource.getMeta().getTag().size()); + } + @AfterClass public static void afterClassClearContext() { TestUtil.clearAllStaticFieldsForUnitTest();