From 8636e4f155c6b474ef6da54214c8549d0a8e35f5 Mon Sep 17 00:00:00 2001 From: jamesagnew Date: Thu, 27 May 2021 07:30:09 -0400 Subject: [PATCH] Test fix --- .../main/java/ca/uhn/fhir/util/TerserUtil.java | 18 ++++++------------ .../java/ca/uhn/fhir/util/TerserUtilTest.java | 13 +++++++++++++ 2 files changed, 19 insertions(+), 12 deletions(-) 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 4f447ab25c3..3b652f931f8 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 @@ -28,12 +28,10 @@ import ca.uhn.fhir.context.RuntimeResourceDefinition; import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.tuple.Triple; import org.hl7.fhir.instance.model.api.IBase; -import org.hl7.fhir.instance.model.api.IBaseEnumeration; import org.hl7.fhir.instance.model.api.IBaseResource; import org.hl7.fhir.instance.model.api.IPrimitiveType; import org.slf4j.Logger; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Arrays; import java.util.Collection; @@ -47,18 +45,12 @@ import static org.slf4j.LoggerFactory.getLogger; public final class TerserUtil { - private static final Logger ourLog = getLogger(TerserUtil.class); - public static final String FIELD_NAME_IDENTIFIER = "identifier"; - - private static final String EQUALS_DEEP = "equalsDeep"; - /** * Exclude for id, identifier and meta fields of a resource. */ public static final Collection IDS_AND_META_EXCLUDES = Collections.unmodifiableSet(Stream.of("id", "identifier", "meta").collect(Collectors.toSet())); - /** * Exclusion predicate for id, identifier, meta fields. */ @@ -68,7 +60,6 @@ public final class TerserUtil { return !IDS_AND_META_EXCLUDES.contains(s); } }; - /** * Exclusion predicate for id/identifier, meta and fields with empty values. This ensures that source / target resources, * empty source fields will not results in erasure of target fields. @@ -84,7 +75,6 @@ public final class TerserUtil { return !isSourceFieldEmpty; } }; - /** * Exclusion predicate for keeping all fields. */ @@ -94,6 +84,8 @@ public final class TerserUtil { return true; } }; + private static final Logger ourLog = getLogger(TerserUtil.class); + private static final String EQUALS_DEEP = "equalsDeep"; private TerserUtil() { } @@ -448,9 +440,11 @@ public final class TerserUtil { private static void replaceField(FhirTerser theTerser, IBaseResource theFrom, IBaseResource theTo, BaseRuntimeChildDefinition childDefinition) { List fromValues = childDefinition.getAccessor().getValues(theFrom); List toValues = childDefinition.getAccessor().getValues(theTo); - clear(toValues); + if (fromValues != toValues) { + clear(toValues); - mergeFields(theTerser, theTo, childDefinition, fromValues, toValues); + mergeFields(theTerser, theTo, childDefinition, fromValues, toValues); + } } /** 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 9dafe34dc63..5611a3eca94 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 @@ -330,6 +330,19 @@ class TerserUtilTest { assertEquals("Doe", p2.getName().get(0).getFamily()); } + @Test + public void testReplaceFields_SameValues() { + Patient p1 = new Patient(); + p1.addName().setFamily("Doe"); + Patient p2 = new Patient(); + p2.setName(p1.getName()); + + TerserUtil.replaceField(ourFhirContext, "name", p1, p2); + + assertEquals(1, p2.getName().size()); + assertEquals("Doe", p2.getName().get(0).getFamily()); + } + @Test public void testReplaceFieldsByPredicate() { Patient p1 = new Patient();