diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/BaseRuntimeChildDefinition.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/BaseRuntimeChildDefinition.java index 1a681d5c93c..18b8a18b632 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/BaseRuntimeChildDefinition.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/BaseRuntimeChildDefinition.java @@ -31,6 +31,8 @@ import org.hl7.fhir.instance.model.api.IBaseReference; public abstract class BaseRuntimeChildDefinition { + private BaseRuntimeChildDefinition myReplacedParentDefinition; + public abstract IAccessor getAccessor(); public abstract BaseRuntimeElementDefinition getChildByName(String theName); @@ -66,6 +68,14 @@ public abstract class BaseRuntimeChildDefinition { return getClass().getSimpleName() + "[" + getElementName() + "]"; } + public BaseRuntimeChildDefinition getReplacedParentDefinition() { + return myReplacedParentDefinition; + } + + public void setReplacedParentDefinition(BaseRuntimeChildDefinition myReplacedParentDefinition) { + this.myReplacedParentDefinition = myReplacedParentDefinition; + } + public interface IAccessor { List getValues(IBase theTarget); diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/BaseRuntimeElementCompositeDefinition.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/BaseRuntimeElementCompositeDefinition.java index b7e658bd362..6a8be697d15 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/BaseRuntimeElementCompositeDefinition.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/BaseRuntimeElementCompositeDefinition.java @@ -263,6 +263,7 @@ public abstract class BaseRuntimeElementCompositeDefinition ext int order = childAnnotation.order(); boolean childIsChoiceType = false; boolean orderIsReplaceParent = false; + BaseRuntimeChildDefinition replacedParent = null; if (order == Child.REPLACE_PARENT) { @@ -274,7 +275,7 @@ public abstract class BaseRuntimeElementCompositeDefinition ext if (nextDef.getExtensionUrl().equals(extensionAttr.url())) { orderIsReplaceParent = true; order = nextEntry.getKey(); - orderMap.remove(nextEntry.getKey()); + replacedParent = orderMap.remove(nextEntry.getKey()); elementNames.remove(elementName); break; } @@ -293,6 +294,7 @@ public abstract class BaseRuntimeElementCompositeDefinition ext orderIsReplaceParent = true; order = nextEntry.getKey(); BaseRuntimeDeclaredChildDefinition existing = orderMap.remove(nextEntry.getKey()); + replacedParent = existing; elementNames.remove(elementName); /* @@ -450,6 +452,7 @@ public abstract class BaseRuntimeElementCompositeDefinition ext } + def.setReplacedParentDefinition(replacedParent); orderMap.put(order, def); elementNames.add(elementName); } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java index 7b124708b6c..dc2306bb17c 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java @@ -24,8 +24,6 @@ import ca.uhn.fhir.context.*; import ca.uhn.fhir.context.BaseRuntimeElementDefinition.ChildTypeEnum; import ca.uhn.fhir.model.api.*; import ca.uhn.fhir.model.api.annotation.Child; -import ca.uhn.fhir.model.api.annotation.DatatypeDef; -import ca.uhn.fhir.model.api.annotation.ResourceDef; import ca.uhn.fhir.model.base.composite.BaseCodingDt; import ca.uhn.fhir.model.base.composite.BaseContainedDt; import ca.uhn.fhir.model.primitive.IdDt; @@ -360,8 +358,6 @@ public class JsonParser extends BaseParser implements IJsonLikeParser { } } - Map parentProfileElementMetadata = getParentProfileElementMetadata(theElement); - boolean haveWrittenExtensions = false; for (CompositeChildElement nextChildElem : super.compositeChildIterator(theElement, theContainedResource, theParent, theEncodeContext)) { @@ -492,8 +488,8 @@ public class JsonParser extends BaseParser implements IJsonLikeParser { if (inArray) { theEventWriter.endArray(); } - Child parentElementChildAnnotation = parentProfileElementMetadata.get(nextChild.getElementName()); - if (isMultipleCardinality(nextChild.getMax()) || (parentElementChildAnnotation != null && isMultipleCardinality(parentElementChildAnnotation.max()))) { + BaseRuntimeChildDefinition replacedParentDefinition = nextChild.getReplacedParentDefinition(); + if (isMultipleCardinality(nextChild.getMax()) || (replacedParentDefinition != null && isMultipleCardinality(replacedParentDefinition.getMax()))) { beginArray(theEventWriter, nextChildSpecificName); inArray = true; encodeChildElementToStreamWriter(theResDef, theResource, theEventWriter, nextValue, childDef, null, theContainedResource, nextChildElem, force, theEncodeContext); @@ -591,20 +587,6 @@ public class JsonParser extends BaseParser implements IJsonLikeParser { return maxCardinality > 1 || maxCardinality == Child.MAX_UNLIMITED; } - private Map getParentProfileElementMetadata(IBase theElement) { - Class superclass = theElement.getClass().getSuperclass(); - Map parentProfileElementsMetadata = new HashMap<>(); - if(superclass.isAnnotationPresent(ResourceDef.class) || superclass.isAnnotationPresent(DatatypeDef.class)) { - for (Field field: superclass.getDeclaredFields()) { - Child childAnnotation = field.getAnnotation(Child.class); - if(childAnnotation != null) { - parentProfileElementsMetadata.put(childAnnotation.name(), childAnnotation); - } - } - } - return parentProfileElementsMetadata; - } - private void encodeCompositeElementToStreamWriter(RuntimeResourceDefinition theResDef, IBaseResource theResource, IBase theNextValue, JsonLikeWriter theEventWriter, boolean theContainedResource, CompositeChildElement theParent, EncodeContext theEncodeContext) throws IOException, DataFormatException { writeCommentsPreAndPost(theNextValue, theEventWriter);