Fix: Replace basic field with empty value in TerserUtil (#4842)

* add test proving bug

* fix: handle empty fromValue in TerserUtil.replaceField

* add comment to TerserUtilTest.testReplaceFieldByEmptyValue
This commit is contained in:
Nicolai Gjøderum 2023-05-11 05:09:43 +02:00 committed by GitHub
parent c052663561
commit 679b809bf3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 2 deletions

View File

@ -441,7 +441,10 @@ public final class TerserUtil {
private static void replaceField(FhirTerser theTerser, IBaseResource theFrom, IBaseResource theTo, BaseRuntimeChildDefinition childDefinition) {
List<IBase> fromValues = childDefinition.getAccessor().getValues(theFrom);
List<IBase> toValues = childDefinition.getAccessor().getValues(theTo);
if (fromValues != toValues) {
if (fromValues.isEmpty() && !toValues.isEmpty()) {
childDefinition.getMutator().setValue(theTo, null);
} else if (fromValues != toValues) {
clear(toValues);
mergeFields(theTerser, theTo, childDefinition, fromValues, toValues);
@ -529,7 +532,6 @@ public final class TerserUtil {
* Creates a new element taking into consideration elements with choice that are not directly retrievable by element
* name
*
*
* @param theFhirTerser
* @param theChildDefinition Child to create a new instance for
* @param theFromFieldValue The base parent field

View File

@ -468,6 +468,17 @@ class TerserUtilTest {
assertEquals(1, p2.getName().size());
assertEquals("Doe", p2.getName().get(0).getFamily());
}
@Test
public void testReplaceFieldByEmptyValue() {
Patient p1 = new Patient();
Patient p2 = new Patient();
p2.setActive(true);
TerserUtil.replaceField(ourFhirContext, "active", p1, p2);
// expect p2 to have 'active removed'
assertFalse(p2.hasActive());
}
@Test
public void testReplaceFieldsByPredicate() {