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 aae934c0b..d72dbc7a1 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 @@ -370,7 +370,7 @@ public class ODataJsonSerializer implements ODataSerializer { writeComplexValue(type, ((LinkedComplexValue) value).getValue(), selectedPaths, json); break; case COLLECTION_COMPLEX: - writeComplexValue(type, property.asComplex(), selectedPaths, json); + writeComplexValue(type, ((Property) value).asComplex(), selectedPaths, json); break; default: throw new SerializerException("Property type not yet supported!", diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/json/ComplexTypeHelper.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/json/ComplexTypeHelper.java new file mode 100644 index 000000000..83368d6d2 --- /dev/null +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/json/ComplexTypeHelper.java @@ -0,0 +1,66 @@ +/* + * 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.server.core.serializer.json; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.olingo.commons.api.ODataException; +import org.apache.olingo.commons.api.edm.EdmComplexType; +import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind; +import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.server.api.edm.provider.ComplexType; +import org.apache.olingo.server.api.edm.provider.EdmProvider; +import org.apache.olingo.server.api.edm.provider.NavigationProperty; +import org.apache.olingo.server.api.edm.provider.Property; +import org.apache.olingo.server.core.edm.provider.EdmComplexTypeImpl; +import org.apache.olingo.server.core.edm.provider.EdmProviderImpl; + +public class ComplexTypeHelper { + + public static EdmComplexType createType() throws ODataException { + EdmProvider provider = mock(EdmProvider.class); + EdmProviderImpl edm = new EdmProviderImpl(provider); + + FullQualifiedName baseName = new FullQualifiedName("namespace", "BaseTypeName"); + ComplexType baseComplexType = new ComplexType(); + List baseProperties = new ArrayList(); + baseProperties.add(new Property().setName("prop1").setType(EdmPrimitiveTypeKind.String.getFullQualifiedName())); + List baseNavigationProperties = new ArrayList(); + baseNavigationProperties.add(new NavigationProperty().setName("nav1")); + baseComplexType.setName("BaseTypeName").setAbstract(false).setOpenType(false).setProperties(baseProperties) + .setNavigationProperties(baseNavigationProperties); + when(provider.getComplexType(baseName)).thenReturn(baseComplexType); + + FullQualifiedName name = new FullQualifiedName("namespace", "typeName"); + ComplexType complexType = new ComplexType().setBaseType(baseName); + List properties = new ArrayList(); + properties.add(new Property().setName("prop2").setType(EdmPrimitiveTypeKind.String.getFullQualifiedName())); + List navigationProperties = new ArrayList(); + navigationProperties.add(new NavigationProperty().setName("nav2")); + complexType.setName("BaseTypeName").setAbstract(false).setOpenType(false).setProperties(properties) + .setNavigationProperties(navigationProperties); + when(provider.getComplexType(name)).thenReturn(complexType); + + return EdmComplexTypeImpl.getInstance(edm, name, complexType); + } +} diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerTest.java new file mode 100644 index 000000000..da7cb086d --- /dev/null +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerTest.java @@ -0,0 +1,71 @@ +/* + * 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.server.core.serializer.json; + +import static org.junit.Assert.assertEquals; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; + +import org.apache.olingo.commons.api.ODataException; +import org.apache.olingo.commons.api.data.ContextURL; +import org.apache.olingo.commons.api.data.Property; +import org.apache.olingo.commons.api.data.ValueType; +import org.apache.olingo.commons.api.format.ODataFormat; +import org.apache.olingo.commons.core.data.PropertyImpl; +import org.apache.olingo.server.api.serializer.ComplexSerializerOptions; +import org.junit.Test; + +public class ODataJsonSerializerTest { + @Test + public void testCollectionComplex() throws ODataException, IOException { + final List col = new ArrayList(); + col.add(new PropertyImpl(null, "ComplexOne", ValueType.COMPLEX, getValues(1))); + col.add(new PropertyImpl(null, "ComplexTwo", ValueType.COMPLEX, getValues(2))); + final Property complexCollection = new PropertyImpl(null, "ComplexCol", ValueType.COLLECTION_COMPLEX, col); + + final ODataJsonSerializer serializer = new ODataJsonSerializer(ODataFormat.APPLICATION_JSON); + final ComplexSerializerOptions options = ComplexSerializerOptions.with() + .contextURL(ContextURL.with().selectList("ComplexCollection").build()).build(); + final InputStream in = serializer.complexCollection(ComplexTypeHelper.createType(), complexCollection, options); + final BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + + String line; + while ((line = reader.readLine()) != null) { + if (line.contains("value")) { + assertEquals("{\"@odata.context\":\"$metadata(ComplexCollection)\",\"value\":" + + "[{\"prop1\":\"test1\",\"prop2\":\"test11\"},{\"prop1\":\"test2\",\"prop2\":\"test22\"}]}", line); + } + } + + } + + private List getValues(int i) { + final List values = new ArrayList(); + + values.add(new PropertyImpl(null, "prop1", ValueType.PRIMITIVE, "test" + i)); + values.add(new PropertyImpl(null, "prop2", ValueType.PRIMITIVE, "test" + i + i)); + + return values; + } +}