From 8f763aadea9730ddf518663a24da3d56aa2ae2e8 Mon Sep 17 00:00:00 2001 From: Ramesh Reddy Date: Tue, 2 Jun 2015 12:12:57 -0500 Subject: [PATCH] OLINGO-650: supporting the complex properties in expand context url generation --- .../serializer/utils/ContextURLHelper.java | 30 +++++++++++++++++++ .../serializer/utils/ExpandSelectHelper.java | 11 ++----- .../utils/ContextURLHelperTest.java | 16 ++++++++++ 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/utils/ContextURLHelper.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/utils/ContextURLHelper.java index 421c6eb14..f6c2ae39f 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/utils/ContextURLHelper.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/utils/ContextURLHelper.java @@ -29,6 +29,8 @@ import org.apache.olingo.commons.api.edm.EdmStructuredType; import org.apache.olingo.commons.core.Encoder; import org.apache.olingo.server.api.serializer.SerializerException; import org.apache.olingo.server.api.uri.UriParameter; +import org.apache.olingo.server.api.uri.UriResource; +import org.apache.olingo.server.api.uri.UriResourceProperty; import org.apache.olingo.server.api.uri.queryoption.ExpandItem; import org.apache.olingo.server.api.uri.queryoption.ExpandOption; import org.apache.olingo.server.api.uri.queryoption.SelectItem; @@ -117,11 +119,39 @@ public final class ContextURLHelper { } result.append(Encoder.encode(propertyName)).append('(').append(innerSelectList).append(')'); } + } else { + final List resourceParts = expandItem.getResourcePath().getUriResourceParts(); + if(resourceParts.size() > 1) { + if (result.length() > 0) { + result.append(','); + } + final List path = getPropertyPath(resourceParts); + String propertyPath = buildPropertyPath(path); + result.append(Encoder.encode(propertyName)); + result.append("/").append(propertyPath); + } } } } } + private static List getPropertyPath(final List path) { + List result = new LinkedList(); + int index = 1; + while (index < path.size() && path.get(index) instanceof UriResourceProperty) { + result.add(((UriResourceProperty) path.get(index)).getProperty().getName()); + index++; + } + return result; + } + + private static String buildPropertyPath(final List path) { + StringBuilder result = new StringBuilder(); + for (final String segment : path) { + result.append(result.length() == 0 ? "" : '/').append(Encoder.encode(segment)); //$NON-NLS-1$ + } + return result.length() == 0?null:result.toString(); + } private static List> getComplexSelectedPaths(final EdmProperty edmProperty, final Set> selectedPaths) { List> result = new ArrayList>(); diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/utils/ExpandSelectHelper.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/utils/ExpandSelectHelper.java index 3acdbd323..30810f088 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/utils/ExpandSelectHelper.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/utils/ExpandSelectHelper.java @@ -127,14 +127,9 @@ public abstract class ExpandSelectHelper { Set expanded = new HashSet(); for (final ExpandItem item : expandItems) { final List resourceParts = item.getResourcePath().getUriResourceParts(); - if (resourceParts.size() == 1) { - final UriResource resource = resourceParts.get(0); - if (resource instanceof UriResourceNavigation) { - expanded.add(((UriResourceNavigation) resource).getProperty().getName()); - } - } else { - throw new SerializerException("Expand is not supported within complex properties.", - SerializerException.MessageKeys.NOT_IMPLEMENTED); + final UriResource resource = resourceParts.get(0); + if (resource instanceof UriResourceNavigation) { + expanded.add(((UriResourceNavigation) resource).getProperty().getName()); } } return expanded; diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/utils/ContextURLHelperTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/utils/ContextURLHelperTest.java index 2416fbecd..b94f97e95 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/utils/ContextURLHelperTest.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/utils/ContextURLHelperTest.java @@ -116,6 +116,22 @@ public class ContextURLHelperTest { assertEquals("$metadata#ESTwoPrim", ContextURLBuilder.create(contextURL).toASCIIString()); } + @Test + public void buildExpandWithNavigationProperty() throws Exception { + final EdmEntitySet entitySet = entityContainer.getEntitySet("ESTwoPrim"); + final ExpandOption expand = ExpandSelectMock.mockExpandOption(Arrays.asList( + ExpandSelectMock.mockExpandItem(entitySet, "NavPropertyETAllPrimOne", "PropertyString"))); + final SelectItem selectItem1 = ExpandSelectMock.mockSelectItem(entitySet, "PropertyInt16"); + final SelectOption select = ExpandSelectMock.mockSelectOption(Arrays.asList( + selectItem1)); + + final ContextURL contextURL = ContextURL.with().entitySet(entitySet) + .selectList(ContextURLHelper.buildSelectList(entitySet.getEntityType(), expand, select)).build(); + assertEquals("$metadata#ESTwoPrim(PropertyInt16,NavPropertyETAllPrimOne/PropertyString)", + ContextURLBuilder.create(contextURL).toASCIIString()); + } + + @Test public void buildExpandSelect() throws Exception { final EdmEntitySet entitySet = entityContainer.getEntitySet("ESTwoPrim");