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(); diff --git a/pom.xml b/pom.xml index 0fabfa4b773..f2dcc835ef6 100755 --- a/pom.xml +++ b/pom.xml @@ -506,6 +506,10 @@ zilin375 + + basecade + Anders Havn + diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 982466f8f1b..1b9c41686d9 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -217,6 +217,10 @@ The HAPI FHIR CLI was unable to start a server in R4 mode in HAPI FHIR 3.7.0. This has been corrected. + + When encoding resources, profile declarations on contained resources will now be + preserved. Thanks to Anders Havn for the pull request! +