diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/TerserUtil.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/TerserUtil.java index f02cee69a54..3b3e47b24c2 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/TerserUtil.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/TerserUtil.java @@ -218,6 +218,13 @@ public final class TerserUtil { mergeFields(theTerser, theResource, childDefinition, theFromFieldValues, theToFieldValues); } + public static void setFieldByFhirPath(FhirContext theFhirContext, FhirTerser theTerser, String theFhirPath, IBaseResource theResource, IBase theValue) { + List theFromFieldValues = theTerser.getValues(theResource, theFhirPath, true, false); + for (IBase theFromFieldValue : theFromFieldValues) { + theTerser.cloneInto(theFromFieldValue, theValue, true); + } + } + private static void replaceField(IBaseResource theFrom, IBaseResource theTo, BaseRuntimeChildDefinition childDefinition) { childDefinition.getAccessor().getFirstValueOrNull(theFrom).ifPresent(v -> { childDefinition.getMutator().setValue(theTo, v); diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/TerserUtilHelper.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/TerserUtilHelper.java index c245f5fb38c..7a9da49cd3d 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/TerserUtilHelper.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/TerserUtilHelper.java @@ -30,12 +30,24 @@ public class TerserUtilHelper { myResource = theResource; } + /** + * Sets string field at the specified FHIR path + * + * @param theField The FHIR Path to set the values at + * @param theValue The string value to be set + * @return Returns current instance + */ public TerserUtilHelper setField(String theField, String theValue) { - IBase value = TerserUtil.newElement(myContext, "string", theValue); - TerserUtil.setField(myContext, getTerser(), theField, myResource, value); + IBase value = newStringElement(theValue); + TerserUtil.setFieldByFhirPath(myContext, getTerser(), theField, myResource, value); return this; } + protected IBase newStringElement(String theValue) { + IBase value = TerserUtil.newElement(myContext, "string", theValue); + return value; + } + public List getFieldValues(String theField) { return TerserUtil.getValues(myContext, myResource, theField); } @@ -66,6 +78,10 @@ public class TerserUtilHelper { return myContext.getResourceDefinition(myResource); } + public IBase newElement(String theElementName) { + return TerserUtil.newElement(myContext, theElementName); + } + public FhirContext getContext() { return myContext; } diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/TerserUtilTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/TerserUtilTest.java index 9a84c04d76a..80e137c41af 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/TerserUtilTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/util/TerserUtilTest.java @@ -37,20 +37,20 @@ class TerserUtilTest { @Test void testCloneEidIntoResourceViaHelper() { - TerserUtilHelper idHelper = TerserUtilHelper.newHelper(ourFhirContext, "Identifier"); - idHelper - .setField("system", "http://org.com/sys") - .setField("value", "123"); - TerserUtilHelper p1Helper = TerserUtilHelper.newHelper(ourFhirContext, "Patient"); - p1Helper.setField("identifier", idHelper.getResource()); + p1Helper.setField("identifier.system", "http://org.com/sys"); + p1Helper.setField("identifier.value", "123"); TerserUtilHelper p2Helper = TerserUtilHelper.newHelper(ourFhirContext, "Patient"); RuntimeResourceDefinition definition = p1Helper.getResourceDefinition(); - TerserUtil.cloneEidIntoResource(ourFhirContext, definition.getChildByName("identifier"), idHelper.getResource(), p2Helper.getResource()); + TerserUtil.cloneEidIntoResource(ourFhirContext, definition.getChildByName("identifier"), p1Helper.getResource(), p2Helper.getResource()); assertEquals(1, p2Helper.getFieldValues("identifier").size()); - assertEquals(p1Helper.getFieldValues("identifier").get(0), p2Helper.getFieldValues("identifier").get(0)); + + Identifier id1 = (Identifier) p1Helper.getFieldValues("identifier").get(0); + Identifier id2 = (Identifier) p2Helper.getFieldValues("identifier").get(0); + assertTrue(id1.equalsDeep(id2)); + assertFalse(id1.equals(id2)); } @Test