From d9ae1f68efa6da7c272d230a70a60c3a7b3cf9a0 Mon Sep 17 00:00:00 2001 From: Ramesh Reddy Date: Wed, 27 Jul 2016 11:47:03 -0500 Subject: [PATCH] OLINGO-981: validating to make sure the navigation property is set to allow nullable before allowing the null as the binding value --- .../json/ODataJsonDeserializer.java | 5 +++-- .../json/ODataJsonDeserializerEntityTest.java | 20 ++++++++++++++----- 2 files changed, 18 insertions(+), 7 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 5f98bd7ba..d30d97352 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 @@ -443,10 +443,11 @@ public class ODataJsonDeserializer implements ODataDeserializer { throw new DeserializerException("Binding annotation: " + key + " must be a string value.", DeserializerException.MessageKeys.INVALID_ANNOTATION_TYPE, key); } - if (jsonNode.isNull()) { + if (edmNavigationProperty.isNullable() && jsonNode.isNull()) { bindingLink.setBindingLink(null); } else { - bindingLink.setBindingLink(jsonNode.asText()); + assertIsNullNode(key, jsonNode); + bindingLink.setBindingLink(jsonNode.asText()); } bindingLink.setType(Constants.ENTITY_BINDING_LINK_TYPE); } 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 368dda6d2..c92edfcd6 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 @@ -1023,18 +1023,28 @@ public class ODataJsonDeserializerEntityTest extends AbstractODataDeserializerTe } @Test - public void bindingOperationNullOnToOne() throws Exception { + public void bindingOperationNullOnToOneNonNull() throws Exception { String entityString = "{\"PropertyInt16\":32767," + "\"PropertyString\":\"First Resource - positive values\"," + "\"NavPropertyETTwoPrimOne@odata.bind\":null" + "}"; - - final Entity entity = deserialize(entityString, "ETAllPrim"); - assertEquals("First Resource - positive values", entity.getProperty("PropertyString").asPrimitive()); - assertNull(entity.getNavigationBinding("NavPropertyETTwoPrimOne").getBindingLink()); + expectException(entityString, "ETAllPrim", + DeserializerException.MessageKeys.INVALID_NULL_ANNOTATION); } + @Test + public void bindingOperationNullOnToOneNull() throws Exception { + String entityString = + "{\"PropertyInt16\":32767," + + "\"PropertyString\":\"First Resource - positive values\"," + + "\"NavPropertyETAllPrimOne@odata.bind\":null" + + "}"; + + final Entity entity = deserialize(entityString, "ETTwoPrim"); + assertEquals("First Resource - positive values", entity.getProperty("PropertyString").asPrimitive()); + assertNull(entity.getNavigationBinding("NavPropertyETAllPrimOne").getBindingLink()); + } @Test public void bindingOperationNullOnToMany() throws Exception { String entityString =