From 591f9522b5056bc924e998a1acb4bcc156211b67 Mon Sep 17 00:00:00 2001 From: Ramesh Reddy Date: Fri, 9 Sep 2016 19:00:57 -0500 Subject: [PATCH] OLINGO-1009: refining the cycle detection logic --- .../core/serializer/json/ODataJsonSerializer.java | 8 +++----- .../server/core/serializer/xml/ODataXmlSerializer.java | 10 +++++----- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializer.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializer.java index f6e6015f3..edca2e436 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializer.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializer.java @@ -323,13 +323,11 @@ public class ODataJsonSerializer extends AbstractODataSerializer { final SelectOption select, final boolean onlyReference, Set ancestors, final JsonGenerator json) throws IOException, SerializerException { boolean cycle = false; - if (expand != null && cycleDetected(ancestors, getEntityId(entity))) { - cycle = true; - } else { + if (expand != null) { if (ancestors == null) { ancestors = new HashSet(); } - ancestors.add(getEntityId(entity)); + cycle = !ancestors.add(getEntityId(entity)); } try { json.writeStartObject(); @@ -383,7 +381,7 @@ public class ODataJsonSerializer extends AbstractODataSerializer { } json.writeEndObject(); } finally { - if (!cycle && ancestors != null) { + if (expand != null && !cycle && ancestors != null) { ancestors.remove(getEntityId(entity)); } } diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/xml/ODataXmlSerializer.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/xml/ODataXmlSerializer.java index 56ee3918d..d8acbf451 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/xml/ODataXmlSerializer.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/xml/ODataXmlSerializer.java @@ -440,13 +440,11 @@ public class ODataXmlSerializer extends AbstractODataSerializer { final XMLStreamWriter writer, final boolean top, final boolean writeOnlyRef, Set ancestors) throws XMLStreamException, SerializerException { boolean cycle = false; - if (expand != null && cycleDetected(ancestors, getEntityId(entity))) { - cycle = true; - } else { + if (expand != null) { if (ancestors == null) { ancestors = new HashSet(); } - ancestors.add(getEntityId(entity)); + cycle = !ancestors.add(getEntityId(entity)); } if (cycle || writeOnlyRef) { @@ -531,7 +529,9 @@ public class ODataXmlSerializer extends AbstractODataSerializer { writer.writeEndElement(); // entry } finally { - ancestors.remove(getEntityId(entity)); + if (!cycle && ancestors != null) { + ancestors.remove(getEntityId(entity)); + } } }