diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java
index b45add38dc1..5363224bf6f 100644
--- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java
+++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java
@@ -1635,7 +1635,7 @@ public class JsonParser extends BaseParser implements IParser {
if (childName == null) {
childName = "value" + myContext.getElementDefinition(value.getClass()).getName();
}
- BaseRuntimeElementDefinition> childDef = myContext.getElementDefinition(value.getClass());
+ BaseRuntimeElementDefinition> childDef = extDef.getChildElementDefinitionByDatatype(value.getClass());
if (childDef == null) {
throw new ConfigurationException("Unable to encode extension, unregognized child element type: " + value.getClass().getCanonicalName());
}
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 001514aa75b..1d2e6b09c81 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
@@ -15,6 +15,7 @@ import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;
@@ -73,6 +74,44 @@ public class JsonParserDstu3Test {
public void after() {
ourCtx.setNarrativeGenerator(null);
}
+
+
+ /**
+ * See #326
+ */
+ @Test
+ public void testEncodeContainedResource() {
+ Patient patient = new Patient();
+ patient.setBirthDate(new Date());
+ patient.addExtension().setUrl("test").setValue(new Reference(new Condition()));
+
+ String encoded = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(patient);
+ ourLog.info(encoded);
+
+ //@formatter:off
+ assertThat(encoded, stringContainsInOrder(
+ "{",
+ "\"resourceType\":\"Patient\",",
+ "\"extension\":[",
+ "{",
+ "\"url\":\"test\",",
+ "\"valueReference\":{",
+ "\"reference\":\"#1\"",
+ "}",
+ "}",
+ "],",
+ "\"contained\":[",
+ "{",
+ "\"resourceType\":\"Condition\",",
+ "\"id\":\"1\"",
+ "}",
+ "],",
+ "\"birthDate\":\"2016-04-05\"",
+ "}"
+ ));
+ //@formatter:on
+ }
+
@Test
public void testEncodeAndParseExtensions() throws Exception {
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 6b1204f85be..aa648fd4077 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
@@ -22,6 +22,7 @@ import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;
@@ -45,6 +46,7 @@ import org.hl7.fhir.dstu3.model.CodeableConcept;
import org.hl7.fhir.dstu3.model.Coding;
import org.hl7.fhir.dstu3.model.Composition;
import org.hl7.fhir.dstu3.model.ConceptMap;
+import org.hl7.fhir.dstu3.model.Condition;
import org.hl7.fhir.dstu3.model.ContactPoint.ContactPointSystem;
import org.hl7.fhir.dstu3.model.DataElement;
import org.hl7.fhir.dstu3.model.DateTimeType;
@@ -102,7 +104,76 @@ public class XmlParserDstu3Test {
ourCtx.setNarrativeGenerator(null);
}
+ @Test
+ public void testBundleWithBinary() {
+ //@formatter:off
+ String bundle = "\n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ " \n" +
+ "";
+ //@formatter:on
+
+ Bundle b = ourCtx.newXmlParser().parseResource(Bundle.class, bundle);
+ assertEquals(1, b.getEntry().size());
+
+ Binary bin = (Binary) b.getEntry().get(0).getResource();
+ assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bin.getContent());
+
+ }
+
+ @Test
+ public void testContainedResourceInExtensionUndeclared() {
+ Patient p = new Patient();
+ p.addName().addFamily("PATIENT");
+
+ Organization o = new Organization();
+ o.setName("ORG");
+ p.addExtension(new Extension("urn:foo", new Reference(o)));
+
+ String str = ourCtx.newXmlParser().encodeResourceToString(p);
+ ourLog.info(str);
+
+ p = ourCtx.newXmlParser().parseResource(Patient.class, str);
+ assertEquals("PATIENT", p.getName().get(0).getFamily().get(0).getValue());
+
+ List exts = p.getExtensionsByUrl("urn:foo");
+ assertEquals(1, exts.size());
+ Reference rr = (Reference) exts.get(0).getValue();
+ o = (Organization) rr.getResource();
+ assertEquals("ORG", o.getName());
+ }
+
+
+ @Test
+ public void testDuration() {
+ Encounter enc = new Encounter();
+ Duration duration = new Duration();
+ duration.setUnit("day").setValue(123L);
+ enc.setLength(duration);
+
+ String str = ourCtx.newXmlParser().encodeResourceToString(enc);
+ ourLog.info(str);
+
+ assertThat(str, not(containsString("meta")));
+ assertThat(str, containsString(""));
+ }
+
@Test
public void testEncodeAndParseBundleWithResourceRefs() {
@@ -140,75 +211,6 @@ public class XmlParserDstu3Test {
assertSame(org, pt.getManagingOrganization().getResource());
}
-
- @Test
- public void testBundleWithBinary() {
- //@formatter:off
- String bundle = "\n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- " \n" +
- "";
- //@formatter:on
-
- Bundle b = ourCtx.newXmlParser().parseResource(Bundle.class, bundle);
- assertEquals(1, b.getEntry().size());
-
- Binary bin = (Binary) b.getEntry().get(0).getResource();
- assertArrayEquals(new byte[] { 1, 2, 3, 4 }, bin.getContent());
-
- }
-
- @Test
- public void testContainedResourceInExtensionUndeclared() {
- Patient p = new Patient();
- p.addName().addFamily("PATIENT");
-
- Organization o = new Organization();
- o.setName("ORG");
- p.addExtension(new Extension("urn:foo", new Reference(o)));
-
- String str = ourCtx.newXmlParser().encodeResourceToString(p);
- ourLog.info(str);
-
- p = ourCtx.newXmlParser().parseResource(Patient.class, str);
- assertEquals("PATIENT", p.getName().get(0).getFamily().get(0).getValue());
-
- List exts = p.getExtensionsByUrl("urn:foo");
- assertEquals(1, exts.size());
- Reference rr = (Reference) exts.get(0).getValue();
- o = (Organization) rr.getResource();
- assertEquals("ORG", o.getName());
- }
-
- @Test
- public void testDuration() {
- Encounter enc = new Encounter();
- Duration duration = new Duration();
- duration.setUnit("day").setValue(123L);
- enc.setLength(duration);
-
- String str = ourCtx.newXmlParser().encodeResourceToString(enc);
- ourLog.info(str);
-
- assertThat(str, not(containsString("meta")));
- assertThat(str, containsString(""));
- }
-
@Test
public void testEncodeAndParseContained() {
IParser xmlParser = ourCtx.newXmlParser().setPrettyPrint(true);
@@ -713,6 +715,37 @@ public class XmlParserDstu3Test {
assertThat(encoded, not(stringContainsInOrder("",
+ "",
+ "",
+ "",
+ "",
+ "",
+ "",
+ "",
+ "",
+ "",
+ "",
+ "",
+ ""
+ ));
+ //@formatter:on
+ }
+
/**
* See #113
*/
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index ec222fa8ebb..e3afbbb05b8 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -382,6 +382,13 @@
the actual resource instance populated into the reference if the
IDs matched as they did in DSTU1/2.
+
+ Contained resource references on DSTU3
+ resources were not serialized correctly when
+ using the Json Parser. Thanks to GitHub user
+ @fw060 for reporting and supplying a patch
+ which corrects the issue!
+