From d5184a8a57365fd873076a45444ef9a0e8e83c65 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Sat, 6 May 2023 16:05:48 -0400 Subject: [PATCH] Don't generate empty JSON element for comment (#4832) * Don't generate empty JSON element for comment * Fixed --- .../java/ca/uhn/fhir/parser/JsonParser.java | 2 +- ...ion-produced-empty-object-for-comment.yaml | 6 ++++ .../ca/uhn/fhir/parser/JsonParserR4Test.java | 31 +++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_8_0/4832-json-serialization-produced-empty-object-for-comment.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 43d29557289..e5bff539be0 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 @@ -553,7 +553,7 @@ public class JsonParser extends BaseParser implements IJsonLikeParser { } - if (!extensions.isEmpty() || !modifierExtensions.isEmpty() || !comments.isEmpty()) { + if (!extensions.isEmpty() || !modifierExtensions.isEmpty() || (!comments.isEmpty() && isSupportsFhirComment())) { if (inArray) { // If this is a repeatable field, the extensions go in an array too beginArray(theEventWriter, '_' + currentChildName); diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_8_0/4832-json-serialization-produced-empty-object-for-comment.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_8_0/4832-json-serialization-produced-empty-object-for-comment.yaml new file mode 100644 index 00000000000..02da55ebe81 --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_8_0/4832-json-serialization-produced-empty-object-for-comment.yaml @@ -0,0 +1,6 @@ +--- +type: fix +issue: 4832 +title: "When serializing resources in JSON, if resource came from an XML-parsed resource that + contained comments, the JSON serializer incorrectly created an empty object. 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 9b716d0b7b7..3566d03f555 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 @@ -42,6 +42,7 @@ import org.hl7.fhir.r4.model.PrimitiveType; import org.hl7.fhir.r4.model.Quantity; import org.hl7.fhir.r4.model.QuestionnaireResponse; import org.hl7.fhir.r4.model.Reference; +import org.hl7.fhir.r4.model.Resource; import org.hl7.fhir.r4.model.StringType; import org.hl7.fhir.r4.model.Type; import org.hl7.fhir.utilities.xhtml.XhtmlNode; @@ -175,6 +176,36 @@ public class JsonParserR4Test extends BaseTest { } + @Test + public void testDontEncodeEmptyExtensionList() { + String asXml = """ + + + + +
+ + + + +
+
+
+
+
+ """; + + ourLog.info(asXml); + + Bundle bundle = ourCtx.newXmlParser().parseResource(Bundle.class, asXml); + + String asString = ourCtx.newJsonParser().setPrettyPrint(true).encodeResourceToString(bundle); + ourLog.info(asString); + assertThat(asString, not(containsString("{ }"))); + } + + + @Test public void testEncodeExtensionOnBinaryData() { Binary b = new Binary();