Fix #504 - Error if custom type has illegal extensions

This commit is contained in:
James Agnew 2017-01-13 21:46:16 -06:00
parent fa1ad5ba85
commit dbc6abc658
6 changed files with 67 additions and 93 deletions

View File

@ -21,11 +21,7 @@ package ca.uhn.fhir.context;
*/ */
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.util.ArrayList; import java.util.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.IBase;
@ -136,6 +132,11 @@ public abstract class BaseRuntimeElementDefinition<T extends IBase> {
return myName; return myName;
} }
public boolean hasExtensions() {
validateSealed();
return myExtensions.size() > 0;
}
public boolean isStandardType() { public boolean isStandardType() {
return myStandardType; return myStandardType;
} }

View File

@ -32,15 +32,7 @@ import java.util.*;
import java.util.Map.Entry; import java.util.Map.Entry;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.instance.model.api.*;
import org.hl7.fhir.instance.model.api.IBaseBackboneElement;
import org.hl7.fhir.instance.model.api.IBaseDatatype;
import org.hl7.fhir.instance.model.api.IBaseDatatypeElement;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IBaseXhtml;
import org.hl7.fhir.instance.model.api.ICompositeType;
import org.hl7.fhir.instance.model.api.IIdType;
import org.hl7.fhir.instance.model.api.IPrimitiveType;
import ca.uhn.fhir.model.api.ExtensionDt; import ca.uhn.fhir.model.api.ExtensionDt;
import ca.uhn.fhir.model.api.IDatatype; import ca.uhn.fhir.model.api.IDatatype;
@ -391,6 +383,18 @@ class ModelScanner {
*/ */
resourceDef.populateScanAlso(myScanAlso); resourceDef.populateScanAlso(myScanAlso);
/*
* See #504:
* Bundle types may not have extensions
*/
if (resourceDef.hasExtensions()) {
if (IAnyResource.class.isAssignableFrom(theClass)) {
if (!IDomainResource.class.isAssignableFrom(theClass)) {
throw new ConfigurationException("Class \"" + theClass + "\" is invalid. This resource type is not a DomainResource, it must not have extensions");
}
}
}
return resourceName; return resourceName;
} }

View File

@ -229,4 +229,5 @@ public class RuntimeResourceDefinition extends BaseRuntimeElementCompositeDefini
return retVal; return retVal;
} }
} }

View File

@ -7,7 +7,6 @@ import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.stringContainsInOrder; import static org.hamcrest.Matchers.stringContainsInOrder;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertSame;
@ -34,7 +33,6 @@ import org.hl7.fhir.instance.model.api.IBaseResource;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.mockito.internal.stubbing.answers.ThrowsException; import org.mockito.internal.stubbing.answers.ThrowsException;
@ -45,8 +43,6 @@ import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.*; import ca.uhn.fhir.model.api.*;
import ca.uhn.fhir.model.api.Bundle; import ca.uhn.fhir.model.api.Bundle;
import ca.uhn.fhir.model.api.annotation.Child; import ca.uhn.fhir.model.api.annotation.Child;
import ca.uhn.fhir.model.api.annotation.Description;
import ca.uhn.fhir.model.api.annotation.Extension;
import ca.uhn.fhir.model.api.annotation.ResourceDef; import ca.uhn.fhir.model.api.annotation.ResourceDef;
import ca.uhn.fhir.model.base.composite.BaseCodingDt; import ca.uhn.fhir.model.base.composite.BaseCodingDt;
import ca.uhn.fhir.model.dstu2.composite.*; import ca.uhn.fhir.model.dstu2.composite.*;
@ -2782,61 +2778,4 @@ public class XmlParserDstu2Test {
} }
} }
/**
* See #504
*/
@Test
@Ignore
public void testBinaryEncodingWithKnownExtension() {
LetterTemplate template = new LetterTemplate();
template.setName(new StringDt("Testname"));
template.setContentType(new CodeDt("test-type"));
IParser parser = FhirContext.forDstu2().newJsonParser();
String resourceToString = parser.encodeResourceToString(template);
LetterTemplate resource = parser.parseResource(LetterTemplate.class, resourceToString);
assertNotNull(resource.getContentType());
assertNotNull(resource.getName());
}
/**
* See #504
*/
@Test
@Ignore
public void testBinaryEncodingWithUnknownExtension() {
Binary template = new Binary();
String extensionURL = "http://www.something.org/StructureDefinition/letter-template";
ExtensionDt e = new ExtensionDt(false, extensionURL, new StringDt("Testname"));
template.getUndeclaredExtensions().add(e);
template.setContentType(new CodeDt("test-type"));
IParser parser = FhirContext.forDstu2().newJsonParser();
String resourceToString = parser.encodeResourceToString(template);
Binary resource = parser.parseResource(Binary.class, resourceToString);
assertNotNull(resource.getContentType());
assertFalse((resource.getUndeclaredExtensionsByUrl(extensionURL).isEmpty()));
assertNotNull(resource.getUndeclaredExtensionsByUrl(extensionURL).get(0));
}
@ResourceDef(name = "Binary", id = "letter-template", profile = "http://www.something.org/StructureDefinition/letter-template")
public static class LetterTemplate extends Binary {
@Child(name="name")
@Extension(url="http://example.com/dontuse#name", definedLocally=false, isModifier=false)
@Description(shortDefinition="The name of the template")
private StringDt myName;
public LetterTemplate() {}
public void setName(StringDt name) {
myName = name;
}
public StringDt getName() {
return myName;
}
}
} }

View File

@ -6,26 +6,14 @@ import static org.junit.Assert.fail;
import java.util.List; import java.util.List;
import org.hl7.fhir.dstu3.model.BaseResource; import org.hl7.fhir.dstu3.model.*;
import org.hl7.fhir.dstu3.model.CarePlan;
import org.hl7.fhir.dstu3.model.CodeType;
import org.hl7.fhir.dstu3.model.IdType;
import org.hl7.fhir.dstu3.model.Meta;
import org.hl7.fhir.dstu3.model.Patient;
import org.hl7.fhir.dstu3.model.Property;
import org.hl7.fhir.dstu3.model.Resource;
import org.hl7.fhir.dstu3.model.ResourceType;
import org.hl7.fhir.instance.model.api.IAnyResource;
import org.hl7.fhir.instance.model.api.IBaseMetaType;
import org.hl7.fhir.instance.model.api.IIdType;
import org.hl7.fhir.instance.model.api.IPrimitiveType;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import ca.uhn.fhir.model.api.annotation.Compartment; import ca.uhn.fhir.model.api.annotation.*;
import ca.uhn.fhir.model.api.annotation.ResourceDef; import ca.uhn.fhir.model.api.annotation.Extension;
import ca.uhn.fhir.model.api.annotation.SearchParamDefinition; import ca.uhn.fhir.model.primitive.StringDt;
import ca.uhn.fhir.parser.DataFormatException; import ca.uhn.fhir.parser.DataFormatException;
import ca.uhn.fhir.util.TestUtil; import ca.uhn.fhir.util.TestUtil;
@ -45,7 +33,6 @@ public class ModelScannerDstu3Test {
assertNotNull(def.getSearchParam("_id")); assertNotNull(def.getSearchParam("_id"));
} }
@Test @Test
public void testBundleMustImplementIBaseBundle() throws DataFormatException { public void testBundleMustImplementIBaseBundle() throws DataFormatException {
FhirContext ctx = FhirContext.forDstu3(); FhirContext ctx = FhirContext.forDstu3();
@ -225,4 +212,40 @@ public class ModelScannerDstu3Test {
} }
/**
* See #504
*/
@Test
public void testBinaryMayNotHaveExtensions() {
FhirContext ctx = FhirContext.forDstu3();
try {
ctx.getResourceDefinition(LetterTemplate.class);
fail();
} catch (ConfigurationException e) {
assertEquals("Class \"class ca.uhn.fhir.context.ModelScannerDstu3Test$LetterTemplate\" is invalid. This resource type is not a DomainResource, it must not have extensions", e.getMessage());
}
}
@ResourceDef(name = "Binary", id = "letter-template", profile = "http://www.something.org/StructureDefinition/letter-template")
public static class LetterTemplate extends Binary {
private static final long serialVersionUID = 1L;
@Child(name = "name")
@Extension(url = "http://example.com/dontuse#name", definedLocally = false, isModifier = false)
@Description(shortDefinition = "The name of the template")
private StringDt myName;
public LetterTemplate() {
}
public void setName(StringDt name) {
myName = name;
}
public StringDt getName() {
return myName;
}
}
} }

View File

@ -198,6 +198,12 @@
wrong type (string instead of token). Thanks to wrong type (string instead of token). Thanks to
Robert Lichtenberger for reporting! Robert Lichtenberger for reporting!
</action> </action>
<action type="add" issue="504">
Custom resource types which extend Binary must not
have declared extensions since this is invalid in
FHIR (and HAPI would just ignore them anyhow). Thanks
to Thomas S Berg for reporting!
</action>
</release> </release>
<release version="2.1" date="2016-11-11"> <release version="2.1" date="2016-11-11">
<action type="add"> <action type="add">