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
This commit is contained in:
basecade 2019-05-22 23:11:03 +02:00 committed by James Agnew
parent 202c798fcd
commit 3bb3c3a383
2 changed files with 48 additions and 4 deletions

View File

@ -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<String> 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;
}

View File

@ -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<String> 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();