From e3cc83a694c8a542ae53a91ea618a4d4df13180c Mon Sep 17 00:00:00 2001 From: JasonRoberts-smile <85363818+JasonRoberts-smile@users.noreply.github.com> Date: Thu, 11 Apr 2024 20:19:56 -0400 Subject: [PATCH] fix bug (#5843) --- .../java/ca/uhn/fhir/parser/BaseParser.java | 25 ++++++++++--------- .../changelog/7_2_0/5842-json-parser-npe.yaml | 5 ++++ .../ca/uhn/fhir/parser/JsonParserR4Test.java | 12 +++++++++ 3 files changed, 30 insertions(+), 12 deletions(-) create mode 100644 hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_2_0/5842-json-parser-npe.yaml 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 a6534da7844..9dbea4a3d7b 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 @@ -609,12 +609,14 @@ public abstract class BaseParser implements IParser { private boolean isStripVersionsFromReferences( CompositeChildElement theCompositeChildElement, IBaseResource theResource) { - Set autoVersionReferencesAtPathExtensions = - MetaUtil.getAutoVersionReferencesAtPath(theResource.getMeta(), myContext.getResourceType(theResource)); + if (theResource != null) { + Set autoVersionReferencesAtPathExtensions = MetaUtil.getAutoVersionReferencesAtPath( + theResource.getMeta(), myContext.getResourceType(theResource)); - if (!autoVersionReferencesAtPathExtensions.isEmpty() - && theCompositeChildElement.anyPathMatches(autoVersionReferencesAtPathExtensions)) { - return false; + if (!autoVersionReferencesAtPathExtensions.isEmpty() + && theCompositeChildElement.anyPathMatches(autoVersionReferencesAtPathExtensions)) { + return false; + } } Boolean stripVersionsFromReferences = myStripVersionsFromReferences; @@ -622,21 +624,20 @@ public abstract class BaseParser implements IParser { return stripVersionsFromReferences; } - if (myContext.getParserOptions().isStripVersionsFromReferences() == false) { + if (!myContext.getParserOptions().isStripVersionsFromReferences()) { return false; } Set dontStripVersionsFromReferencesAtPaths = myDontStripVersionsFromReferencesAtPaths; - if (dontStripVersionsFromReferencesAtPaths != null) { - if (dontStripVersionsFromReferencesAtPaths.isEmpty() == false - && theCompositeChildElement.anyPathMatches(dontStripVersionsFromReferencesAtPaths)) { - return false; - } + if (dontStripVersionsFromReferencesAtPaths != null + && !dontStripVersionsFromReferencesAtPaths.isEmpty() + && theCompositeChildElement.anyPathMatches(dontStripVersionsFromReferencesAtPaths)) { + return false; } dontStripVersionsFromReferencesAtPaths = myContext.getParserOptions().getDontStripVersionsFromReferencesAtPaths(); - return dontStripVersionsFromReferencesAtPaths.isEmpty() != false + return dontStripVersionsFromReferencesAtPaths.isEmpty() || !theCompositeChildElement.anyPathMatches(dontStripVersionsFromReferencesAtPaths); } diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_2_0/5842-json-parser-npe.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_2_0/5842-json-parser-npe.yaml new file mode 100644 index 00000000000..4f6f8570ff9 --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/7_2_0/5842-json-parser-npe.yaml @@ -0,0 +1,5 @@ +--- +type: fix +issue: 5842 +title: "Fixed a bug where an NPE was being thrown when trying to serialize a FHIR fragment (e.g., backboneElement +, compound datatype) to a string representation if the fragment contains a `Reference`." 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 47b73e6d0cc..8964bd244f2 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 @@ -1168,6 +1168,18 @@ public class JsonParserR4Test extends BaseTest { assertEquals(expected, actual); } + @Test + public void testEncodeToString_CompoundTypeWithReference() { + Identifier identifier = new Identifier(); + identifier.setSystem("http://system.org"); + identifier.setValue("123"); + Reference reference = new Reference("Organization/1"); + identifier.setAssigner(reference); + String expected = "{\"system\":\"http://system.org\",\"value\":\"123\",\"assigner\":{\"reference\":\"Organization/1\"}}"; + String actual = ourCtx.newJsonParser().encodeToString(identifier); + assertEquals(expected, actual); + } + @Test public void testEncodeToString_Resource() { Patient p = new Patient();