From f4a0397b18e2f199adc62dbe591243623d9fc31c Mon Sep 17 00:00:00 2001 From: James Agnew Date: Tue, 9 Aug 2022 17:30:10 -0400 Subject: [PATCH] Add test for #3890 (#3891) * Add test for #3890 * Remove unneeded import * Add changelog --- .../java/ca/uhn/fhir/parser/JsonParser.java | 10 +++--- .../6_2_0/3890-serialize-refobj.yaml | 6 ++++ .../ca/uhn/fhir/parser/JsonParserR4Test.java | 30 +++++++++++++++++ .../ca/uhn/fhir/parser/XmlParserR4Test.java | 32 +++++++++++++++++++ pom.xml | 2 +- 5 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_2_0/3890-serialize-refobj.yaml 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 049efbaf780..1dff2f2b15e 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 @@ -123,7 +123,7 @@ public class JsonParser extends BaseParser implements IJsonLikeParser { } private boolean addToHeldExtensions(int valueIdx, List> ext, ArrayList> list, boolean theIsModifier, CompositeChildElement theChildElem, - CompositeChildElement theParent, EncodeContext theEncodeContext, boolean theContainedResource, IBase theContainingElement) { + CompositeChildElement theParent, EncodeContext theEncodeContext, boolean theContainedResource, IBase theContainingElement) { boolean retVal = false; if (ext.size() > 0) { Boolean encodeExtension = null; @@ -1379,6 +1379,10 @@ public class JsonParser extends BaseParser implements IJsonLikeParser { } } + private static void write(JsonLikeWriter theWriter, String theName, String theValue) throws IOException { + theWriter.write(theName, theValue); + } + private class HeldExtension implements Comparable { private CompositeChildElement myChildElem; @@ -1562,8 +1566,4 @@ public class JsonParser extends BaseParser implements IJsonLikeParser { theEventWriter.endObject(); } } - - private static void write(JsonLikeWriter theWriter, String theName, String theValue) throws IOException { - theWriter.write(theName, theValue); - } } diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_2_0/3890-serialize-refobj.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_2_0/3890-serialize-refobj.yaml new file mode 100644 index 00000000000..84cd4527640 --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_2_0/3890-serialize-refobj.yaml @@ -0,0 +1,6 @@ +--- +type: fix +issue: 3890 +title: "When serializing references where the reference target has been instantiated using a resource object and + not a reference string, the serialization was omitted when the reference appeared in the resource + metadata section. This has been corrected." diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/JsonParserR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/JsonParserR4Test.java index 7822b86c5e9..d7d20a468a9 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/JsonParserR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/JsonParserR4Test.java @@ -14,6 +14,7 @@ import org.apache.commons.io.IOUtils; import org.apache.commons.io.output.NullWriter; import org.apache.commons.lang.StringUtils; import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.r4.model.Appointment; import org.hl7.fhir.r4.model.AuditEvent; import org.hl7.fhir.r4.model.Basic; import org.hl7.fhir.r4.model.Binary; @@ -28,8 +29,10 @@ import org.hl7.fhir.r4.model.Medication; import org.hl7.fhir.r4.model.MedicationDispense; import org.hl7.fhir.r4.model.MedicationRequest; import org.hl7.fhir.r4.model.MessageHeader; +import org.hl7.fhir.r4.model.Meta; import org.hl7.fhir.r4.model.Narrative; import org.hl7.fhir.r4.model.Observation; +import org.hl7.fhir.r4.model.Organization; import org.hl7.fhir.r4.model.Patient; import org.hl7.fhir.r4.model.Practitioner; import org.hl7.fhir.r4.model.PrimitiveType; @@ -67,6 +70,8 @@ import static org.hamcrest.Matchers.stringContainsInOrder; import static org.hamcrest.core.IsNot.not; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; public class JsonParserR4Test extends BaseTest { @@ -736,6 +741,31 @@ public class JsonParserR4Test extends BaseTest { } + /** + * See #3890 + */ + @Test + public void testEncodeExtensionWithReferenceObjectValue() { + + Appointment appointment = new Appointment(); + appointment.setId("123"); + + Meta meta = new Meta(); + Extension extension = new Extension(); + extension.setUrl("http://example-source-team.com"); + extension.setValue(new Reference(new Organization().setId("546"))); + meta.addExtension(extension); + appointment.setMeta(meta); + + var parser = ourCtx.newJsonParser(); + String output = parser.encodeResourceToString(appointment); + ourLog.info("Output: {}", output); + + Appointment input = parser.parseResource(Appointment.class, output); + + assertNotNull(input.getMeta().getExtensionByUrl("http://example-source-team.com")); + } + @Test public void testParseExtensionWithUriValue_BuiltInStructure() { String input = "{\n" + diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/XmlParserR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/XmlParserR4Test.java index 0666044fe70..6352495d024 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/XmlParserR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/parser/XmlParserR4Test.java @@ -5,19 +5,25 @@ import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.stringContainsInOrder; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import java.io.IOException; import java.net.URL; +import org.hl7.fhir.r4.model.Appointment; import org.hl7.fhir.r4.model.AuditEvent; import org.hl7.fhir.r4.model.Bundle; import org.hl7.fhir.r4.model.Composition; import org.hl7.fhir.r4.model.DocumentReference; +import org.hl7.fhir.r4.model.Extension; import org.hl7.fhir.r4.model.MessageHeader; +import org.hl7.fhir.r4.model.Meta; import org.hl7.fhir.r4.model.Narrative; import org.hl7.fhir.r4.model.Observation; +import org.hl7.fhir.r4.model.Organization; import org.hl7.fhir.r4.model.Parameters; import org.hl7.fhir.r4.model.Patient; +import org.hl7.fhir.r4.model.Reference; import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -45,6 +51,32 @@ public class XmlParserR4Test extends BaseTest { } + /** + * See #3890 + */ + @Test + public void testEncodeExtensionWithReferenceObjectValue() { + + Appointment appointment = new Appointment(); + appointment.setId("123"); + + Meta meta = new Meta(); + Extension extension = new Extension(); + extension.setUrl("http://example-source-team.com"); + extension.setValue(new Reference(new Organization().setId("546"))); + meta.addExtension(extension); + appointment.setMeta(meta); + + var parser = ourCtx.newXmlParser(); + String output = parser.encodeResourceToString(appointment); + ourLog.info("Output: {}", output); + + Appointment input = parser.parseResource(Appointment.class, output); + + assertNotNull(input.getMeta().getExtensionByUrl("http://example-source-team.com")); + } + + /** * Ensure that a contained bundle doesn't cause a crash */ diff --git a/pom.xml b/pom.xml index 1b8c54ce081..4d7e649145a 100644 --- a/pom.xml +++ b/pom.xml @@ -767,7 +767,7 @@ - 5.6.54 + 5.6.55-SNAPSHOT 1.0.3 -Dfile.encoding=UTF-8 -Xmx2048m