[(fixjsongenerationtomaster)] When max cardinality in profile is changed to 1 output JSON contain an array.

This commit is contained in:
Petro Mykhaylyshyn 2020-01-09 15:07:19 +02:00 committed by James Agnew
parent a234b1f8a9
commit fdab81361c
3 changed files with 16 additions and 21 deletions

View File

@ -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<IBase> getValues(IBase theTarget);

View File

@ -263,6 +263,7 @@ public abstract class BaseRuntimeElementCompositeDefinition<T extends IBase> 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<T extends IBase> 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<T extends IBase> 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<T extends IBase> ext
}
def.setReplacedParentDefinition(replacedParent);
orderMap.put(order, def);
elementNames.add(elementName);
}

View File

@ -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<String, Child> 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<String, Child> getParentProfileElementMetadata(IBase theElement) {
Class<?> superclass = theElement.getClass().getSuperclass();
Map<String, Child> 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);