From 90781859bd0e36c47aeaee6e0ade37ffaa51740e Mon Sep 17 00:00:00 2001 From: Christian Amend Date: Tue, 17 Feb 2015 15:54:15 +0100 Subject: [PATCH] [OLINGO-578] Deserialize enum values as strings --- .../json/ODataJsonDeserializer.java | 8 +++- .../tecsvc/provider/ComplexTypeProvider.java | 2 +- .../tecsvc/provider/EdmTechProvider.java | 2 - .../tecsvc/provider/PropertyProvider.java | 7 ++- .../json/ODataJsonDeserializerEntityTest.java | 47 ++++++++++++------- .../resources/EntityETMixEnumDefCollComp.json | 16 +++---- 6 files changed, 53 insertions(+), 29 deletions(-) diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/json/ODataJsonDeserializer.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/json/ODataJsonDeserializer.java index 7e624e36d..c12fb27b9 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/json/ODataJsonDeserializer.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/json/ODataJsonDeserializer.java @@ -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 diff --git a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/provider/ComplexTypeProvider.java b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/provider/ComplexTypeProvider.java index bf30b327b..3f70ea694 100644 --- a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/provider/ComplexTypeProvider.java +++ b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/provider/ComplexTypeProvider.java @@ -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)); diff --git a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/provider/EdmTechProvider.java b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/provider/EdmTechProvider.java index aadc45b87..65975eca1 100644 --- a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/provider/EdmTechProvider.java +++ b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/provider/EdmTechProvider.java @@ -54,7 +54,6 @@ public class EdmTechProvider extends EdmProvider { private final ActionProvider actionProvider; private final FunctionProvider functionProvider; private final TypeDefinitionProvider typeDefinitionProvider; - private final List references; public EdmTechProvider() { this(Collections.emptyList()); @@ -69,7 +68,6 @@ public class EdmTechProvider extends EdmProvider { functionProvider = new FunctionProvider(); typeDefinitionProvider = new TypeDefinitionProvider(); schemaProvider = new SchemaProvider(this); - this.references = references; } @Override diff --git a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/provider/PropertyProvider.java b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/provider/PropertyProvider.java index 7b1ef0f9c..d3aabe40f 100644 --- a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/provider/PropertyProvider.java +++ b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/provider/PropertyProvider.java @@ -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") diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/deserializer/json/ODataJsonDeserializerEntityTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/deserializer/json/ODataJsonDeserializerEntityTest.java index f80a4e468..ff54e1e34 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/deserializer/json/ODataJsonDeserializerEntityTest.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/deserializer/json/ODataJsonDeserializerEntityTest.java @@ -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()); diff --git a/lib/server-test/src/test/resources/EntityETMixEnumDefCollComp.json b/lib/server-test/src/test/resources/EntityETMixEnumDefCollComp.json index c56a80900..f081ca89a 100644 --- a/lib/server-test/src/test/resources/EntityETMixEnumDefCollComp.json +++ b/lib/server-test/src/test/resources/EntityETMixEnumDefCollComp.json @@ -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"] } ]