Work on multitype extensions

This commit is contained in:
James Agnew 2017-01-25 22:35:57 -05:00
parent 95b2751829
commit 2eb780a690
8 changed files with 171 additions and 145 deletions

View File

@ -216,18 +216,6 @@ public abstract class BaseRuntimeElementCompositeDefinition<T extends IBase> ext
orderToElementDef = newOrderToExtensionDef;
}
// while (orderToElementDef.size() > 0 && orderToElementDef.firstKey() <
// 0) {
// BaseRuntimeDeclaredChildDefinition elementDef =
// orderToElementDef.remove(orderToElementDef.firstKey());
// if (elementDef.getElementName().equals("identifier")) {
// orderToElementDef.put(theIdentifierOrder, elementDef);
// } else {
// throw new ConfigurationException("Don't know how to handle element: "
// + elementDef.getElementName());
// }
// }
TreeSet<Integer> orders = new TreeSet<Integer>();
orders.addAll(orderToElementDef.keySet());
orders.addAll(orderToExtensionDef.keySet());
@ -372,7 +360,7 @@ public abstract class BaseRuntimeElementCompositeDefinition<T extends IBase> ext
def = new RuntimeChildDirectResource(nextField, childAnnotation, descriptionAnnotation, elementName);
} else {
childIsChoiceType |= choiceTypes.size() > 1;
if (childIsChoiceType && !BaseResourceReferenceDt.class.isAssignableFrom(nextElementType) && !IBaseReference.class.isAssignableFrom(nextElementType)) {
if (extensionAttr == null && childIsChoiceType && !BaseResourceReferenceDt.class.isAssignableFrom(nextElementType) && !IBaseReference.class.isAssignableFrom(nextElementType)) {
def = new RuntimeChildChoiceDefinition(nextField, elementName, childAnnotation, descriptionAnnotation, choiceTypes);
} else if (extensionAttr != null) {
/*

View File

@ -38,6 +38,9 @@ public class RuntimeChildChoiceDefinition extends BaseRuntimeDeclaredChildDefini
private String myReferenceSuffix;
private List<Class<? extends IBaseResource>> myResourceTypes;
/**
* Constructor
*/
public RuntimeChildChoiceDefinition(Field theField, String theElementName, Child theChildAnnotation, Description theDescriptionAnnotation, List<Class<? extends IBase>> theChoiceTypes) {
super(theField, theChildAnnotation, theDescriptionAnnotation, theElementName);
@ -45,6 +48,8 @@ public class RuntimeChildChoiceDefinition extends BaseRuntimeDeclaredChildDefini
}
/**
* Constructor
*
* For extension, if myChoiceTypes will be set some other way
*/
RuntimeChildChoiceDefinition(Field theField, String theElementName, Child theChildAnnotation, Description theDescriptionAnnotation) {

View File

@ -22,32 +22,26 @@ package ca.uhn.fhir.context;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.lang.reflect.Modifier;
import java.util.*;
import org.hl7.fhir.instance.model.api.IBase;
import org.hl7.fhir.instance.model.api.IBaseResource;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.model.api.IElement;
import ca.uhn.fhir.model.api.annotation.Child;
import ca.uhn.fhir.model.api.annotation.Description;
import ca.uhn.fhir.model.api.annotation.Extension;
public class RuntimeChildDeclaredExtensionDefinition extends BaseRuntimeDeclaredChildDefinition {
public class RuntimeChildDeclaredExtensionDefinition extends RuntimeChildChoiceDefinition {
private BaseRuntimeElementDefinition<?> myChildDef;
private Class<? extends IBase> myChildType;
private String myDatatypeChildName;
private boolean myDefinedLocally;
private String myExtensionUrl;
private boolean myModifier;
private Map<String, RuntimeChildDeclaredExtensionDefinition> myUrlToChildExtension;
private volatile Object myInstanceConstructorArguments;
private Class<?> myEnumerationType;
private Class<? extends IBase> myChildType;
private RuntimeResourceBlockDefinition myChildResourceBlock;
/**
* @param theBoundTypeBinder
@ -56,15 +50,25 @@ public class RuntimeChildDeclaredExtensionDefinition extends BaseRuntimeDeclared
* @param theDefinedLocally
* See {@link Extension#definedLocally()}
*/
@SuppressWarnings("unchecked")
RuntimeChildDeclaredExtensionDefinition(Field theField, Child theChild, Description theDescriptionAnnotation, Extension theExtension, String theElementName, String theExtensionUrl, Class<? extends IBase> theChildType, Object theBoundTypeBinder)
throws ConfigurationException {
super(theField, theChild, theDescriptionAnnotation, theElementName);
super(theField, theElementName, theChild, theDescriptionAnnotation);
assert isNotBlank(theExtensionUrl);
myExtensionUrl = theExtensionUrl;
myChildType = theChildType;
myDefinedLocally = theExtension.definedLocally();
myModifier = theExtension.isModifier();
myInstanceConstructorArguments = theBoundTypeBinder;
List<Class<? extends IBase>> choiceTypes = new ArrayList<Class<? extends IBase>>();
for (Class<? extends IElement> next : theChild.type()) {
choiceTypes.add(next);
}
if (Modifier.isAbstract(theField.getType().getModifiers()) == false) {
choiceTypes.add((Class<? extends IBase>) theField.getType());
}
setChoiceTypes(choiceTypes);
}
@Override
@ -82,57 +86,49 @@ public class RuntimeChildDeclaredExtensionDefinition extends BaseRuntimeDeclared
}
@Override
public BaseRuntimeElementDefinition<?> getChildByName(String theName) {
if (myDatatypeChildName != null) {
if (myDatatypeChildName.equals(theName)) {
return myChildDef;
public String getChildNameByDatatype(Class<? extends IBase> theDatatype) {
String retVal = super.getChildNameByDatatype(theDatatype);
if (retVal == null) {
if (myModifier) {
return "modifierExtension";
}else {
return null;
return "extension";
}
} else {
return null;
}
return retVal;
}
@Override
public BaseRuntimeElementDefinition<?> getChildElementDefinitionByDatatype(Class<? extends IBase> theType) {
if (myChildType.equals(theType)) {
return myChildDef;
public BaseRuntimeElementDefinition<?> getChildElementDefinitionByDatatype(Class<? extends IBase> theDatatype) {
if (myChildResourceBlock != null) {
if (myChildResourceBlock.getImplementingClass().equals(theDatatype)) {
return myChildResourceBlock;
}
return null;
}
return super.getChildElementDefinitionByDatatype(theDatatype);
}
public RuntimeChildDeclaredExtensionDefinition getChildExtensionForUrl(String theUrl) {
return myUrlToChildExtension.get(theUrl);
}
@Override
public String getChildNameByDatatype(Class<? extends IBase> theDatatype) {
if (myChildType.equals(theDatatype) && myDatatypeChildName != null) {
return myDatatypeChildName;
} else {
return "extension";
}
}
// @Override
// public String getChildNameByDatatype(Class<? extends IBase> theDatatype) {
// String retVal = super.getChildNameByDatatype(theDatatype);
// if (retVal )
// if (myChildType.equals(theDatatype) && myDatatypeChildName != null) {
// return myDatatypeChildName;
// } else {
// return "extension";
// }
// }
public Class<? extends IBase> getChildType() {
return myChildType;
}
@Override
public String getExtensionUrl() {
return myExtensionUrl;
}
@Override
public BaseRuntimeElementDefinition<?> getSingleChildOrThrow() {
return myChildDef;
}
@Override
public Set<String> getValidChildNames() {
return Collections.emptySet();
}
public boolean isDefinedLocally() {
return myDefinedLocally;
@ -143,15 +139,6 @@ public class RuntimeChildDeclaredExtensionDefinition extends BaseRuntimeDeclared
return myModifier;
}
public IBase newInstance() {
try {
return myChildType.newInstance();
} catch (InstantiationException e) {
throw new ConfigurationException("Failed to instantiate type:" + myChildType.getName(), e);
} catch (IllegalAccessException e) {
throw new ConfigurationException("Failed to instantiate type:" + myChildType.getName(), e);
}
}
@Override
void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
@ -164,29 +151,22 @@ public class RuntimeChildDeclaredExtensionDefinition extends BaseRuntimeDeclared
* built-in types, e.g. custom structures or custom extensions
*/
if (elementDef == null) {
if (Modifier.isAbstract(myChildType.getModifiers()) == false) {
elementDef = theContext.getElementDefinition(myChildType);
}
if (elementDef instanceof RuntimePrimitiveDatatypeDefinition || elementDef instanceof RuntimeCompositeDatatypeDefinition) {
myDatatypeChildName = "value" + elementDef.getName().substring(0, 1).toUpperCase() + elementDef.getName().substring(1);
if ("valueResourceReference".equals(myDatatypeChildName)) {
// Per one of the examples here: http://hl7.org/implement/standards/fhir/extensibility.html#extension
myDatatypeChildName = "valueResource";
List<Class<? extends IBaseResource>> types = new ArrayList<Class<? extends IBaseResource>>();
types.add(IResource.class);
myChildDef = findResourceReferenceDefinition(theClassToElementDefinitions);
} else {
myChildDef = elementDef;
}
} else {
if (elementDef instanceof RuntimeResourceBlockDefinition) {
RuntimeResourceBlockDefinition extDef = ((RuntimeResourceBlockDefinition) elementDef);
for (RuntimeChildDeclaredExtensionDefinition next : extDef.getExtensions()) {
myUrlToChildExtension.put(next.getExtensionUrl(), next);
}
myChildDef = extDef;
myChildResourceBlock = (RuntimeResourceBlockDefinition)elementDef;
}
myUrlToChildExtension = Collections.unmodifiableMap(myUrlToChildExtension);
super.sealAndInitialize(theContext, theClassToElementDefinitions);
}
}

View File

@ -1487,7 +1487,8 @@ class ParserState<T> {
RuntimeChildDeclaredExtensionDefinition declaredExtension = myDefinition.getChildExtensionForUrl(theUrlAttr);
if (declaredExtension != null) {
if (myChildInstance == null) {
myChildInstance = myDefinition.newInstance();
BaseRuntimeElementDefinition<?> child = declaredExtension.getChildByName(theElement.getName().getLocalPart());
myChildInstance = child.newInstance();
myDefinition.getMutator().addValue(myParentInstance, myChildInstance);
}
BaseState newState = new DeclaredExtensionState(getPreResourceState(), declaredExtension, myChildInstance);

View File

@ -53,8 +53,6 @@ public class DaoConfig {
// ***
private long myExpireSearchResultsAfterMillis = DateUtils.MILLIS_PER_HOUR;
private int myHardSearchLimit = 1000;
private int myHardTagListLimit = 1000;
private int myIncludeLimit = 2000;
@ -325,8 +323,17 @@ public class DaoConfig {
myExpireSearchResultsAfterMillis = theExpireSearchResultsAfterMillis;
}
public void setHardSearchLimit(int theHardSearchLimit) {
myHardSearchLimit = theHardSearchLimit;
/**
* Do not call this method, it exists only for legacy reasons. It
* will be removed in a future version. Configure the page size on your
* paging provider instead.
*
* @deprecated This method does not do anything. Configure the page size on your
* paging provider instead. Deprecated in HAPI FHIR 2.3 (Jan 2017)
*/
@Deprecated
public void setHardSearchLimit(@SuppressWarnings("unused") int theHardSearchLimit) {
// this method does nothing
}
/**

View File

@ -54,7 +54,6 @@ public class TestDstu3Config extends BaseJavaConfigDstu3 {
retVal.setAllowExternalReferences(true);
retVal.getTreatBaseUrlsAsLocal().add("http://fhirtest.uhn.ca/baseDstu3");
retVal.getTreatBaseUrlsAsLocal().add("https://fhirtest.uhn.ca/baseDstu3");
retVal.setHardSearchLimit(500);
return retVal;
}

View File

@ -919,7 +919,7 @@ public class XmlParserDstu2Test {
public void testEncodeAndParseProfiledDatatypeChoice() throws Exception {
IParser xmlParser = ourCtx.newXmlParser();
String input = IOUtils.toString(XmlParser.class.getResourceAsStream("/medicationstatement_invalidelement.xml"));
String input = IOUtils.toString(XmlParser.class.getResourceAsStream("/medicationstatement_invalidelement.xml"), StandardCharsets.UTF_8);
MedicationStatement ms = xmlParser.parseResource(MedicationStatement.class, input);
SimpleQuantityDt q = (SimpleQuantityDt) ms.getDosage().get(0).getQuantity();
assertEquals("1", q.getValueElement().getValueAsString());
@ -1869,7 +1869,7 @@ public class XmlParserDstu2Test {
@Test
public void testParseAndEncodeBundle() throws Exception {
String content = IOUtils.toString(XmlParserDstu2Test.class.getResourceAsStream("/bundle-example.xml"));
String content = IOUtils.toString(XmlParserDstu2Test.class.getResourceAsStream("/bundle-example.xml"), StandardCharsets.UTF_8);
Bundle parsed = ourCtx.newXmlParser().parseBundle(content);
assertEquals("Bundle/example/_history/1", parsed.getId().getValue());
@ -1904,7 +1904,7 @@ public class XmlParserDstu2Test {
@Test
public void testParseAndEncodeBundleNewStyle() throws Exception {
String content = IOUtils.toString(XmlParserDstu2Test.class.getResourceAsStream("/bundle-example.xml"));
String content = IOUtils.toString(XmlParserDstu2Test.class.getResourceAsStream("/bundle-example.xml"), StandardCharsets.UTF_8);
IParser newXmlParser = ourCtx.newXmlParser();
ca.uhn.fhir.model.dstu2.resource.Bundle parsed = newXmlParser.parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, content);
@ -2411,12 +2411,14 @@ public class XmlParserDstu2Test {
// TODO: implement this test, make sure we handle ID and meta correctly in Binary
}
/**
* See #191
*/
@Test
public void testParseBundleWithLinksOfUnknownRelation() throws Exception {
String input = IOUtils.toString(XmlParserDstu2Test.class.getResourceAsStream("/bundle_orion.xml"));
String input = IOUtils.toString(XmlParserDstu2Test.class.getResourceAsStream("/bundle_orion.xml"), StandardCharsets.UTF_8);
ca.uhn.fhir.model.dstu2.resource.Bundle parsed = ourCtx.newXmlParser().parseResource(ca.uhn.fhir.model.dstu2.resource.Bundle.class, input);
Link link = parsed.getLink().get(0);

View File

@ -10,13 +10,7 @@ import static org.junit.Assert.assertThat;
import java.util.ArrayList;
import java.util.List;
import org.hl7.fhir.dstu3.model.Bundle;
import org.hl7.fhir.dstu3.model.DateTimeType;
import org.hl7.fhir.dstu3.model.Medication;
import org.hl7.fhir.dstu3.model.MedicationRequest;
import org.hl7.fhir.dstu3.model.Patient;
import org.hl7.fhir.dstu3.model.Quantity;
import org.hl7.fhir.dstu3.model.StringType;
import org.hl7.fhir.dstu3.model.*;
import org.hl7.fhir.instance.model.api.IBase;
import org.junit.AfterClass;
import org.junit.Before;
@ -49,6 +43,63 @@ public class CustomTypeDstu3Test {
TestUtil.clearAllStaticFieldsForUnitTest();
}
@SuppressWarnings("serial")
@ResourceDef
public static class PatientWithExtensionWithTwoTypes extends Patient {
@Child(name = "foo", type = { DateTimeType.class, Period.class }, min = 0, max = 1)
@Extension(url = "http://example.com/extension#foo", definedLocally = true, isModifier = false)
@Description(shortDefinition = "Some description")
private Type foo;
public Type getFoo() {
return foo;
}
public void setFoo(Type theFoo) {
foo = theFoo;
}
}
@Test
public void testExtensionWithTwoTypes() {
PatientWithExtensionWithTwoTypes pt = new PatientWithExtensionWithTwoTypes();
pt.setFoo(new DateTimeType("2011-01-01T00:00:00Z"));
String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(pt);
ourLog.info(encoded);
pt = ourCtx.newXmlParser().parseResource(PatientWithExtensionWithTwoTypes.class, encoded);
assertEquals("2011-01-01T00:00:00Z", ((DateTimeType)pt.getFoo()).getValueAsString());
}
@SuppressWarnings("serial")
@ResourceDef
public static class PatientWithExtensionWithOneTypes extends Patient {
@Child(name = "foo", type = { DateTimeType.class }, min = 0, max = 1)
@Extension(url = "http://example.com/extension#foo", definedLocally = true, isModifier = false)
@Description(shortDefinition = "Some description")
private Type foo;
public Type getFoo() {
return foo;
}
public void setFoo(Type theFoo) {
foo = theFoo;
}
}
@Test
public void testExtensionWithOneTypes() {
PatientWithExtensionWithOneTypes pt = new PatientWithExtensionWithOneTypes();
pt.setFoo(new DateTimeType("2011-01-01T00:00:00Z"));
String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(pt);
ourLog.info(encoded);
pt = ourCtx.newXmlParser().parseResource(PatientWithExtensionWithOneTypes.class, encoded);
assertEquals("2011-01-01T00:00:00Z", ((DateTimeType)pt.getFoo()).getValueAsString());
}
/**
* See #364
@ -71,8 +122,7 @@ public class CustomTypeDstu3Test {
"<CustomResource xmlns=\"http://hl7.org/fhir\">",
"<meta><profile value=\"http://hl7.org/fhir/profiles/custom-resource\"/></meta>",
"<baseValueCustomDate><date value=\"2016-05-13\"/></baseValueCustomDate>",
"</CustomResource>"
));
"</CustomResource>"));
//@formatter:on
CustomResource364Dstu3 parsedResource = parser.parseResource(CustomResource364Dstu3.class, xml);
@ -99,15 +149,13 @@ public class CustomTypeDstu3Test {
"<CustomResource xmlns=\"http://hl7.org/fhir\">",
"<meta><profile value=\"http://hl7.org/fhir/profiles/custom-resource\"/></meta>",
"<baseValueString value=\"2016-05-13\"/>",
"</CustomResource>"
));
"</CustomResource>"));
//@formatter:on
CustomResource364Dstu3 parsedResource = parser.parseResource(CustomResource364Dstu3.class, xml);
assertEquals("2016-05-13", ((StringType) parsedResource.getBaseValues()).getValueAsString());
}
@Test
public void parseBundleWithResourceDirective() {
String input = createBundle(createResource(false), createResource(true));
@ -148,7 +196,6 @@ public class CustomTypeDstu3Test {
assertEquals("185 cm", parsed.getWeight().getValue());
}
@Test
public void parseResourceWithNoDirective() {
String input = createResource(true);
@ -411,8 +458,7 @@ public class CustomTypeDstu3Test {
b.append(" <given value=\"Mario\"/>\n");
b.append(" </name>\n");
b.append("</Patient>");
String input =
b.toString();
String input = b.toString();
return input;
}
@ -596,10 +642,8 @@ public class CustomTypeDstu3Test {
}
@ResourceDef()
public static class MyMedication extends Medication
{
public static class MyMedication extends Medication {
private static final long serialVersionUID = 1L;