[OLINGO-578] Deserialize enum values as strings

This commit is contained in:
Christian Amend 2015-02-17 15:54:15 +01:00
parent 0a1c1298ef
commit 90781859bd
6 changed files with 53 additions and 29 deletions

View File

@ -504,7 +504,13 @@ public class ODataJsonDeserializer implements ODataDeserializer {
}
try {
EdmEnumType edmEnumType = (EdmEnumType) edmProperty.getType();
checkJsonTypeBasedOnPrimitiveType(edmProperty.getName(), edmEnumType.getUnderlyingType().getName(), jsonNode);
// Enum values must be strings
if (!jsonNode.isTextual()) {
throw new DeserializerException("Invalid json type: " + jsonNode.getNodeType() + " for enum property: "
+ edmProperty.getName(), DeserializerException.MessageKeys.INVALID_VALUE_FOR_PROPERTY, edmProperty
.getName());
}
Class<?> javaClass = getJavaClassForPrimitiveType(edmProperty, edmEnumType.getUnderlyingType());
return edmEnumType
.valueOfString(jsonNode.asText(), edmProperty.isNullable(), edmProperty.getMaxLength(), edmProperty

View File

@ -168,7 +168,7 @@ public class ComplexTypeProvider {
return new ComplexType()
.setName(nameCTMixEnumDef.getName())
.setProperties(Arrays.asList(
PropertyProvider.propertyEnumString_ENString,
PropertyProvider.propertyEnumString_ENString_Nullable,
PropertyProvider.collPropertyEnumString_ENString,
PropertyProvider.propertyTypeDefinition_TDString,
PropertyProvider.collPropertyTypeDefinition_TDString));

View File

@ -54,7 +54,6 @@ public class EdmTechProvider extends EdmProvider {
private final ActionProvider actionProvider;
private final FunctionProvider functionProvider;
private final TypeDefinitionProvider typeDefinitionProvider;
private final List<EdmxReference> references;
public EdmTechProvider() {
this(Collections.<EdmxReference>emptyList());
@ -69,7 +68,6 @@ public class EdmTechProvider extends EdmProvider {
functionProvider = new FunctionProvider();
typeDefinitionProvider = new TypeDefinitionProvider();
schemaProvider = new SchemaProvider(this);
this.references = references;
}
@Override

View File

@ -686,7 +686,12 @@ public class PropertyProvider {
// EnumProperties --------------------------------------------------------------------------------------------------
public static final Property propertyEnumString_ENString = new Property()
.setName("PropertyEnumString")
.setType(EnumTypeProvider.nameENString);
.setType(EnumTypeProvider.nameENString)
.setNullable(false);
public static final Property propertyEnumString_ENString_Nullable = new Property()
.setName("PropertyEnumString")
.setType(EnumTypeProvider.nameENString);
public static final Property collPropertyEnumString_ENString = new Property()
.setName("CollPropertyEnumString")

View File

@ -497,7 +497,7 @@ public class ODataJsonDeserializerEntityTest extends AbstractODataDeserializerTe
@SuppressWarnings("unchecked")
@Test
public void eTTwoKeyNavEnumTest() throws Exception {
public void eTMixEnumDefCollCompTest() throws Exception {
InputStream stream = getFileAsStream("EntityETMixEnumDefCollComp.json");
ODataDeserializer deserializer = OData.newInstance().createDeserializer(ODataFormat.JSON);
Entity entity =
@ -602,7 +602,7 @@ public class ODataJsonDeserializerEntityTest extends AbstractODataDeserializerTe
@Test
public void eTMixEnumDefCollCompNavValidComplexEnumValueNull() throws Exception {
String entityString = "{"
+ "\"PropertyEnumString\" : 2,"
+ "\"PropertyEnumString\" : \"String2\","
+ "\"PropertyCompMixedEnumDef\" : {"
+ "\"PropertyEnumString\" : null"
+ "}}";
@ -612,11 +612,25 @@ public class ODataJsonDeserializerEntityTest extends AbstractODataDeserializerTe
Entity e = deserializer.entity(stream, edm.getEntityType(
new FullQualifiedName("Namespace1_Alias", "ETMixEnumDefCollComp")));
assertEquals(Short.valueOf("2"), e.getProperty("PropertyEnumString").getValue());
assertEquals((short) 2, e.getProperty("PropertyEnumString").getValue());
Property propertyCompMixedEnumDef = e.getProperty("PropertyCompMixedEnumDef");
assertNull(propertyCompMixedEnumDef.asComplex().get(0).getValue());
}
@Test
public void eTMixEnumDefCollCompMultipleValuesForEnum() throws Exception {
String entityString = "{"
+ "\"PropertyEnumString\" : \"String1,String2\""
+ "}";
InputStream stream = new ByteArrayInputStream(entityString.getBytes());
ODataDeserializer deserializer = OData.newInstance().createDeserializer(ODataFormat.JSON);
Entity e = deserializer.entity(stream, edm.getEntityType(
new FullQualifiedName("Namespace1_Alias", "ETMixEnumDefCollComp")));
assertEquals((short) 3, e.getProperty("PropertyEnumString").getValue());
}
@SuppressWarnings("unused")
@Test
public void mappingTest() throws Exception {
@ -1303,18 +1317,19 @@ public class ODataJsonDeserializerEntityTest extends AbstractODataDeserializerTe
public void eTMixEnumDefCollCompInvalidEnumValueNull() throws Exception {
String entityString = "{"
+ "\"PropertyEnumString\" : null,"
+ "\"PropertyCompEnum\" : {"
+ "\"PropertyEnumString\" : 2"
+ "\"PropertyCompMixedEnumDef\" : {"
+ "\"PropertyEnumString\" : \"2\""
+ "}}";
InputStream stream = new ByteArrayInputStream(entityString.getBytes());
ODataDeserializer deserializer = OData.newInstance().createDeserializer(ODataFormat.JSON);
Entity e = deserializer.entity(stream, edm.getEntityType(
new FullQualifiedName("Namespace1_Alias", "ETMixEnumDefCollComp")));
assertNull(e.getProperty("PropertyEnumString").getValue());
Property propertyCompMixedEnumDef = e.getProperty("PropertyCompMixedEnumDef");
assertEquals(Short.valueOf("2"), propertyCompMixedEnumDef.asComplex().get(0).getValue());
try {
deserializer.entity(stream, edm.getEntityType(
new FullQualifiedName("Namespace1_Alias", "ETMixEnumDefCollComp")));
} catch (DeserializerException e) {
assertEquals(DeserializerException.MessageKeys.INVALID_NULL_PROPERTY, e.getMessageKey());
throw e;
}
}
@Test(expected = DeserializerException.class)
@ -1322,7 +1337,7 @@ public class ODataJsonDeserializerEntityTest extends AbstractODataDeserializerTe
String entityString = "{"
+ "\"PropertyEnumString\" : [],"
+ "\"PropertyCompEnum\" : {"
+ "\"PropertyEnumString\" : 2"
+ "\"PropertyEnumString\" : \"2\""
+ "}}";
InputStream stream = new ByteArrayInputStream(entityString.getBytes());
@ -1340,7 +1355,7 @@ public class ODataJsonDeserializerEntityTest extends AbstractODataDeserializerTe
String entityString = "{"
+ "\"PropertyEnumString\" : {},"
+ "\"PropertyCompEnum\" : {"
+ "\"PropertyEnumString\" : 2"
+ "\"PropertyEnumString\" : \"2\""
+ "}}";
InputStream stream = new ByteArrayInputStream(entityString.getBytes());
@ -1358,7 +1373,7 @@ public class ODataJsonDeserializerEntityTest extends AbstractODataDeserializerTe
String entityString = "{"
+ "\"PropertyEnumString\" : \"invalid\","
+ "\"PropertyCompEnum\" : {"
+ "\"PropertyEnumString\" : 2"
+ "\"PropertyEnumString\" : \"2\""
+ "}}";
InputStream stream = new ByteArrayInputStream(entityString.getBytes());
@ -1374,9 +1389,9 @@ public class ODataJsonDeserializerEntityTest extends AbstractODataDeserializerTe
@Test(expected = DeserializerException.class)
public void eTMixEnumDefCollCompInvalidEnumValueByPrimitiveTypeException() throws Exception {
String entityString = "{"
+ "\"PropertyEnumString\" : 18,"
+ "\"PropertyEnumString\" : \"18\","
+ "\"PropertyCompEnum\" : {"
+ "\"PropertyEnumString\" : 2"
+ "\"PropertyEnumString\" : \"2\""
+ "}}";
InputStream stream = new ByteArrayInputStream(entityString.getBytes());

View File

@ -1,23 +1,23 @@
{
"PropertyEnumString" : 2,
"PropertyEnumString" : "2",
"PropertyDefString" : "string",
"CollPropertyEnumString" : [1, 2],
"CollPropertyEnumString" : ["1", "2"],
"CollPropertyDefString" : ["string1", "string2"],
"PropertyCompMixedEnumDef" : {
"PropertyEnumString" : 2,
"PropertyEnumString" : "2",
"PropertyDefString" : "string",
"CollPropertyEnumString" : [1, 2],
"CollPropertyEnumString" : ["1", "2"],
"CollPropertyDefString" : ["string1", "string2"]
},
"CollPropertyCompMixedEnumDef" : [{
"PropertyEnumString" : 2,
"PropertyEnumString" : "2",
"PropertyDefString" : "string",
"CollPropertyEnumString" : [1, 2],
"CollPropertyEnumString" : ["1", "2"],
"CollPropertyDefString" : ["string1", "string2"]
}, {
"PropertyEnumString" : 2,
"PropertyEnumString" : "2",
"PropertyDefString" : "string",
"CollPropertyEnumString" : [1, 2],
"CollPropertyEnumString" : ["1", "2"],
"CollPropertyDefString" : ["string1", "string2"]
}
]