From bb59e2d73a46ff6ae2e28b7378f59e9e3956729a Mon Sep 17 00:00:00 2001
From: James Agnew
Date: Thu, 1 Nov 2018 10:25:13 -0400
Subject: [PATCH] Fix broken test
---
.../java/ca/uhn/fhir/util/FhirTerser.java | 393 +++++++++---------
1 file changed, 195 insertions(+), 198 deletions(-)
diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/FhirTerser.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/FhirTerser.java
index 281b6d94077..7fa863b524d 100644
--- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/FhirTerser.java
+++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/util/FhirTerser.java
@@ -28,9 +28,9 @@ import static org.apache.commons.lang3.StringUtils.*;
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -96,34 +96,36 @@ public class FhirTerser {
/**
* Clones all values from a source object into the equivalent fields in a target object
- * @param theSource The source object (must not be null)
- * @param theTarget The target object to copy values into (must not be null)
+ *
+ * @param theSource The source object (must not be null)
+ * @param theTarget The target object to copy values into (must not be null)
* @param theIgnoreMissingFields The ignore fields in the target which do not exist (if false, an exception will be thrown if the target is unable to accept a value from the source)
+ * @return Returns the target (which will be the same object that was passed into theTarget) for easy chaining
*/
- public void cloneInto(IBase theSource, IBase theTarget, boolean theIgnoreMissingFields) {
+ public IBase cloneInto(IBase theSource, IBase theTarget, boolean theIgnoreMissingFields) {
Validate.notNull(theSource, "theSource must not be null");
Validate.notNull(theTarget, "theTarget must not be null");
-
+
if (theSource instanceof IPrimitiveType>) {
if (theTarget instanceof IPrimitiveType>) {
- ((IPrimitiveType>)theTarget).setValueAsString(((IPrimitiveType>)theSource).getValueAsString());
- return;
+ ((IPrimitiveType>) theTarget).setValueAsString(((IPrimitiveType>) theSource).getValueAsString());
+ return theSource;
}
if (theIgnoreMissingFields) {
- return;
+ return theSource;
}
throw new DataFormatException("Can not copy value from primitive of type " + theSource.getClass().getName() + " into type " + theTarget.getClass().getName());
}
-
- BaseRuntimeElementCompositeDefinition> sourceDef = (BaseRuntimeElementCompositeDefinition>) myContext.getElementDefinition(theSource.getClass());
+
+ BaseRuntimeElementCompositeDefinition> sourceDef = (BaseRuntimeElementCompositeDefinition>) myContext.getElementDefinition(theSource.getClass());
BaseRuntimeElementCompositeDefinition> targetDef = (BaseRuntimeElementCompositeDefinition>) myContext.getElementDefinition(theTarget.getClass());
-
+
List children = sourceDef.getChildren();
if (sourceDef instanceof RuntimeExtensionDtDefinition) {
- children = ((RuntimeExtensionDtDefinition)sourceDef).getChildrenIncludingUrl();
+ children = ((RuntimeExtensionDtDefinition) sourceDef).getChildrenIncludingUrl();
}
-
- for (BaseRuntimeChildDefinition nextChild : children) {
+
+ for (BaseRuntimeChildDefinition nextChild : children)
for (IBase nextValue : nextChild.getAccessor().getValues(theSource)) {
String elementName = nextChild.getChildNameByDatatype(nextValue.getClass());
BaseRuntimeChildDefinition targetChild = targetDef.getChildByName(elementName);
@@ -133,14 +135,15 @@ public class FhirTerser {
}
throw new DataFormatException("Type " + theTarget.getClass().getName() + " does not have a child with name " + elementName);
}
-
- BaseRuntimeElementDefinition> childDef = targetChild.getChildByName(elementName);
- IBase target = childDef.newInstance();
+
+ BaseRuntimeElementDefinition> element = myContext.getElementDefinition(nextValue.getClass());
+ IBase target = element.newInstance();
+
targetChild.getMutator().addValue(theTarget, target);
cloneInto(nextValue, target, theIgnoreMissingFields);
}
- }
-
+
+ return theTarget;
}
/**
@@ -153,11 +156,9 @@ public class FhirTerser {
* Note on scope: This method will descend into any contained resources ({@link IResource#getContained()}) as well, but will not descend into linked resources (e.g.
* {@link BaseResourceReferenceDt#getResource()}) or embedded resources (e.g. Bundle.entry.resource)
*
- *
- * @param theResource
- * The resource instance to search. Must not be null.
- * @param theType
- * The type to search for. Must not be null.
+ *
+ * @param theResource The resource instance to search. Must not be null.
+ * @param theType The type to search for. Must not be null.
* @return Returns a list of all matching elements
*/
public List getAllPopulatedChildElementsOfType(IBaseResource theResource, final Class theType) {
@@ -274,7 +275,7 @@ public class FhirTerser {
.collect(Collectors.toList());
if (theAddExtension
- && (!(theCurrentObj instanceof IBaseExtension) || (extensionDts.isEmpty() && theSubList.size() == 1))) {
+ && (!(theCurrentObj instanceof IBaseExtension) || (extensionDts.isEmpty() && theSubList.size() == 1))) {
extensionDts.add(createEmptyExtensionDt((ISupportsUndeclaredExtensions) theCurrentObj, extensionUrl));
}
@@ -286,7 +287,7 @@ public class FhirTerser {
extensionDts = ((IBaseExtension) theCurrentObj).getExtension();
if (theAddExtension
- && (extensionDts.isEmpty() && theSubList.size() == 1)) {
+ && (extensionDts.isEmpty() && theSubList.size() == 1)) {
extensionDts.add(createEmptyExtensionDt((IBaseExtension) theCurrentObj, extensionUrl));
}
@@ -311,7 +312,7 @@ public class FhirTerser {
.collect(Collectors.toList());
if (theAddExtension
- && (!(theCurrentObj instanceof IBaseExtension) || (extensions.isEmpty() && theSubList.size() == 1))) {
+ && (!(theCurrentObj instanceof IBaseExtension) || (extensions.isEmpty() && theSubList.size() == 1))) {
extensions.add(createEmptyExtension((IBaseHasExtensions) theCurrentObj, extensionUrl));
}
@@ -396,7 +397,7 @@ public class FhirTerser {
.collect(Collectors.toList());
if (theAddExtension
- && (!(theCurrentObj instanceof IBaseExtension) || (extensions.isEmpty() && theSubList.size() == 1))) {
+ && (!(theCurrentObj instanceof IBaseExtension) || (extensions.isEmpty() && theSubList.size() == 1))) {
extensions.add(createEmptyModifierExtension((IBaseHasModifierExtensions) theCurrentObj, extensionUrl));
}
@@ -478,7 +479,7 @@ public class FhirTerser {
* type {@link Object}.
*
* @param theResource The resource instance to be accessed. Must not be null.
- * @param thePath The path for the element to be accessed.
+ * @param thePath The path for the element to be accessed.
* @return A list of values of type {@link Object}.
*/
public List