Fix #304 - Better error message if wrong generic type is used
This commit is contained in:
parent
3fd9f9518a
commit
94f50686bc
|
@ -99,7 +99,14 @@ public class ElementUtil {
|
|||
return true;
|
||||
}
|
||||
for (int i = 0; i < theElements.size(); i++) {
|
||||
IBase next = theElements.get(i);
|
||||
IBase next;
|
||||
try {
|
||||
next = theElements.get(i);
|
||||
} catch (ClassCastException e) {
|
||||
List<?> elements = theElements;
|
||||
String s = "Found instance of " + elements.get(i).getClass() + " - Did you set a field value to the incorrect type? Expected " + IBase.class.getName();
|
||||
throw new ClassCastException(s);
|
||||
}
|
||||
if (next != null && !next.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package ca.uhn.fhir.model.dstu2;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
@ -18,21 +17,17 @@ public class ModelDstu2Test {
|
|||
/**
|
||||
* See #304
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
@Test
|
||||
public void testPopulateWrongGenericType() {
|
||||
Patient p = new Patient();
|
||||
List names = Arrays.asList("name");
|
||||
p.setName(names);
|
||||
|
||||
p.setName(null);
|
||||
|
||||
ourCtx.newXmlParser().encodeResourceToString(p);
|
||||
|
||||
try {
|
||||
p.setName(names);
|
||||
fail();
|
||||
ourCtx.newXmlParser().encodeResourceToString(p);
|
||||
} catch (ClassCastException e) {
|
||||
assertEquals("Failed to set invalid value, found element in list of type String but expected ca.uhn.fhir.model.dstu2.composite.HumanNameDt", e.getMessage());
|
||||
assertEquals("Found instance of class java.lang.String - Did you set a field value to the incorrect type? Expected org.hl7.fhir.instance.model.api.IBase", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -164,6 +164,12 @@
|
|||
other similar errors are handled. This allows the parser to be more lenient
|
||||
when needed.
|
||||
</action>
|
||||
<action type="add" issue="304">
|
||||
Improve error message if incorrect type is placed in a list field in the data model. Java
|
||||
uses generics to prevent this at compile time, but if someone is in an environment without
|
||||
generics this helps improve the error message at runtime. Thanks to Hugo Soares for
|
||||
suggesting.
|
||||
</action>
|
||||
</release>
|
||||
<release version="1.4" date="2016-02-04">
|
||||
<action type="add">
|
||||
|
|
Loading…
Reference in New Issue