You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by ch...@apache.org on 2015/02/17 15:54:48 UTC

olingo-odata4 git commit: [OLINGO-578] Deserialize enum values as strings

Repository: olingo-odata4
Updated Branches:
  refs/heads/master 0a1c1298e -> 90781859b


[OLINGO-578] Deserialize enum values as strings


Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/90781859
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/90781859
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/90781859

Branch: refs/heads/master
Commit: 90781859bd0e36c47aeaee6e0ade37ffaa51740e
Parents: 0a1c129
Author: Christian Amend <ch...@apache.org>
Authored: Tue Feb 17 15:54:15 2015 +0100
Committer: Christian Amend <ch...@apache.org>
Committed: Tue Feb 17 15:54:15 2015 +0100

----------------------------------------------------------------------
 .../json/ODataJsonDeserializer.java             |  8 +++-
 .../tecsvc/provider/ComplexTypeProvider.java    |  2 +-
 .../server/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(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/90781859/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/json/ODataJsonDeserializer.java
----------------------------------------------------------------------
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 7e624e3..c12fb27 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

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/90781859/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/provider/ComplexTypeProvider.java
----------------------------------------------------------------------
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 bf30b32..3f70ea6 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));

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/90781859/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/provider/EdmTechProvider.java
----------------------------------------------------------------------
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 aadc45b..65975ec 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<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

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/90781859/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/provider/PropertyProvider.java
----------------------------------------------------------------------
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 7b1ef0f..d3aabe4 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")

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/90781859/lib/server-test/src/test/java/org/apache/olingo/server/core/deserializer/json/ODataJsonDeserializerEntityTest.java
----------------------------------------------------------------------
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 f80a4e4..ff54e1e 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());

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/90781859/lib/server-test/src/test/resources/EntityETMixEnumDefCollComp.json
----------------------------------------------------------------------
diff --git a/lib/server-test/src/test/resources/EntityETMixEnumDefCollComp.json b/lib/server-test/src/test/resources/EntityETMixEnumDefCollComp.json
index c56a809..f081ca8 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"]
 		}
 	]