[OLINGO-806] small fix for JSON deserializer Signed-off-by: Michael Bolz <[email protected]>
Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/ac828a35 Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/ac828a35 Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/ac828a35 Branch: refs/heads/OLINGO-811_CountForExpand Commit: ac828a3554a8737139fba3b2e9328f2b917042f5 Parents: 303c4e8 Author: Klaus Straubinger <[email protected]> Authored: Fri Nov 6 09:06:30 2015 +0100 Committer: Michael Bolz <[email protected]> Committed: Fri Nov 6 09:08:49 2015 +0100 ---------------------------------------------------------------------- .../json/ODataJsonDeserializer.java | 8 +-- .../json/ODataJsonDeserializerBasicTest.java | 51 ++++---------------- .../json/ODataJsonDeserializerEntityTest.java | 42 +++++++--------- 3 files changed, 31 insertions(+), 70 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ac828a35/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 9b9de1d..efe1531 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 @@ -213,12 +213,12 @@ public class ODataJsonDeserializer implements ODataDeserializer { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY, true); JsonParser parser = new JsonFactory(objectMapper).createParser(stream); - final ObjectNode tree = parser.getCodec().readTree(parser); - if (tree == null) { + final JsonNode tree = parser.getCodec().readTree(parser); + if (tree == null || !tree.isObject()) { throw new DeserializerException("Invalid JSON syntax.", DeserializerException.MessageKeys.JSON_SYNTAX_EXCEPTION); } - return tree; + return (ObjectNode) tree; } private Map<String, Parameter> consumeParameters(final EdmAction edmAction, final ObjectNode node) @@ -757,7 +757,7 @@ public class ODataJsonDeserializer implements ODataDeserializer { @Override public DeserializerResult entityReferences(final InputStream stream) throws DeserializerException { try { - ArrayList<URI> parsedValues = new ArrayList<URI>(); + List<URI> parsedValues = new ArrayList<URI>(); final ObjectNode tree = parseJsonTree(stream); final String key = Constants.JSON_ID; JsonNode jsonNode = tree.get(Constants.VALUE); http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ac828a35/lib/server-core/src/test/java/org/apache/olingo/server/core/deserializer/json/ODataJsonDeserializerBasicTest.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/deserializer/json/ODataJsonDeserializerBasicTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/deserializer/json/ODataJsonDeserializerBasicTest.java index 9e22c6c..8160165 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/deserializer/json/ODataJsonDeserializerBasicTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/deserializer/json/ODataJsonDeserializerBasicTest.java @@ -40,34 +40,6 @@ public class ODataJsonDeserializerBasicTest { } @Test - public void collectionProperties() throws Exception { - String payload = "{\n" + - " \"@odata.context\": \"http://host/service/$metadata#Collection($ref)\",\n" + - " \"value\": [\n" + - " { \"@odata.id\": \"Orders(10643)\" },\n" + - " { \"@odata.id\": \"Orders(10759)\" }\n" + - " ]\n" + - "}"; - List<URI> values = deserializer.entityReferences(new ByteArrayInputStream(payload.getBytes())) - .getEntityReferences(); - assertEquals(2, values.size()); - assertEquals("Orders(10643)", values.get(0).toASCIIString()); - assertEquals("Orders(10759)", values.get(1).toASCIIString()); - } - - @Test - public void properties() throws Exception { - String payload = "{\n" + - " \"@odata.context\": \"http://host/service/$metadata#$ref\",\n" + - " \"@odata.id\": \"Orders(10643)\"\n" + - "}"; - List<URI> values = deserializer.entityReferences(new ByteArrayInputStream(payload - .getBytes())).getEntityReferences(); - assertEquals(1, values.size()); - assertEquals("Orders(10643)", values.get(0).toASCIIString()); - } - - @Test public void reference() throws Exception { String entityString = "{" + "\"@odata.context\": \"$metadata#$ref\"," @@ -76,7 +48,6 @@ public class ODataJsonDeserializerBasicTest { InputStream stream = new ByteArrayInputStream(entityString.getBytes()); final List<URI> entityReferences = deserializer.entityReferences(stream).getEntityReferences(); - assertEquals(1, entityReferences.size()); assertEquals("ESAllPrim(0)", entityReferences.get(0).toASCIIString()); } @@ -135,15 +106,12 @@ public class ODataJsonDeserializerBasicTest { @Test public void referenceEmpty() throws Exception { - String entityString = "{" + - " \"@odata.context\": \"$metadata#Collection($ref)\"," + - " \"value\": [" + - " ]" + - "}"; + final String entityString = "{\"@odata.context\": \"$metadata#Collection($ref)\"," + + " \"value\": [ ]" + + "}"; InputStream stream = new ByteArrayInputStream(entityString.getBytes()); final List<URI> entityReferences = deserializer.entityReferences(stream).getEntityReferences(); - assertEquals(0, entityReferences.size()); } @@ -153,9 +121,7 @@ public class ODataJsonDeserializerBasicTest { * See OData JSON Format chapter 13 * ... the object that MUST contain the id of the referenced entity */ - String entityString = "{ }"; - - InputStream stream = new ByteArrayInputStream(entityString.getBytes()); + InputStream stream = new ByteArrayInputStream(new byte[] { '{', '}' }); deserializer.entityReferences(stream).getEntityReferences(); } @@ -165,13 +131,16 @@ public class ODataJsonDeserializerBasicTest { } @Test(expected = DeserializerException.class) + public void referencesInvalidJson() throws Exception { + deserializer.entityReferences(new ByteArrayInputStream(new byte[] { 'A' })); + } + + @Test(expected = DeserializerException.class) public void referenceValueIsNotAnArray() throws Exception { String entityString = "{" + " \"@odata.context\": \"$metadata#Collection($ref)\"," + " \"value\": \"ESAllPrim(0)\"" + // This is not allowed. Value must be followed by an array "}"; - - InputStream stream = new ByteArrayInputStream(entityString.getBytes()); - deserializer.entityReferences(stream); + deserializer.entityReferences(new ByteArrayInputStream(entityString.getBytes())); } } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/ac828a35/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 c5fcfe2..b528b28 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 @@ -504,9 +504,7 @@ public class ODataJsonDeserializerEntityTest extends AbstractODataDeserializerTe @Test public void validJsonValueForComplexTypeNull() throws Exception { - final String entityString = "{" - + "\"PropertyComp\":null" - + "}"; + final String entityString = "{\"PropertyComp\":null}"; final Entity entity = deserialize(entityString, "ETMixPrimCollComp"); assertNull(entity.getProperty("PropertyComp").getValue()); } @@ -550,9 +548,7 @@ public class ODataJsonDeserializerEntityTest extends AbstractODataDeserializerTe @Test public void eTMixEnumDefCollCompMultipleValuesForEnum() throws Exception { - String entityString = "{" - + "\"PropertyEnumString\" : \"String1,String2\"" - + "}"; + final String entityString = "{\"PropertyEnumString\": \"String1,String2\"}"; final Entity entity = deserialize(entityString, "ETMixEnumDefCollComp"); assertEquals((short) 3, entity.getProperty("PropertyEnumString").getValue()); } @@ -560,7 +556,7 @@ public class ODataJsonDeserializerEntityTest extends AbstractODataDeserializerTe @Test public void mappingTest() throws Exception { EdmEntityType entityType = mock(EdmEntityType.class); - when(entityType.getFullQualifiedName()).thenReturn(new FullQualifiedName("napespace", "name")); + when(entityType.getFullQualifiedName()).thenReturn(new FullQualifiedName("namespace", "name")); List<String> propertyNames = new ArrayList<String>(); propertyNames.add("PropertyDate"); propertyNames.add("PropertyDateTimeOffset"); @@ -606,6 +602,13 @@ public class ODataJsonDeserializerEntityTest extends AbstractODataDeserializerTe } @Test + public void nonJsonInput() throws Exception { + expectException("0", "ETAllPrim", DeserializerException.MessageKeys.JSON_SYNTAX_EXCEPTION); + expectException("[]", "ETAllPrim", DeserializerException.MessageKeys.JSON_SYNTAX_EXCEPTION); + expectException("}{", "ETAllPrim", DeserializerException.MessageKeys.JSON_SYNTAX_EXCEPTION); + } + + @Test public void etAllPrimWithInvalidNullValue() throws Exception { String entityString = "{\"PropertyInt16\":null," + @@ -957,24 +960,21 @@ public class ODataJsonDeserializerEntityTest extends AbstractODataDeserializerTe @Test public void invalidJsonSyntax() throws Exception { - String entityString = - "{\"PropertyInt16\":32767,}"; + final String entityString = "{\"PropertyInt16\":32767,}"; expectException(entityString, "ETAllPrim", DeserializerException.MessageKeys.JSON_SYNTAX_EXCEPTION); } @Test public void invalidJsonValueForPrimTypeArray() throws Exception { - String entityString = - "{\"PropertyInt16\":[]}"; + final String entityString = "{\"PropertyInt16\":[]}"; expectException(entityString, "ETAllPrim", DeserializerException.MessageKeys.INVALID_JSON_TYPE_FOR_PROPERTY); } @Test public void invalidJsonValueForPrimTypeObject() throws Exception { - String entityString = - "{\"PropertyInt16\":{}}"; + final String entityString = "{\"PropertyInt16\":{}}"; expectException(entityString, "ETAllPrim", DeserializerException.MessageKeys.INVALID_JSON_TYPE_FOR_PROPERTY); } @@ -991,18 +991,14 @@ public class ODataJsonDeserializerEntityTest extends AbstractODataDeserializerTe @Test public void invalidJsonValueForComplexTypeTypeString() throws Exception { - final String entityString = "{" - + "\"PropertyComp\":\"InvalidString\"" - + "}"; + final String entityString = "{\"PropertyComp\":\"InvalidString\"}"; expectException(entityString, "ETMixPrimCollComp", DeserializerException.MessageKeys.INVALID_JSON_TYPE_FOR_PROPERTY); } @Test public void invalidNullValueForComplexTypeNullableFalse() throws Exception { - final String entityString = "{" - + "\"PropertyComp\":null" - + "}"; + final String entityString = "{\"PropertyComp\":null}"; expectException(entityString, "ETTwoKeyNav", DeserializerException.MessageKeys.INVALID_NULL_PROPERTY); } @@ -1040,18 +1036,14 @@ public class ODataJsonDeserializerEntityTest extends AbstractODataDeserializerTe @Test public void invalidNullValueForPrimIntCollectionNullableFalse() throws Exception { - final String entityString = "{" - + "\"CollPropertyInt16\":[123,\"null\",4711]" - + "}"; + final String entityString = "{\"CollPropertyInt16\":[123,\"null\",4711]}"; expectException(entityString, "ETCollAllPrim", DeserializerException.MessageKeys.INVALID_VALUE_FOR_PROPERTY); } @Test public void provokedPrimitiveTypeException() throws Exception { - final String entityString = "{" - + "\"PropertyInt16\":32767000000000000000000000000000000000000" - + "}"; + final String entityString = "{\"PropertyInt16\":32767000000000000000000000000000000000000}"; expectException(entityString, "ETMixPrimCollComp", DeserializerException.MessageKeys.INVALID_VALUE_FOR_PROPERTY); }
