Add mode to parser to force it to not encode certain elements when serializing

This commit is contained in:
jamesagnew 2016-03-16 07:53:21 -04:00
parent 2ae2cbba23
commit 9aa7eea274
11 changed files with 456 additions and 52 deletions

View File

@ -91,6 +91,10 @@ public abstract class BaseParser implements IParser {
private boolean mySummaryMode;
private boolean mySuppressNarratives;
private Set<String> myDontEncodeElements;
private boolean myDontEncodeElementsIncludesStars;
/**
* Constructor
*
@ -605,6 +609,21 @@ public abstract class BaseParser implements IParser {
return theValues;
}
@Override
public void setDontEncodeElements(Set<String> theDontEncodeElements) {
myDontEncodeElementsIncludesStars = false;
if (theDontEncodeElements == null || theDontEncodeElements.isEmpty()) {
myDontEncodeElements = null;
} else {
myDontEncodeElements = theDontEncodeElements;
for (String next : theDontEncodeElements) {
if (next.startsWith("*.")) {
myDontEncodeElementsIncludesStars = true;
}
}
}
}
@Override
public void setEncodeElements(Set<String> theEncodeElements) {
myEncodeElementsIncludesStars = false;
@ -676,6 +695,38 @@ public abstract class BaseParser implements IParser {
return isSummaryMode() || isSuppressNarratives();
}
protected boolean shouldEncodeResourceId(IBaseResource theResource) {
boolean retVal = true;
if (isOmitResourceId()) {
retVal = false;
} else {
if (myDontEncodeElements != null) {
String resourceName = myContext.getResourceDefinition(theResource).getName();
if (myDontEncodeElements.contains(resourceName + ".id")) {
retVal = false;
} else if (myDontEncodeElements.contains("*.id")) {
retVal = false;
}
}
}
return retVal;
}
/**
* Used for DSTU2 only
*/
protected boolean shouldEncodeResourceMeta(IResource theResource) {
if (myDontEncodeElements != null) {
String resourceName = myContext.getResourceDefinition(theResource).getName();
if (myDontEncodeElements.contains(resourceName + ".meta")) {
return false;
} else if (myDontEncodeElements.contains("*.meta")) {
return false;
}
}
return true;
}
private String subsetDescription() {
return "Resource encoded in summary mode";
}
@ -770,32 +821,46 @@ public abstract class BaseParser implements IParser {
}
}
private boolean checkIfParentShouldBeEncodedAndBuildPath(StringBuilder theB, boolean theStarPass) {
private boolean checkIfParentShouldBeEncodedAndBuildPath(StringBuilder thePathBuilder, boolean theStarPass) {
return checkIfPathMatches(thePathBuilder, theStarPass, myEncodeElementsAppliesToResourceTypes, myEncodeElements, true);
}
private boolean checkIfParentShouldNotBeEncodedAndBuildPath(StringBuilder thePathBuilder, boolean theStarPass) {
return checkIfPathMatches(thePathBuilder, theStarPass, null, myDontEncodeElements, false);
}
private boolean checkIfPathMatches(StringBuilder thePathBuilder, boolean theStarPass, Set<String> theResourceTypes, Set<String> theElements, boolean theCheckingForWhitelist) {
if (myResDef != null) {
if (myEncodeElementsAppliesToResourceTypes != null) {
if (!myEncodeElementsAppliesToResourceTypes.contains(myResDef.getName())) {
if (theResourceTypes != null) {
if (!theResourceTypes.contains(myResDef.getName())) {
return true;
}
}
if (theStarPass) {
theB.append('*');
thePathBuilder.append('*');
} else {
theB.append(myResDef.getName());
thePathBuilder.append(myResDef.getName());
}
if (myEncodeElements.contains(theB.toString())) {
if (theElements.contains(thePathBuilder.toString())) {
return true;
} else {
return false;
}
} else if (myParent != null) {
if (myParent.checkIfParentShouldBeEncodedAndBuildPath(theB, theStarPass)) {
boolean parentCheck;
if (theCheckingForWhitelist) {
parentCheck = myParent.checkIfParentShouldBeEncodedAndBuildPath(thePathBuilder, theStarPass);
} else {
parentCheck = myParent.checkIfParentShouldNotBeEncodedAndBuildPath(thePathBuilder, theStarPass);
}
if (parentCheck) {
return true;
}
if (myDef != null) {
theB.append('.');
theB.append(myDef.getElementName());
return myEncodeElements.contains(theB.toString());
thePathBuilder.append('.');
thePathBuilder.append(myDef.getElementName());
return theElements.contains(thePathBuilder.toString());
}
}
@ -815,12 +880,18 @@ public abstract class BaseParser implements IParser {
}
public boolean shouldBeEncoded() {
if (myEncodeElements == null) {
return true;
boolean retVal = true;
if (myEncodeElements != null) {
retVal = checkIfParentShouldBeEncodedAndBuildPath(new StringBuilder(), false);
if (retVal == false && myEncodeElementsIncludesStars) {
retVal = checkIfParentShouldBeEncodedAndBuildPath(new StringBuilder(), true);
}
}
boolean retVal = checkIfParentShouldBeEncodedAndBuildPath(new StringBuilder(), false);
if (retVal == false && myEncodeElementsIncludesStars) {
retVal = checkIfParentShouldBeEncodedAndBuildPath(new StringBuilder(), true);
if (retVal && myDontEncodeElements != null) {
retVal = !checkIfParentShouldNotBeEncodedAndBuildPath(new StringBuilder(), false);
if (retVal && myDontEncodeElementsIncludesStars) {
retVal = !checkIfParentShouldNotBeEncodedAndBuildPath(new StringBuilder(), true);
}
}
return retVal;
}

View File

@ -215,18 +215,41 @@ public interface IParser {
*/
TagList parseTagList(String theString);
/**
* If provided, specifies the elements which should NOT be encoded. Valid values for this
* field would include:
* <ul>
* <li><b>Patient</b> - Don't encode patient and all its children</li>
* <li><b>Patient.name</b> - Don't encode the patient's name</li>
* <li><b>Patient.name.family</b> - Don't encode the patient's family name</li>
* <li><b>*.text</b> - Don't encode the text element on any resource (only the very first position may contain a wildcard)</li>
* </ul>
* <p>
* DSTU2 note: Note that values including meta, such as <code>Patient.meta</code>
* will work for DSTU2 parsers, but values with subelements on meta such
* as <code>Patient.meta.lastUpdated</code> will only work in
* DSTU3+ mode.
* </p>
*
* @param theDontEncodeElements
* The elements to encode
* @see #setEncodeElements(Set)
*/
void setDontEncodeElements(Set<String> theDontEncodeElements);
/**
* If provided, specifies the elements which should be encoded, to the exclusion of all others. Valid values for this
* field would include:
* <ul>
* <li><b>Patient</b> - Encode patient and all its children</li>
* <li><b>Patient.name</b> - Encoding only the patient's name</li>
* <li><b>Patient.name</b> - Encode only the patient's name</li>
* <li><b>Patient.name.family</b> - Encode only the patient's family name</li>
* <li><b>*.text</b> - Encode the text element on any resource (only the very first position may contain a wildcard)</li>
* </ul>
*
* @param theEncodeElements
* The elements to encode
* @see #setDontEncodeElements(Set)
*/
void setEncodeElements(Set<String> theEncodeElements);

View File

@ -800,7 +800,7 @@ public class JsonParser extends BaseParser implements IParser {
}
if (!theContainedResource) {
if (isOmitResourceId()) {
if (super.shouldEncodeResourceId(theResource) == false) {
resourceId = null;
} else if (getEncodeForceResourceId() != null) {
resourceId = getEncodeForceResourceId();
@ -847,7 +847,7 @@ public class JsonParser extends BaseParser implements IParser {
versionIdPart = ResourceMetadataKeyEnum.VERSION.get(resource);
}
if (ElementUtil.isEmpty(versionIdPart, updated, securityLabels, tags, profiles) == false) {
if (super.shouldEncodeResourceMeta(resource) && ElementUtil.isEmpty(versionIdPart, updated, securityLabels, tags, profiles) == false) {
theEventWriter.writeStartObject("meta");
writeOptionalTagWithTextNode(theEventWriter, "versionId", versionIdPart);
writeOptionalTagWithTextNode(theEventWriter, "lastUpdated", updated);

View File

@ -787,7 +787,7 @@ public class XmlParser extends BaseParser implements IParser {
}
if (!theIncludedResource) {
if (isOmitResourceId()) {
if (super.shouldEncodeResourceId(theResource) == false) {
resourceId = null;
} else if (getEncodeForceResourceId() != null) {
resourceId = getEncodeForceResourceId();
@ -844,7 +844,7 @@ public class XmlParser extends BaseParser implements IParser {
List<IdDt> profiles = extractMetadataListNotNull(resource, ResourceMetadataKeyEnum.PROFILES);
TagList tags = getMetaTagsForEncoding((resource));
if (ElementUtil.isEmpty(versionIdPart, updated, securityLabels, tags, profiles) == false) {
if (super.shouldEncodeResourceMeta(resource) && ElementUtil.isEmpty(versionIdPart, updated, securityLabels, tags, profiles) == false) {
theEventWriter.writeStartElement("meta");
writeOptionalTagWithValue(theEventWriter, "versionId", versionIdPart);
if (updated != null) {

View File

@ -68,6 +68,7 @@ import org.hl7.fhir.instance.model.api.IPrimitiveType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.PlatformTransactionManager;
import com.google.common.base.Charsets;
import com.google.common.collect.ArrayListMultimap;
import ca.uhn.fhir.context.BaseRuntimeChildDefinition;
@ -720,17 +721,13 @@ public abstract class BaseHapiFhirDao<T extends IBaseResource> implements IDao {
ResourceEncodingEnum encoding = myConfig.getResourceEncoding();
theEntity.setEncoding(encoding);
theEntity.setFhirVersion(myContext.getVersion().getVersion());
try {
switch (encoding) {
case JSON:
theEntity.setResource(encoded.getBytes("UTF-8"));
break;
case JSONC:
theEntity.setResource(GZipUtil.compress(encoded));
break;
}
} catch (UnsupportedEncodingException e) {
throw new InternalErrorException(e);
switch (encoding) {
case JSON:
theEntity.setResource(encoded.getBytes(Charsets.UTF_8));
break;
case JSONC:
theEntity.setResource(GZipUtil.compress(encoded));
break;
}
Set<TagDefinition> allDefs = new HashSet<TagDefinition>();
@ -1119,16 +1116,6 @@ public abstract class BaseHapiFhirDao<T extends IBaseResource> implements IDao {
theEntity.setPublished(theUpdateTime);
}
// if (theUpdateHistory) {
// Long existingId = theEntity.getId();
// ResourceHistoryTable existingHistory = existingId != null ? myResourceHistoryTableDao.findForIdAndVersion(existingId, theEntity.getVersion()) : null;
// final ResourceHistoryTable historyEntry = theEntity.toHistory(existingHistory);
//
// ourLog.info("Saving history entry for update {}", historyEntry.getIdDt());
// myResourceHistoryTableDao.save(historyEntry);
//
// }
if (theUpdateVersion) {
theEntity.setVersion(theEntity.getVersion() + 1);
}

View File

@ -31,6 +31,7 @@ import org.hl7.fhir.dstu3.model.Resource;
import org.hl7.fhir.dstu3.model.UriType;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
@ -277,6 +278,25 @@ public class FhirResourceDaoDstu3UpdateTest extends BaseJpaDstu3Test {
}
@Test
@Ignore
public void testUpdateIgnoresIdenticalVersions() throws InterruptedException {
String methodName = "testUpdateIgnoresIdenticalVersions";
Patient p1 = new Patient();
p1.addIdentifier().setSystem("urn:system").setValue(methodName);
p1.addName().addFamily("Tester").addGiven(methodName);
IIdType p1id = myPatientDao.create(p1, new ServletRequestDetails()).getId();
IIdType p1id2 = myPatientDao.update(p1, mySrd).getId();
assertEquals(p1id.getValue(), p1id2.getValue());
p1.addName().addGiven("NewGiven");
IIdType p1id3 = myPatientDao.update(p1, mySrd).getId();
assertNotEquals(p1id.getValue(), p1id3.getValue());
}
@Test
public void testDuplicateProfilesIgnored() {
String name = "testDuplicateProfilesIgnored";

View File

@ -16,6 +16,8 @@ import static org.mockito.Mockito.verify;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;
@ -27,6 +29,8 @@ import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.mockito.internal.stubbing.answers.ThrowsException;
import com.google.common.collect.Sets;
import ca.uhn.fhir.context.ConfigurationException;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.Bundle;
@ -71,6 +75,7 @@ public class JsonParserDstu2Test {
private static final FhirContext ourCtx = FhirContext.forDstu2();
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(JsonParserDstu2Test.class);
/**
* See #308
*/
@ -510,6 +515,77 @@ public class JsonParserDstu2Test {
assertThat(json, not(containsString("\"id\"")));
}
@Test
public void testEncodeWithDontEncodeElements() throws Exception {
Patient patient = new Patient();
patient.setId("123");
ArrayList<IdDt> list = new ArrayList<IdDt>();
list.add(new IdDt("http://profile"));
ResourceMetadataKeyEnum.PROFILES.put(patient, list);
patient.addName().addFamily("FAMILY").addGiven("GIVEN");
patient.addAddress().addLine("LINE1");
{
IParser p = ourCtx.newJsonParser();
p.setDontEncodeElements(Sets.newHashSet("*.meta", "*.id"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("address"));
assertThat(out, not(containsString("id")));
assertThat(out, not(containsString("meta")));
}
{
IParser p = ourCtx.newJsonParser();
p.setDontEncodeElements(Sets.newHashSet("Patient.meta", "Patient.id"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("address"));
assertThat(out, not(containsString("id")));
assertThat(out, not(containsString("meta")));
}
{
IParser p = ourCtx.newJsonParser();
p.setDontEncodeElements(Sets.newHashSet("Patient.name.family"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("GIVEN"));
assertThat(out, not(containsString("FAMILY")));
}
{
IParser p = ourCtx.newJsonParser();
p.setDontEncodeElements(Sets.newHashSet("*.meta", "*.id"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("address"));
assertThat(out, not(containsString("id")));
assertThat(out, not(containsString("meta")));
}
{
IParser p = ourCtx.newJsonParser();
p.setDontEncodeElements(Sets.newHashSet("Patient.meta"));
p.setEncodeElements(new HashSet<String>(Arrays.asList("Patient.name")));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("id"));
assertThat(out, not(containsString("address")));
assertThat(out, not(containsString("meta")));
}
}
@Test
public void testEncodingNullExtension() {
Patient p = new Patient();

View File

@ -38,6 +38,8 @@ import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.internal.stubbing.answers.ThrowsException;
import com.google.common.collect.Sets;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.Bundle;
import ca.uhn.fhir.model.api.ExtensionDt;
@ -1158,6 +1160,78 @@ public class XmlParserDstu2Test {
}
@Test
public void testEncodeWithDontEncodeElements() throws Exception {
Patient patient = new Patient();
patient.setId("123");
ArrayList<IdDt> list = new ArrayList<IdDt>();
list.add(new IdDt("http://profile"));
ResourceMetadataKeyEnum.PROFILES.put(patient, list);
patient.addName().addFamily("FAMILY").addGiven("GIVEN");
patient.addAddress().addLine("LINE1");
{
IParser p = ourCtx.newXmlParser();
p.setDontEncodeElements(Sets.newHashSet("*.meta", "*.id"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("address"));
assertThat(out, not(containsString("id")));
assertThat(out, not(containsString("meta")));
}
{
IParser p = ourCtx.newXmlParser();
p.setDontEncodeElements(Sets.newHashSet("Patient.meta", "Patient.id"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("address"));
assertThat(out, not(containsString("id")));
assertThat(out, not(containsString("meta")));
}
{
IParser p = ourCtx.newXmlParser();
p.setDontEncodeElements(Sets.newHashSet("Patient.name.family"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("GIVEN"));
assertThat(out, not(containsString("FAMILY")));
}
{
IParser p = ourCtx.newXmlParser();
p.setDontEncodeElements(Sets.newHashSet("*.meta", "*.id"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("address"));
assertThat(out, not(containsString("id")));
assertThat(out, not(containsString("meta")));
}
{
IParser p = ourCtx.newXmlParser();
p.setDontEncodeElements(Sets.newHashSet("Patient.meta"));
p.setEncodeElements(new HashSet<String>(Arrays.asList("Patient.name")));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("id"));
assertThat(out, not(containsString("address")));
assertThat(out, not(containsString("meta")));
}
}
@Test
public void testMoreExtensions() throws Exception {

View File

@ -14,6 +14,8 @@ import static org.junit.Assert.fail;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;
@ -54,6 +56,8 @@ import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import com.google.common.collect.Sets;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator;
import ca.uhn.fhir.rest.server.Constants;
@ -70,15 +74,6 @@ public class JsonParserDstu3Test {
ourCtx.setNarrativeGenerator(null);
}
@Test
public void testLinkage() {
Linkage l = new Linkage();
l.addItem().getResource().setDisplay("FOO");
String out = ourCtx.newXmlParser().encodeResourceToString(l);
ourLog.info(out);
assertEquals("<Linkage xmlns=\"http://hl7.org/fhir\"><item><resource><display value=\"FOO\"/></resource></item></Linkage>", out);
}
@Test
public void testEncodeAndParseExtensions() throws Exception {
@ -241,6 +236,7 @@ public class JsonParserDstu3Test {
assertEquals("sec_label2", tagList.get(1).getDisplay());
}
@Test
public void testEncodeAndParseSecurityLabels() {
Patient p = new Patient();
@ -300,7 +296,7 @@ public class JsonParserDstu3Test {
assertEquals("DISPLAY2", label.getDisplay());
assertEquals("VERSION2", label.getVersion());
}
@Test
public void testEncodeBundleNewBundleNoText() {
@ -474,6 +470,75 @@ public class JsonParserDstu3Test {
assertThat(json, not(containsString("\"id\"")));
}
@Test
public void testEncodeWithDontEncodeElements() throws Exception {
Patient patient = new Patient();
patient.setId("123");
patient.getMeta().addProfile(("http://profile"));
patient.addName().addFamily("FAMILY").addGiven("GIVEN");
patient.addAddress().addLine("LINE1");
{
IParser p = ourCtx.newJsonParser();
p.setDontEncodeElements(Sets.newHashSet("*.meta", "*.id"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("address"));
assertThat(out, not(containsString("id")));
assertThat(out, not(containsString("meta")));
}
{
IParser p = ourCtx.newJsonParser();
p.setDontEncodeElements(Sets.newHashSet("Patient.meta", "Patient.id"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("address"));
assertThat(out, not(containsString("id")));
assertThat(out, not(containsString("meta")));
}
{
IParser p = ourCtx.newJsonParser();
p.setDontEncodeElements(Sets.newHashSet("Patient.name.family"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("GIVEN"));
assertThat(out, not(containsString("FAMILY")));
}
{
IParser p = ourCtx.newJsonParser();
p.setDontEncodeElements(Sets.newHashSet("*.meta", "*.id"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("address"));
assertThat(out, not(containsString("id")));
assertThat(out, not(containsString("meta")));
}
{
IParser p = ourCtx.newJsonParser();
p.setDontEncodeElements(Sets.newHashSet("Patient.meta"));
p.setEncodeElements(new HashSet<String>(Arrays.asList("Patient.name")));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("id"));
assertThat(out, not(containsString("address")));
assertThat(out, not(containsString("meta")));
}
}
@Test
public void testEncodeWithNarrative() {
Patient p = new Patient();
@ -547,6 +612,15 @@ public class JsonParserDstu3Test {
}
@Test
public void testLinkage() {
Linkage l = new Linkage();
l.addItem().getResource().setDisplay("FOO");
String out = ourCtx.newXmlParser().encodeResourceToString(l);
ourLog.info(out);
assertEquals("<Linkage xmlns=\"http://hl7.org/fhir\"><item><resource><display value=\"FOO\"/></resource></item></Linkage>", out);
}
// FIXME: this should pass
@Test
@Ignore

View File

@ -85,6 +85,8 @@ import org.junit.Ignore;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import com.google.common.collect.Sets;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.narrative.DefaultThymeleafNarrativeGenerator;
import ca.uhn.fhir.parser.IParserErrorHandler.IParseLocation;
@ -101,6 +103,8 @@ public class XmlParserDstu3Test {
}
@Test
public void testBundleWithBinary() {
//@formatter:off
@ -1018,9 +1022,10 @@ public class XmlParserDstu3Test {
assertThat(encoded, not(containsString("maritalStatus")));
}
@Test @Ignore
@Test
public void testEncodeWithEncodeElements() throws Exception {
Patient patient = new Patient();
patient.getMeta().addProfile("http://profile");
patient.addName().addFamily("FAMILY");
patient.addAddress().addLine("LINE1");
@ -1066,6 +1071,73 @@ public class XmlParserDstu3Test {
}
@Test
public void testEncodeWithDontEncodeElements() throws Exception {
Patient patient = new Patient();
patient.setId("123");
patient.getMeta().addProfile("http://profile");
patient.addName().addFamily("FAMILY").addGiven("GIVEN");
patient.addAddress().addLine("LINE1");
{
IParser p = ourCtx.newXmlParser();
p.setDontEncodeElements(Sets.newHashSet("*.meta", "*.id"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("address"));
assertThat(out, not(containsString("id")));
assertThat(out, not(containsString("meta")));
}
{
IParser p = ourCtx.newXmlParser();
p.setDontEncodeElements(Sets.newHashSet("Patient.meta", "Patient.id"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("address"));
assertThat(out, not(containsString("id")));
assertThat(out, not(containsString("meta")));
}
{
IParser p = ourCtx.newXmlParser();
p.setDontEncodeElements(Sets.newHashSet("Patient.name.family"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("GIVEN"));
assertThat(out, not(containsString("FAMILY")));
}
{
IParser p = ourCtx.newXmlParser();
p.setDontEncodeElements(Sets.newHashSet("*.meta", "*.id"));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("address"));
assertThat(out, not(containsString("id")));
assertThat(out, not(containsString("meta")));
}
{
IParser p = ourCtx.newXmlParser();
p.setDontEncodeElements(Sets.newHashSet("Patient.meta"));
p.setEncodeElements(new HashSet<String>(Arrays.asList("Patient.name")));
p.setPrettyPrint(true);
String out = p.encodeResourceToString(patient);
ourLog.info(out);
assertThat(out, containsString("Patient"));
assertThat(out, containsString("name"));
assertThat(out, containsString("id"));
assertThat(out, not(containsString("address")));
assertThat(out, not(containsString("meta")));
}
}
@Test
public void testEncodeWithNarrative() {

View File

@ -241,6 +241,13 @@
first query. This should improve performance when searching against large
datasets.
</action>
<action type="add">
Parsers have new method
<![CDATA[<code>setDontEncodeElements</code>]]>
which can be used to force the parser to not encode certain elements
in a resource when serializing. For example this can be used to omit
sensitive data or skip the resource metadata.
</action>
</release>
<release version="1.4" date="2016-02-04">
<action type="add">