From 35a1e12354fb79cf59e47349b4309375ad6e5bb5 Mon Sep 17 00:00:00 2001 From: Christian Holzer Date: Mon, 26 Jan 2015 10:47:59 +0100 Subject: [PATCH] [OLINGO-540] Fix: AtomDeserializer causes no invite loop while deserializing empty inline entities/feeds Signed-off-by: Michael Bolz --- .../core/serialization/AtomDeserializer.java | 24 +- .../serialization/AtomDeserializerTest.java | 330 ++++++++++++++++++ 2 files changed, 345 insertions(+), 9 deletions(-) create mode 100644 lib/commons-core/src/test/java/org/apache/olingo/commons/core/serialization/AtomDeserializerTest.java diff --git a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/AtomDeserializer.java b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/AtomDeserializer.java index 0867b0008..a9453185b 100644 --- a/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/AtomDeserializer.java +++ b/lib/commons-core/src/main/java/org/apache/olingo/commons/core/serialization/AtomDeserializer.java @@ -398,15 +398,7 @@ public class AtomDeserializer extends AbstractAtomDealer implements ODataDeseria if (event.isStartElement()) { if (inlineQName.equals(event.asStartElement().getName())) { - StartElement inline = null; - while (reader.hasNext() && inline == null) { - final XMLEvent innerEvent = reader.peek(); - if (innerEvent.isCharacters() && innerEvent.asCharacters().isWhiteSpace()) { - reader.nextEvent(); - } else if (innerEvent.isStartElement()) { - inline = innerEvent.asStartElement(); - } - } + StartElement inline = getStartElement(reader); if (inline != null) { if (Constants.QNAME_ATOM_ELEM_ENTRY.equals(inline.getName())) { link.setInlineEntity(entity(reader, inline)); @@ -426,6 +418,20 @@ public class AtomDeserializer extends AbstractAtomDealer implements ODataDeseria } } + private StartElement getStartElement(XMLEventReader reader) throws XMLStreamException { + while (reader.hasNext()) { + final XMLEvent innerEvent = reader.peek(); + if (innerEvent.isCharacters() && innerEvent.asCharacters().isWhiteSpace()) { + reader.nextEvent(); + } else if (innerEvent.isStartElement()) { + return innerEvent.asStartElement(); + } else if (innerEvent.isEndElement() && inlineQName.equals(innerEvent.asEndElement().getName())) { + return null; + } + } + return null; + } + public ResWrap delta(final InputStream input) throws XMLStreamException, EdmPrimitiveTypeException { final XMLEventReader reader = getReader(input); diff --git a/lib/commons-core/src/test/java/org/apache/olingo/commons/core/serialization/AtomDeserializerTest.java b/lib/commons-core/src/test/java/org/apache/olingo/commons/core/serialization/AtomDeserializerTest.java new file mode 100644 index 000000000..3126fc240 --- /dev/null +++ b/lib/commons-core/src/test/java/org/apache/olingo/commons/core/serialization/AtomDeserializerTest.java @@ -0,0 +1,330 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.olingo.commons.core.serialization; + +import static org.junit.Assert.*; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; + +import org.apache.olingo.commons.api.data.Entity; +import org.apache.olingo.commons.api.data.EntitySet; +import org.apache.olingo.commons.api.data.ResWrap; +import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion; +import org.junit.Test; + +public class AtomDeserializerTest { + + @Test + public void emptyInlineEntityOlingo540() throws Exception { + final String content = "" + + "\r\n" + + " http://services.odata.org/V3/OData/OData.svc/Products(3)\r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " Havina Cola\r\n" + + " The Original Key Lime Cola\r\n" + + " 2015-01-26T08:57:02Z\r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " 3\r\n" + + " 2005-10-01T00:00:00\r\n" + + " 2006-10-01T00:00:00\r\n" + + " 3\r\n" + + " 19.9\r\n" + + " \r\n" + + " \r\n" + + " "; + + final AtomDeserializer deserializer = new AtomDeserializer(ODataServiceVersion.V30); + final InputStream in = new ByteArrayInputStream(content.getBytes("UTF-8")); + final ResWrap entity = deserializer.toEntity(in); + + assertNotNull(entity); + assertNull(entity.getPayload().getNavigationLink("Supplier").getInlineEntitySet()); + } + + @Test + public void filledInlineEntity() throws Exception { + final String content = "" + + "\r\n" + + " http://services.odata.org/V3/OData/OData.svc/Products(3)\r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " http://services.odata.org/V3/OData/OData.svc/Suppliers(0)\r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " Exotic Liquids\r\n" + + " 2015-01-26T08:57:02Z\r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " 0\r\n" + + " Exotic Liquids\r\n" + + " \r\n" + + " NE 228th\r\n" + + " Sammamish\r\n" + + " WA\r\n" + + " 98074\r\n" + + " USA\r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " 47.6316604614258 -122.03547668457\r\n" + + " \r\n" + + " \r\n" + + " 0\r\n" + + " \r\n" + + " \r\n" + + " " + + " \r\n" + + " \r\n" + + " \r\n" + + " Havina Cola\r\n" + + " The Original Key Lime Cola\r\n" + + " 2015-01-26T08:57:02Z\r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " 3\r\n" + + " 2005-10-01T00:00:00\r\n" + + " 2006-10-01T00:00:00\r\n" + + " 3\r\n" + + " 19.9\r\n" + + " \r\n" + + " \r\n" + + " "; + + final AtomDeserializer deserializer = new AtomDeserializer(ODataServiceVersion.V30); + final InputStream in = new ByteArrayInputStream(content.getBytes("UTF-8")); + final ResWrap entity = deserializer.toEntity(in); + + assertNotNull(entity); + final Entity inlineEntity = entity.getPayload().getNavigationLink("Supplier").getInlineEntity(); + assertNotNull(inlineEntity); + + assertEquals(new Integer(0), inlineEntity.getProperty("ID").getValue()); + assertEquals("Exotic Liquids", inlineEntity.getProperty("Name").getValue()); + } + + @Test + public void emptyInlineEntityCollection() throws Exception { + final String content = "" + + "\r\n" + + " http://services.odata.org/V3/OData/OData.svc/Products(3)\r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " Havina Cola\r\n" + + " The Original Key Lime Cola\r\n" + + " 2015-01-26T08:57:02Z\r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " 3\r\n" + + " 2005-10-01T00:00:00\r\n" + + " 2006-10-01T00:00:00\r\n" + + " 3\r\n" + + " 19.9\r\n" + + " \r\n" + + " \r\n" + + " "; + + final AtomDeserializer deserializer = new AtomDeserializer(ODataServiceVersion.V30); + final InputStream in = new ByteArrayInputStream(content.getBytes("UTF-8")); + final ResWrap entity = deserializer.toEntity(in); + + assertNotNull(entity); + final EntitySet inlineEntitySet = entity.getPayload().getNavigationLink("Supplier").getInlineEntitySet(); + assertNotNull(inlineEntitySet); + assertEquals(0, inlineEntitySet.getEntities().size()); + } + + @Test + public void filledInlineEntityCollection() throws Exception { + final String content = "" + + "\r\n" + + " http://services.odata.org/V3/OData/OData.svc/Products(3)\r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " http://services.odata.org/V3/OData/OData.svc/Suppliers(0)\r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " Exotic Liquids\r\n" + + " 2015-01-26T08:57:02Z\r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " 0\r\n" + + " Exotic Liquids\r\n" + + " \r\n" + + " NE 228th\r\n" + + " Sammamish\r\n" + + " WA\r\n" + + " 98074\r\n" + + " USA\r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " 47.6316604614258 -122.03547668457\r\n" + + " \r\n" + + " \r\n" + + " 0\r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " Havina Cola\r\n" + + " The Original Key Lime Cola\r\n" + + " 2015-01-26T08:57:02Z\r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " \r\n" + + " 3\r\n" + + " 2005-10-01T00:00:00\r\n" + + " 2006-10-01T00:00:00\r\n" + + " 3\r\n" + + " 19.9\r\n" + + " \r\n" + + " \r\n" + + " "; + + final AtomDeserializer deserializer = new AtomDeserializer(ODataServiceVersion.V30); + final InputStream in = new ByteArrayInputStream(content.getBytes("UTF-8")); + final ResWrap entity = deserializer.toEntity(in); + + assertNotNull(entity); + final EntitySet inlineEntitySet = entity.getPayload().getNavigationLink("Supplier").getInlineEntitySet(); + assertNotNull(inlineEntitySet); + assertEquals(1, inlineEntitySet.getEntities().size()); + } +}