Repository: olingo-odata4 Updated Branches: refs/heads/master 44d6f5a17 -> 756ae564e
added some testcases and method writeComplex to write the type and value correctly. Signed-off-by: Christian Amend <[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/756ae564 Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/756ae564 Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/756ae564 Branch: refs/heads/master Commit: 756ae564e547d7d01aff30bbd2c45bf7bcd4c8ca Parents: 44d6f5a Author: Morten Riedel <[email protected]> Authored: Mon Jul 25 16:23:55 2016 +0200 Committer: Christian Amend <[email protected]> Committed: Tue Jul 26 10:33:32 2016 +0200 ---------------------------------------------------------------------- .../serializer/json/ODataJsonSerializer.java | 43 ++++--- .../json/ODataJsonSerializerTest.java | 126 ++++++++++++++++++- 2 files changed, 150 insertions(+), 19 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/756ae564/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializer.java ---------------------------------------------------------------------- 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 adf8411..8c60373 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 @@ -400,8 +400,10 @@ public class ODataJsonSerializer extends AbstractODataSerializer { protected EdmComplexType resolveComplexType(final ServiceMetadata metadata, final EdmComplexType baseType, final String derivedTypeName) throws SerializerException { + + String fullQualifiedName = baseType.getFullQualifiedName().getFullQualifiedNameAsString(); if (derivedTypeName == null || - baseType.getFullQualifiedName().getFullQualifiedNameAsString().equals(derivedTypeName)) { + fullQualifiedName.equals(derivedTypeName)) { return baseType; } EdmComplexType derivedType = metadata.getEdm().getComplexType(new FullQualifiedName(derivedTypeName)); @@ -611,8 +613,7 @@ public class ODataJsonSerializer extends AbstractODataSerializer { if (edmProperty.isCollection()) { writeComplexCollection(metadata, (EdmComplexType) type, property, selectedPaths, json); } else { - writeComplexValue(metadata, property, (EdmComplexType) type, property.asComplex().getValue(), selectedPaths, - json); + writeComplex(metadata, (EdmComplexType) type, property, selectedPaths, json); } } else { throw new SerializerException("Property type not yet supported!", @@ -625,6 +626,20 @@ public class ODataJsonSerializer extends AbstractODataSerializer { } } + private void writeComplex(final ServiceMetadata metadata, final EdmComplexType type, + final Property property, final Set<List<String>> selectedPaths, final JsonGenerator json) + throws IOException, SerializerException{ + json.writeStartObject(); + String derivedName = property.getType(); + final EdmComplexType resolvedType = resolveComplexType(metadata, (EdmComplexType) type, derivedName); + if (!isODataMetadataNone && !resolvedType.equals(type) || isODataMetadataFull) { + json.writeStringField(Constants.JSON_TYPE, "#" + property.getType()); + } + writeComplexValue(metadata, resolvedType, property.asComplex().getValue(), selectedPaths, + json); + json.writeEndObject(); + } + private void writePrimitiveCollection(final EdmPrimitiveType type, final Property property, final Boolean isNullable, final Integer maxLength, final Integer precision, final Integer scale, final Boolean isUnicode, final JsonGenerator json) @@ -662,7 +677,13 @@ public class ODataJsonSerializer extends AbstractODataSerializer { for (Object value : property.asCollection()) { switch (property.getValueType()) { case COLLECTION_COMPLEX: - writeComplexValue(metadata, property, type, ((ComplexValue) value).getValue(), selectedPaths, json); + json.writeStartObject(); + if (isODataMetadataFull) { + json.writeStringField(Constants.JSON_TYPE, "#" + + type.getFullQualifiedName().getFullQualifiedNameAsString()); + } + writeComplexValue(metadata, type, ((ComplexValue) value).getValue(), selectedPaths, json); + json.writeEndObject(); break; default: throw new SerializerException("Property type not yet supported!", @@ -735,27 +756,19 @@ public class ODataJsonSerializer extends AbstractODataSerializer { } } - protected void writeComplexValue(final ServiceMetadata metadata, final Property complexProperty, + protected void writeComplexValue(final ServiceMetadata metadata, final EdmComplexType type, final List<Property> properties, final Set<List<String>> selectedPaths, final JsonGenerator json) throws IOException, SerializerException { - json.writeStartObject(); - - final EdmComplexType resolvedType = resolveComplexType(metadata, - type, complexProperty.getType()); - if (!isODataMetadataNone && !resolvedType.equals(type) || isODataMetadataFull) { - json.writeStringField(Constants.JSON_TYPE, "#" + complexProperty.getType()); - } - for (final String propertyName : resolvedType.getPropertyNames()) { + for (final String propertyName : type.getPropertyNames()) { final Property property = findProperty(propertyName, properties); if (selectedPaths == null || ExpandSelectHelper.isSelected(selectedPaths, propertyName)) { - writeProperty(metadata, (EdmProperty) resolvedType.getProperty(propertyName), property, + writeProperty(metadata, (EdmProperty) type.getProperty(propertyName), property, selectedPaths == null ? null : ExpandSelectHelper.getReducedSelectedPaths(selectedPaths, propertyName), json); } } - json.writeEndObject(); } private Property findProperty(final String propertyName, final List<Property> properties) { http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/756ae564/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerTest.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerTest.java index 1f55040..156420e 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerTest.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerTest.java @@ -938,9 +938,7 @@ public class ODataJsonSerializerTest { InputStream result = serializer .entityCollection(metadata, entityType, entitySet, EntityCollectionSerializerOptions.with() - .contextURL(ContextURL.with().entitySet(edmEntitySet) - .selectList(helper.buildContextURLSelectList(entityType, null, null)) - .build()) + .contextURL(ContextURL.with().entitySet(edmEntitySet).build()) .build()).getContent(); final String resultString = IOUtils.toString(result); @@ -986,7 +984,127 @@ public class ODataJsonSerializerTest { resultString); } - + @Test + public void selectComplexNestedCollectionOfComplexWithMetadataFull() throws Exception{ + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESCompCollComp"); + final EntityCollection entitySet = data.readAll(edmEntitySet); + InputStream result = serializerFullMetadata + .entityCollection(metadata, edmEntitySet.getEntityType(), entitySet, + EntityCollectionSerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).build()) + .build()) + .getContent(); + final String resultString = IOUtils.toString(result); + final String expectedResult = "{\"@odata.context\":\"$metadata#ESCompCollComp\"," + + "\"@odata.metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"value\":[{\"@odata.type\":\"#olingo.odata.test1.ETCompCollComp\"," + + "\"@odata.id\":\"ESCompCollComp(32767)\"," + + "\"[email protected]\":\"#Int16\",\"PropertyInt16\":32767," + + "\"PropertyComp\":{" + + "\"@odata.type\":\"#olingo.odata.test1.CTCompCollComp\"," + + "\"[email protected]\":\"#Collection(olingo.odata.test1.CTTwoPrim)\"," + + "\"CollPropertyComp\":[" + + "{" + + "\"@odata.type\":\"#olingo.odata.test1.CTTwoPrim\"," + + "\"[email protected]\":\"#Int16\"," + +"\"PropertyInt16\":555," + +"\"PropertyString\":\"1 Test Complex in Complex Property\"" + +"},{" + +"\"@odata.type\":\"#olingo.odata.test1.CTTwoPrim\"," + +"\"[email protected]\":\"#Int16\"," + +"\"PropertyInt16\":666," + +"\"PropertyString\":\"2 Test Complex in Complex Property\"" + +"},{" + +"\"@odata.type\":\"#olingo.odata.test1.CTTwoPrim\"," + +"\"[email protected]\":\"#Int16\"," + +"\"PropertyInt16\":777," + +"\"PropertyString\":\"3 Test Complex in Complex Property\"" + +"}]}},{" + +"\"@odata.type\":\"#olingo.odata.test1.ETCompCollComp\"," + +"\"@odata.id\":\"ESCompCollComp(12345)\"," + +"\"[email protected]\":\"#Int16\"," + +"\"PropertyInt16\":12345," + +"\"PropertyComp\":{" + +"\"@odata.type\":\"#olingo.odata.test1.CTCompCollComp\"," + +"\"[email protected]\":\"#Collection(olingo.odata.test1.CTTwoPrim)\"," + +"\"CollPropertyComp\":[" + +"{" + +"\"@odata.type\":\"#olingo.odata.test1.CTTwoPrim\"," + +"\"[email protected]\":\"#Int16\"," + +"\"PropertyInt16\":888," + +"\"PropertyString\":\"11 Test Complex in Complex Property\"" + +"},{" + +"\"@odata.type\":\"#olingo.odata.test1.CTTwoPrim\"," + +"\"[email protected]\":\"#Int16\"," + +"\"PropertyInt16\":999," + +"\"PropertyString\":\"12 Test Complex in Complex Property\"" + +"},{" + +"\"@odata.type\":\"#olingo.odata.test1.CTTwoPrim\"," + +"\"[email protected]\":\"#Int16\"," + +"\"PropertyInt16\":0," + +"\"PropertyString\":\"13 Test Complex in Complex Property\"" + +"}]}}]}"; + Assert.assertEquals(expectedResult, resultString); + } + + @Test + public void selectComplexNestedCollectionOfComplexWithMetadataMinimal() throws Exception{ + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESCompCollComp"); + final EntityCollection entitySet = data.readAll(edmEntitySet); + InputStream result = serializer + .entityCollection(metadata, edmEntitySet.getEntityType(), entitySet, + EntityCollectionSerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).build()) + .build()) + .getContent(); + final String resultString = IOUtils.toString(result); + final String expectedResult = "{\"@odata.context\":\"$metadata#ESCompCollComp\"," + + "\"@odata.metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"value\":[{" + + "\"PropertyInt16\":32767," + + "\"PropertyComp\":{" + + "\"CollPropertyComp\":[" + + "{" + +"\"PropertyInt16\":555," + +"\"PropertyString\":\"1 Test Complex in Complex Property\"" + +"},{" + +"\"PropertyInt16\":666," + +"\"PropertyString\":\"2 Test Complex in Complex Property\"" + +"},{" + +"\"PropertyInt16\":777," + +"\"PropertyString\":\"3 Test Complex in Complex Property\"" + +"}]}},{" + +"\"PropertyInt16\":12345," + +"\"PropertyComp\":{" + +"\"CollPropertyComp\":[" + +"{" + +"\"PropertyInt16\":888," + +"\"PropertyString\":\"11 Test Complex in Complex Property\"" + +"},{" + +"\"PropertyInt16\":999," + +"\"PropertyString\":\"12 Test Complex in Complex Property\"" + +"},{" + +"\"PropertyInt16\":0," + +"\"PropertyString\":\"13 Test Complex in Complex Property\"" + +"}]}}]}"; + Assert.assertEquals(expectedResult, resultString); + } + + @Test + public void selectComplexNestedCollectionOfComplexWithMetadataNone() throws Exception{ + final String METADATA_TEXT = "@odata."; + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESCompCollComp"); + final EntityCollection entitySet = data.readAll(edmEntitySet); + InputStream result = serializerNoMetadata + .entityCollection(metadata, edmEntitySet.getEntityType(), entitySet, + EntityCollectionSerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).build()) + .build()) + .getContent(); + final String resultString = IOUtils.toString(result); + Assert.assertEquals(false, resultString.contains(METADATA_TEXT)); + } + @Test(expected = SerializerException.class) public void selectMissingId() throws Exception { final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim");
