diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/ParserOptions.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/ParserOptions.java index fdba17a13df..418b38c73b8 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/ParserOptions.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/ParserOptions.java @@ -41,6 +41,7 @@ public class ParserOptions { private boolean myStripVersionsFromReferences = true; private Set myDontStripVersionsFromReferencesAtPaths = Collections.emptySet(); + private boolean myOverrideResourceIdWithBundleEntryFullUrl = true; /** * If supplied value(s), any resource references at the specified paths will have their @@ -144,4 +145,33 @@ public class ParserOptions { return myDontStripVersionsFromReferencesAtPaths; } + /** + * If set to true (which is the default), the Bundle.entry.fullUrl will override the Bundle.entry.resource's + * resource id if the fullUrl is defined. This behavior happens when parsing the source data into a Bundle object. Set this + * to false if this is not the desired behavior (e.g. the client code wishes to perform additional + * validation checks between the fullUrl and the resource id). + * + * @return Returns the parser instance's configuration setting for overriding resource ids with Bundle.entry.fullUrl when + * parsing the source data into a Bundle object. Default is true. + */ + public boolean isOverrideResourceIdWithBundleEntryFullUrl() { + return myOverrideResourceIdWithBundleEntryFullUrl; + } + + /** + * If set to true (which is the default), the Bundle.entry.fullUrl will override the Bundle.entry.resource's + * resource id if the fullUrl is defined. This behavior happens when parsing the source data into a Bundle object. Set this + * to false if this is not the desired behavior (e.g. the client code wishes to perform additional + * validation checks between the fullUrl and the resource id). + * + * @param theOverrideResourceIdWithBundleEntryFullUrl + * Set this to false to prevent the parser from overriding resource ids with the + * Bundle.entry.fullUrl + * + * @return Returns a reference to this parser so that method calls can be chained together + */ + public ParserOptions setOverrideResourceIdWithBundleEntryFullUrl(boolean theOverrideResourceIdWithBundleEntryFullUrl) { + myOverrideResourceIdWithBundleEntryFullUrl = theOverrideResourceIdWithBundleEntryFullUrl; + return this; + } } 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 481fb3eba66..d84eed8a41d 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 @@ -97,6 +97,7 @@ public abstract class BaseParser implements IParser { private List> myPreferTypes; private String myServerBaseUrl; private Boolean myStripVersionsFromReferences; + private Boolean myOverrideResourceIdWithBundleEntryFullUrl; private boolean mySummaryMode; private boolean mySuppressNarratives; private Set myDontStripVersionsFromReferencesAtPaths; @@ -346,6 +347,15 @@ public abstract class BaseParser implements IParser { return true; } + + private boolean isOverrideResourceIdWithBundleEntryFullUrl() { + Boolean overrideResourceIdWithBundleEntryFullUrl = myOverrideResourceIdWithBundleEntryFullUrl; + if (overrideResourceIdWithBundleEntryFullUrl != null) { + return overrideResourceIdWithBundleEntryFullUrl; + } + + return myContext.getParserOptions().isOverrideResourceIdWithBundleEntryFullUrl(); + } protected abstract void doEncodeBundleToWriter(Bundle theBundle, Writer theWriter) throws IOException, DataFormatException; @@ -596,6 +606,11 @@ public abstract class BaseParser implements IParser { public Boolean getStripVersionsFromReferences() { return myStripVersionsFromReferences; } + + @Override + public Boolean getOverrideResourceIdWithBundleEntryFullUrl() { + return myOverrideResourceIdWithBundleEntryFullUrl; + } @Override public boolean isSummaryMode() { @@ -658,22 +673,23 @@ public abstract class BaseParser implements IParser { if (fullUrlChild == null) { continue; // TODO: remove this once the data model in tinder plugin catches up to 1.2 } - List fullUrl = fullUrlChild.getAccessor().getValues(nextEntry); - if (fullUrl != null && !fullUrl.isEmpty()) { - IPrimitiveType value = (IPrimitiveType) fullUrl.get(0); - if (value.isEmpty() == false) { - List entryResources = entryDef.getChildByName("resource").getAccessor().getValues(nextEntry); - if (entryResources != null && entryResources.size() > 0) { - IBaseResource res = (IBaseResource) entryResources.get(0); - String versionId = res.getIdElement().getVersionIdPart(); - res.setId(value.getValueAsString()); - if (isNotBlank(versionId) && res.getIdElement().hasVersionIdPart() == false) { - res.setId(res.getIdElement().withVersion(versionId)); + if (isOverrideResourceIdWithBundleEntryFullUrl()) { + List fullUrl = fullUrlChild.getAccessor().getValues(nextEntry); + if (fullUrl != null && !fullUrl.isEmpty()) { + IPrimitiveType value = (IPrimitiveType) fullUrl.get(0); + if (value.isEmpty() == false) { + List entryResources = entryDef.getChildByName("resource").getAccessor().getValues(nextEntry); + if (entryResources != null && entryResources.size() > 0) { + IBaseResource res = (IBaseResource) entryResources.get(0); + String versionId = res.getIdElement().getVersionIdPart(); + res.setId(value.getValueAsString()); + if (isNotBlank(versionId) && res.getIdElement().hasVersionIdPart() == false) { + res.setId(res.getIdElement().withVersion(versionId)); + } } } } } - } } @@ -876,6 +892,12 @@ public abstract class BaseParser implements IParser { myStripVersionsFromReferences = theStripVersionsFromReferences; return this; } + + @Override + public IParser setOverrideResourceIdWithBundleEntryFullUrl(Boolean theOverrideResourceIdWithBundleEntryFullUrl) { + myOverrideResourceIdWithBundleEntryFullUrl = theOverrideResourceIdWithBundleEntryFullUrl; + return this; + } @Override public IParser setDontStripVersionsFromReferencesAtPaths(String... thePaths) { diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/IParser.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/IParser.java index 4c7be497bff..582b8212ec3 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/IParser.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/IParser.java @@ -128,6 +128,19 @@ public interface IParser { * @see ParserOptions */ Boolean getStripVersionsFromReferences(); + + /** + * If set to true (which is the default), the Bundle.entry.fullUrl will override the Bundle.entry.resource's + * resource id if the fullUrl is defined. This behavior happens when parsing the source data into a Bundle object. Set this + * to false if this is not the desired behavior (e.g. the client code wishes to perform additional + * validation checks between the fullUrl and the resource id). + * + * @return Returns the parser instance's configuration setting for overriding resource ids with Bundle.entry.fullUrl when + * parsing the source data into a Bundle object. This method will return null if no value is set, in + * which case the value from the {@link ParserOptions} will be used (default is true) + * @see ParserOptions + */ + Boolean getOverrideResourceIdWithBundleEntryFullUrl(); /** * Is the parser in "summary mode"? See {@link #setSummaryMode(boolean)} for information @@ -361,6 +374,22 @@ public interface IParser { * @return Returns a reference to this parser so that method calls can be chained together */ IParser setStripVersionsFromReferences(Boolean theStripVersionsFromReferences); + + /** + * If set to true (which is the default), the Bundle.entry.fullUrl will override the Bundle.entry.resource's + * resource id if the fullUrl is defined. This behavior happens when parsing the source data into a Bundle object. Set this + * to false if this is not the desired behavior (e.g. the client code wishes to perform additional + * validation checks between the fullUrl and the resource id). + * + * @param theOverrideResourceIdWithBundleEntryFullUrl + * Set this to false to prevent the parser from overriding resource ids with the + * Bundle.entry.fullUrl (or null to apply the default setting from the {@link ParserOptions}) + * + * @see ParserOptions + * + * @return Returns a reference to this parser so that method calls can be chained together + */ + IParser setOverrideResourceIdWithBundleEntryFullUrl(Boolean theOverrideResourceIdWithBundleEntryFullUrl); /** * If set to true (default is false) only elements marked by the FHIR specification as diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2_1Test.java index 3a903e512a5..1d25182fcb3 100644 --- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2_1Test.java +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2_1Test.java @@ -6,6 +6,7 @@ import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.stringContainsInOrder; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; @@ -36,6 +37,7 @@ import org.hl7.fhir.dstu2016may.model.Enumeration; import org.hl7.fhir.dstu2016may.model.Enumerations.AdministrativeGender; import org.hl7.fhir.dstu2016may.model.Identifier.IdentifierUse; import org.hl7.fhir.dstu2016may.model.Observation.ObservationStatus; +import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.utilities.xhtml.XhtmlNode; import org.junit.*; import org.mockito.ArgumentCaptor; @@ -62,7 +64,46 @@ public class JsonParserDstu2_1Test { ourCtx.setNarrativeGenerator(null); } + @Test + public void testOverrideResourceIdWithBundleEntryFullUrlDisabled_ConfiguredOnFhirContext() { + try { + String tmp = "{\"resourceType\":\"Bundle\",\"entry\":[{\"fullUrl\":\"http://lalaland.org/patient/pat1\",\"resource\":{\"resourceType\":\"Patient\",\"id\":\"patxuzos\"}}]}"; + ourCtx.getParserOptions().setOverrideResourceIdWithBundleEntryFullUrl(false); + Bundle bundle = (Bundle) ourCtx.newJsonParser().parseResource(tmp); + assertEquals(1, bundle.getEntry().size()); + { + Patient o1 = (Patient) bundle.getEntry().get(0).getResource(); + IIdType o1Id = o1.getIdElement(); + assertFalse(o1Id.hasBaseUrl()); + assertEquals("Patient", o1Id.getResourceType()); + assertEquals("patxuzos", o1Id.getIdPart()); + assertFalse(o1Id.hasVersionIdPart()); + } + } finally { + // ensure we cleanup ourCtx so other tests continue to work + ourCtx = FhirContext.forDstu2_1(); + } + } + @Test + public void testOverrideResourceIdWithBundleEntryFullUrlDisabled_ConfiguredOnParser() { + try { + String tmp = "{\"resourceType\":\"Bundle\",\"entry\":[{\"fullUrl\":\"http://lalaland.org/patient/pat1\",\"resource\":{\"resourceType\":\"Patient\",\"id\":\"patxuzos\"}}]}"; + Bundle bundle = (Bundle) ourCtx.newJsonParser().setOverrideResourceIdWithBundleEntryFullUrl(false).parseResource(tmp); + assertEquals(1, bundle.getEntry().size()); + { + Patient o1 = (Patient) bundle.getEntry().get(0).getResource(); + IIdType o1Id = o1.getIdElement(); + assertFalse(o1Id.hasBaseUrl()); + assertEquals("Patient", o1Id.getResourceType()); + assertEquals("patxuzos", o1Id.getIdPart()); + assertFalse(o1Id.hasVersionIdPart()); + } + } finally { + // ensure we cleanup ourCtx so other tests continue to work + ourCtx = FhirContext.forDstu2_1(); + } + } /** * #480 diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2_1Test.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2_1Test.java index b8e8dab1fdb..529aad2e195 100644 --- a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2_1Test.java +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2_1Test.java @@ -8,6 +8,7 @@ import static org.hamcrest.Matchers.startsWith; import static org.hamcrest.Matchers.stringContainsInOrder; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; @@ -96,6 +97,7 @@ import org.hl7.fhir.dstu2016may.model.StringType; import org.hl7.fhir.dstu2016may.model.UriType; import org.hl7.fhir.dstu2016may.model.ValueSet; import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.instance.model.api.IIdType; import org.junit.After; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -128,6 +130,47 @@ public class XmlParserDstu2_1Test { } ourCtx.setNarrativeGenerator(null); } + + @Test + public void testOverrideResourceIdWithBundleEntryFullUrlDisabled_ConfiguredOnFhirContext() { + try { + String tmp = ""; + ourCtx.getParserOptions().setOverrideResourceIdWithBundleEntryFullUrl(false); + Bundle bundle = (Bundle) ourCtx.newXmlParser().parseResource(tmp); + assertEquals(1, bundle.getEntry().size()); + { + Patient o1 = (Patient) bundle.getEntry().get(0).getResource(); + IIdType o1Id = o1.getIdElement(); + assertFalse(o1Id.hasBaseUrl()); + assertEquals("Patient", o1Id.getResourceType()); + assertEquals("patxuzos", o1Id.getIdPart()); + assertFalse(o1Id.hasVersionIdPart()); + } + } finally { + // ensure we cleanup ourCtx so other tests continue to work + ourCtx = null; + } + } + + @Test + public void testOverrideResourceIdWithBundleEntryFullUrlDisabled_ConfiguredOnParser() { + try { + String tmp = ""; + Bundle bundle = (Bundle) ourCtx.newXmlParser().setOverrideResourceIdWithBundleEntryFullUrl(false).parseResource(tmp); + assertEquals(1, bundle.getEntry().size()); + { + Patient o1 = (Patient) bundle.getEntry().get(0).getResource(); + IIdType o1Id = o1.getIdElement(); + assertFalse(o1Id.hasBaseUrl()); + assertEquals("Patient", o1Id.getResourceType()); + assertEquals("patxuzos", o1Id.getIdPart()); + assertFalse(o1Id.hasVersionIdPart()); + } + } finally { + // ensure we cleanup ourCtx so other tests continue to work + ourCtx = null; + } + } /** * See #414 diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2Test.java index b109314a0b3..7f489ab3f16 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2Test.java @@ -5,6 +5,7 @@ import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.stringContainsInOrder; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; @@ -22,6 +23,7 @@ import java.util.*; import org.apache.commons.io.IOUtils; import org.hamcrest.Matchers; import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.instance.model.api.IIdType; import org.junit.AfterClass; import org.junit.Assert; import org.junit.Test; @@ -70,6 +72,47 @@ public class JsonParserDstu2Test { private static FhirContext ourCtx = FhirContext.forDstu2(); private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(JsonParserDstu2Test.class); + @Test + public void testOverrideResourceIdWithBundleEntryFullUrlDisabled_ConfiguredOnFhirContext() { + try { + String tmp = "{\"resourceType\":\"Bundle\",\"entry\":[{\"fullUrl\":\"http://lalaland.org/patient/pat1\",\"resource\":{\"resourceType\":\"Patient\",\"id\":\"patxuzos\"}}]}"; + ourCtx.getParserOptions().setOverrideResourceIdWithBundleEntryFullUrl(false); + ca.uhn.fhir.model.dstu2.resource.Bundle bundle = (ca.uhn.fhir.model.dstu2.resource.Bundle) ourCtx.newJsonParser().parseResource(tmp); + assertEquals(1, bundle.getEntry().size()); + { + Patient o1 = (Patient) bundle.getEntry().get(0).getResource(); + IIdType o1Id = o1.getIdElement(); + assertFalse(o1Id.hasBaseUrl()); + assertEquals("Patient", o1Id.getResourceType()); + assertEquals("patxuzos", o1Id.getIdPart()); + assertFalse(o1Id.hasVersionIdPart()); + } + } finally { + // ensure we cleanup ourCtx so other tests continue to work + ourCtx = FhirContext.forDstu2(); + } + } + + @Test + public void testOverrideResourceIdWithBundleEntryFullUrlDisabled_ConfiguredOnParser() { + try { + String tmp = "{\"resourceType\":\"Bundle\",\"entry\":[{\"fullUrl\":\"http://lalaland.org/patient/pat1\",\"resource\":{\"resourceType\":\"Patient\",\"id\":\"patxuzos\"}}]}"; + ca.uhn.fhir.model.dstu2.resource.Bundle bundle = (ca.uhn.fhir.model.dstu2.resource.Bundle) ourCtx.newJsonParser().setOverrideResourceIdWithBundleEntryFullUrl(false).parseResource(tmp); + assertEquals(1, bundle.getEntry().size()); + { + Patient o1 = (Patient) bundle.getEntry().get(0).getResource(); + IIdType o1Id = o1.getIdElement(); + assertFalse(o1Id.hasBaseUrl()); + assertEquals("Patient", o1Id.getResourceType()); + assertEquals("patxuzos", o1Id.getIdPart()); + assertFalse(o1Id.hasVersionIdPart()); + } + } finally { + // ensure we cleanup ourCtx so other tests continue to work + ourCtx = FhirContext.forDstu2(); + } + } + /** * See #544 */ diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2Test.java index 1b3e9617515..8501a3ad5ea 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2Test.java @@ -12,6 +12,7 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.fail; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; @@ -30,6 +31,7 @@ import org.hamcrest.collection.IsEmptyCollection; import org.hamcrest.core.StringContains; import org.hamcrest.text.StringContainsInOrder; import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.instance.model.api.IIdType; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; @@ -68,6 +70,47 @@ public class XmlParserDstu2Test { } } + @Test + public void testOverrideResourceIdWithBundleEntryFullUrlDisabled_ConfiguredOnFhirContext() { + try { + String tmp = ""; + ourCtx.getParserOptions().setOverrideResourceIdWithBundleEntryFullUrl(false); + ca.uhn.fhir.model.dstu2.resource.Bundle bundle = (ca.uhn.fhir.model.dstu2.resource.Bundle) ourCtx.newXmlParser().parseResource(tmp); + assertEquals(1, bundle.getEntry().size()); + { + Patient o1 = (Patient) bundle.getEntry().get(0).getResource(); + IIdType o1Id = o1.getIdElement(); + assertFalse(o1Id.hasBaseUrl()); + assertEquals("Patient", o1Id.getResourceType()); + assertEquals("patxuzos", o1Id.getIdPart()); + assertFalse(o1Id.hasVersionIdPart()); + } + } finally { + // ensure we cleanup ourCtx so other tests continue to work + ourCtx = null; + } + } + + @Test + public void testOverrideResourceIdWithBundleEntryFullUrlDisabled_ConfiguredOnParser() { + try { + String tmp = ""; + ca.uhn.fhir.model.dstu2.resource.Bundle bundle = (ca.uhn.fhir.model.dstu2.resource.Bundle) ourCtx.newXmlParser().setOverrideResourceIdWithBundleEntryFullUrl(false).parseResource(tmp); + assertEquals(1, bundle.getEntry().size()); + { + Patient o1 = (Patient) bundle.getEntry().get(0).getResource(); + IIdType o1Id = o1.getIdElement(); + assertFalse(o1Id.hasBaseUrl()); + assertEquals("Patient", o1Id.getResourceType()); + assertEquals("patxuzos", o1Id.getIdPart()); + assertFalse(o1Id.hasVersionIdPart()); + } + } finally { + // ensure we cleanup ourCtx so other tests continue to work + ourCtx = null; + } + } + /** * See #414 */ 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 31cab91804c..17d6f968bee 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 @@ -7,6 +7,7 @@ import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.stringContainsInOrder; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; @@ -45,6 +46,7 @@ import org.hl7.fhir.dstu3.model.CapabilityStatement.UnknownContentCode; import org.hl7.fhir.dstu3.model.Enumerations.AdministrativeGender; import org.hl7.fhir.dstu3.model.Identifier.IdentifierUse; import org.hl7.fhir.dstu3.model.Observation.ObservationStatus; +import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.utilities.xhtml.XhtmlNode; import org.junit.After; import org.junit.AfterClass; @@ -79,6 +81,47 @@ public class JsonParserDstu3Test { public void after() { ourCtx.setNarrativeGenerator(null); } + + @Test + public void testOverrideResourceIdWithBundleEntryFullUrlDisabled_ConfiguredOnFhirContext() { + try { + String tmp = "{\"resourceType\":\"Bundle\",\"entry\":[{\"fullUrl\":\"http://lalaland.org/patient/pat1\",\"resource\":{\"resourceType\":\"Patient\",\"id\":\"patxuzos\"}}]}"; + ourCtx.getParserOptions().setOverrideResourceIdWithBundleEntryFullUrl(false); + Bundle bundle = (Bundle) ourCtx.newJsonParser().parseResource(tmp); + assertEquals(1, bundle.getEntry().size()); + { + Patient o1 = (Patient) bundle.getEntry().get(0).getResource(); + IIdType o1Id = o1.getIdElement(); + assertFalse(o1Id.hasBaseUrl()); + assertEquals("Patient", o1Id.getResourceType()); + assertEquals("patxuzos", o1Id.getIdPart()); + assertFalse(o1Id.hasVersionIdPart()); + } + } finally { + // ensure we cleanup ourCtx so other tests continue to work + ourCtx = FhirContext.forDstu3(); + } + } + + @Test + public void testOverrideResourceIdWithBundleEntryFullUrlDisabled_ConfiguredOnParser() { + try { + String tmp = "{\"resourceType\":\"Bundle\",\"entry\":[{\"fullUrl\":\"http://lalaland.org/patient/pat1\",\"resource\":{\"resourceType\":\"Patient\",\"id\":\"patxuzos\"}}]}"; + Bundle bundle = (Bundle) ourCtx.newJsonParser().setOverrideResourceIdWithBundleEntryFullUrl(false).parseResource(tmp); + assertEquals(1, bundle.getEntry().size()); + { + Patient o1 = (Patient) bundle.getEntry().get(0).getResource(); + IIdType o1Id = o1.getIdElement(); + assertFalse(o1Id.hasBaseUrl()); + assertEquals("Patient", o1Id.getResourceType()); + assertEquals("patxuzos", o1Id.getIdPart()); + assertFalse(o1Id.hasVersionIdPart()); + } + } finally { + // ensure we cleanup ourCtx so other tests continue to work + ourCtx = FhirContext.forDstu3(); + } + } /** * See #544 diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java index 94dabf40483..fbd14c52f9a 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java @@ -8,6 +8,7 @@ import static org.hamcrest.Matchers.startsWith; import static org.hamcrest.Matchers.stringContainsInOrder; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; @@ -50,6 +51,7 @@ import org.hl7.fhir.dstu3.model.Identifier.IdentifierUse; import org.hl7.fhir.dstu3.model.Observation.ObservationRelationshipType; import org.hl7.fhir.dstu3.model.Observation.ObservationStatus; import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.instance.model.api.IIdType; import org.junit.*; import org.mockito.ArgumentCaptor; @@ -77,6 +79,47 @@ public class XmlParserDstu3Test { } ourCtx.setNarrativeGenerator(null); } + + @Test + public void testOverrideResourceIdWithBundleEntryFullUrlDisabled_ConfiguredOnFhirContext() { + try { + String tmp = ""; + ourCtx.getParserOptions().setOverrideResourceIdWithBundleEntryFullUrl(false); + Bundle bundle = (Bundle) ourCtx.newXmlParser().parseResource(tmp); + assertEquals(1, bundle.getEntry().size()); + { + Patient o1 = (Patient) bundle.getEntry().get(0).getResource(); + IIdType o1Id = o1.getIdElement(); + assertFalse(o1Id.hasBaseUrl()); + assertEquals("Patient", o1Id.getResourceType()); + assertEquals("patxuzos", o1Id.getIdPart()); + assertFalse(o1Id.hasVersionIdPart()); + } + } finally { + // ensure we cleanup ourCtx so other tests continue to work + ourCtx = null; + } + } + + @Test + public void testOverrideResourceIdWithBundleEntryFullUrlDisabled_ConfiguredOnParser() { + try { + String tmp = ""; + Bundle bundle = (Bundle) ourCtx.newXmlParser().setOverrideResourceIdWithBundleEntryFullUrl(false).parseResource(tmp); + assertEquals(1, bundle.getEntry().size()); + { + Patient o1 = (Patient) bundle.getEntry().get(0).getResource(); + IIdType o1Id = o1.getIdElement(); + assertFalse(o1Id.hasBaseUrl()); + assertEquals("Patient", o1Id.getResourceType()); + assertEquals("patxuzos", o1Id.getIdPart()); + assertFalse(o1Id.hasVersionIdPart()); + } + } finally { + // ensure we cleanup ourCtx so other tests continue to work + ourCtx = null; + } + } /** * See #551 diff --git a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/parser/JsonParserHl7OrgDstu2Test.java b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/parser/JsonParserHl7OrgDstu2Test.java index 7f71ba9c948..b14af83f968 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/parser/JsonParserHl7OrgDstu2Test.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/parser/JsonParserHl7OrgDstu2Test.java @@ -5,6 +5,7 @@ import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.stringContainsInOrder; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -56,6 +57,7 @@ import org.hl7.fhir.instance.model.ValueSet; import org.hl7.fhir.instance.model.ValueSet.ConceptDefinitionComponent; import org.hl7.fhir.instance.model.ValueSet.ValueSetCodeSystemComponent; import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.instance.model.api.INarrative; import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.hl7.fhir.utilities.xhtml.XhtmlNode; @@ -88,6 +90,67 @@ public class JsonParserHl7OrgDstu2Test { public static void afterClassClearContext() { TestUtil.clearAllStaticFieldsForUnitTest(); } + + @Test + public void testOverrideResourceIdWithBundleEntryFullUrlEnabled() { + try { + String tmp = "{\"resourceType\":\"Bundle\",\"entry\":[{\"fullUrl\":\"http://lalaland.org/patient/pat1\",\"resource\":{\"resourceType\":\"Patient\",\"id\":\"patxuzos\"}}]}"; + Bundle bundle = (Bundle) ourCtx.newJsonParser().parseResource(tmp); + assertEquals(1, bundle.getEntry().size()); + { + Patient o1 = (Patient) bundle.getEntry().get(0).getResource(); + IIdType o1Id = o1.getIdElement(); + assertEquals("http://lalaland.org", o1Id.getBaseUrl()); + assertEquals("patient", o1Id.getResourceType()); + assertEquals("pat1", o1Id.getIdPart()); + assertFalse(o1Id.hasVersionIdPart()); + } + } finally { + // ensure we cleanup ourCtx so other tests continue to work + ourCtx = FhirContext.forDstu2Hl7Org(); + } + } + + @Test + public void testOverrideResourceIdWithBundleEntryFullUrlDisabled_ConfiguredOnFhirContext() { + try { + String tmp = "{\"resourceType\":\"Bundle\",\"entry\":[{\"fullUrl\":\"http://lalaland.org/patient/pat1\",\"resource\":{\"resourceType\":\"Patient\",\"id\":\"patxuzos\"}}]}"; + ourCtx.getParserOptions().setOverrideResourceIdWithBundleEntryFullUrl(false); + Bundle bundle = (Bundle) ourCtx.newJsonParser().parseResource(tmp); + assertEquals(1, bundle.getEntry().size()); + { + Patient o1 = (Patient) bundle.getEntry().get(0).getResource(); + IIdType o1Id = o1.getIdElement(); + assertFalse(o1Id.hasBaseUrl()); + assertEquals("Patient", o1Id.getResourceType()); + assertEquals("patxuzos", o1Id.getIdPart()); + assertFalse(o1Id.hasVersionIdPart()); + } + } finally { + // ensure we cleanup ourCtx so other tests continue to work + ourCtx = FhirContext.forDstu2Hl7Org(); + } + } + + @Test + public void testOverrideResourceIdWithBundleEntryFullUrlDisabled_ConfiguredOnParser() { + try { + String tmp = "{\"resourceType\":\"Bundle\",\"entry\":[{\"fullUrl\":\"http://lalaland.org/patient/pat1\",\"resource\":{\"resourceType\":\"Patient\",\"id\":\"patxuzos\"}}]}"; + Bundle bundle = (Bundle) ourCtx.newJsonParser().setOverrideResourceIdWithBundleEntryFullUrl(false).parseResource(tmp); + assertEquals(1, bundle.getEntry().size()); + { + Patient o1 = (Patient) bundle.getEntry().get(0).getResource(); + IIdType o1Id = o1.getIdElement(); + assertFalse(o1Id.hasBaseUrl()); + assertEquals("Patient", o1Id.getResourceType()); + assertEquals("patxuzos", o1Id.getIdPart()); + assertFalse(o1Id.hasVersionIdPart()); + } + } finally { + // ensure we cleanup ourCtx so other tests continue to work + ourCtx = FhirContext.forDstu2Hl7Org(); + } + } @Test public void testEncodeUndeclaredExtensionWithEnumerationContent() { diff --git a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserHl7OrgDstu2Test.java b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserHl7OrgDstu2Test.java index 332a1cf79a4..a090236f69d 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserHl7OrgDstu2Test.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserHl7OrgDstu2Test.java @@ -5,6 +5,7 @@ import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.stringContainsInOrder; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; @@ -60,6 +61,7 @@ import org.hl7.fhir.instance.model.SimpleQuantity; import org.hl7.fhir.instance.model.Specimen; import org.hl7.fhir.instance.model.StringType; import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.instance.model.api.IIdType; import org.hl7.fhir.instance.model.api.INarrative; import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.junit.After; @@ -97,6 +99,52 @@ public class XmlParserHl7OrgDstu2Test { private String fixDivNodeTextJson(String htmlNoNs) { return htmlNoNs.replace("
", "
"); } + + @Test + public void testOverrideResourceIdWithBundleEntryFullUrlEnabled() { + String tmp = ""; + Bundle bundle = (Bundle) ourCtx.newXmlParser().parseResource(tmp); + assertEquals(1, bundle.getEntry().size()); + { + Patient o1 = (Patient) bundle.getEntry().get(0).getResource(); + IIdType o1Id = o1.getIdElement(); + assertEquals("http://lalaland.org", o1Id.getBaseUrl()); + assertEquals("patient", o1Id.getResourceType()); + assertEquals("pat1", o1Id.getIdPart()); + assertFalse(o1Id.hasVersionIdPart()); + } + } + + @Test + public void testOverrideResourceIdWithBundleEntryFullUrlDisabled_ConfiguredOnFhirContext() { + String tmp = ""; + ourCtx.getParserOptions().setOverrideResourceIdWithBundleEntryFullUrl(false); + Bundle bundle = (Bundle) ourCtx.newXmlParser().parseResource(tmp); + assertEquals(1, bundle.getEntry().size()); + { + Patient o1 = (Patient) bundle.getEntry().get(0).getResource(); + IIdType o1Id = o1.getIdElement(); + assertFalse(o1Id.hasBaseUrl()); + assertEquals("Patient", o1Id.getResourceType()); + assertEquals("patxuzos", o1Id.getIdPart()); + assertFalse(o1Id.hasVersionIdPart()); + } + } + + @Test + public void testOverrideResourceIdWithBundleEntryFullUrlDisabled_ConfiguredOnParser() { + String tmp = ""; + Bundle bundle = (Bundle) ourCtx.newXmlParser().setOverrideResourceIdWithBundleEntryFullUrl(false).parseResource(tmp); + assertEquals(1, bundle.getEntry().size()); + { + Patient o1 = (Patient) bundle.getEntry().get(0).getResource(); + IIdType o1Id = o1.getIdElement(); + assertFalse(o1Id.hasBaseUrl()); + assertEquals("Patient", o1Id.getResourceType()); + assertEquals("patxuzos", o1Id.getIdPart()); + assertFalse(o1Id.hasVersionIdPart()); + } + } @Test public void testComposition() {