diff options
author | youssefm <youssefm@microsoft.com> | 2012-10-09 22:06:18 +0400 |
---|---|---|
committer | youssefm <youssefm@microsoft.com> | 2012-10-09 22:53:18 +0400 |
commit | 5b7dd71fb1190fa2772ada7ba5d374b438947e4e (patch) | |
tree | 7e5444493470990569ad5c703678e9448197649e | |
parent | b1994655f1115acf3576badfad574093eaf9ba9f (diff) |
[OData] Allow serialization of complex type properties that have a null value
5 files changed, 40 insertions, 15 deletions
diff --git a/src/System.Web.Http.OData/OData/Formatter/Serialization/ODataCollectionSerializer.cs b/src/System.Web.Http.OData/OData/Formatter/Serialization/ODataCollectionSerializer.cs index c18fd953..587cef71 100644 --- a/src/System.Web.Http.OData/OData/Formatter/Serialization/ODataCollectionSerializer.cs +++ b/src/System.Web.Http.OData/OData/Formatter/Serialization/ODataCollectionSerializer.cs @@ -69,7 +69,11 @@ namespace System.Web.Http.OData.Formatter.Serialization } IEnumerable enumerable = graph as IEnumerable; - if (enumerable != null) + if (enumerable == null) + { + return new ODataProperty() { Name = elementName, Value = null }; + } + else { ArrayList valueCollection = new ArrayList(); @@ -90,8 +94,6 @@ namespace System.Web.Http.OData.Formatter.Serialization // Also, note that TypeName is an optional property for ODataCollectionValue return new ODataProperty() { Name = elementName, Value = new ODataCollectionValue { Items = valueCollection } }; } - - return null; } } } diff --git a/src/System.Web.Http.OData/OData/Formatter/Serialization/ODataComplexTypeSerializer.cs b/src/System.Web.Http.OData/OData/Formatter/Serialization/ODataComplexTypeSerializer.cs index 7013c961..0b0f5df0 100644 --- a/src/System.Web.Http.OData/OData/Formatter/Serialization/ODataComplexTypeSerializer.cs +++ b/src/System.Web.Http.OData/OData/Formatter/Serialization/ODataComplexTypeSerializer.cs @@ -47,11 +47,13 @@ namespace System.Web.Http.OData.Formatter.Serialization throw Error.ArgumentNull("writeContext"); } - List<ODataProperty> propertyCollection = null; - - if (graph != null) + if (graph == null) { - propertyCollection = new List<ODataProperty>(); + return new ODataProperty() { Name = elementName, Value = null }; + } + else + { + List<ODataProperty> propertyCollection = new List<ODataProperty>(); foreach (IEdmProperty property in _edmComplexType.ComplexDefinition().Properties()) { IEdmTypeReference propertyType = property.Type; @@ -66,10 +68,6 @@ namespace System.Web.Http.OData.Formatter.Serialization propertyCollection.Add(propertySerializer.CreateProperty(propertyValue, property.Name, writeContext)); } - } - - if (propertyCollection != null) - { return new ODataProperty() { Name = elementName, @@ -80,10 +78,6 @@ namespace System.Web.Http.OData.Formatter.Serialization } }; } - else - { - return null; - } } } } diff --git a/test/System.Web.Http.OData.Test/OData/Formatter/Serialization/ODataCollectionSerializerTests.cs b/test/System.Web.Http.OData.Test/OData/Formatter/Serialization/ODataCollectionSerializerTests.cs index f78652be..d03c3f4d 100644 --- a/test/System.Web.Http.OData.Test/OData/Formatter/Serialization/ODataCollectionSerializerTests.cs +++ b/test/System.Web.Http.OData.Test/OData/Formatter/Serialization/ODataCollectionSerializerTests.cs @@ -53,5 +53,14 @@ namespace System.Web.Http.OData.Formatter.Serialization Assert.Equal(elements, new int[] { 1, 2, 3 }); } + + [Fact] + public void CreateProperty_ReturnsODataProperty_ForNullValue() + { + var property = _serializer.CreateProperty(null, "TestCollection", new ODataSerializerContext()); + + Assert.NotNull(property); + Assert.Null(property.Value); + } } } diff --git a/test/System.Web.Http.OData.Test/OData/Formatter/Serialization/ODataComplexTypeSerializerTests.cs b/test/System.Web.Http.OData.Test/OData/Formatter/Serialization/ODataComplexTypeSerializerTests.cs index 78174117..b9d87118 100644 --- a/test/System.Web.Http.OData.Test/OData/Formatter/Serialization/ODataComplexTypeSerializerTests.cs +++ b/test/System.Web.Http.OData.Test/OData/Formatter/Serialization/ODataComplexTypeSerializerTests.cs @@ -52,5 +52,14 @@ namespace System.Web.Http.OData.Formatter.Serialization Tuple.Create("Country", "United States"), Tuple.Create("ZipCode","98052") }); } + + [Fact] + public void CreateProperty_ReturnsODataProperty_ForNullValue() + { + var property = _serializer.CreateProperty(null, "ComplexElement", new ODataSerializerContext()); + + Assert.NotNull(property); + Assert.Null(property.Value); + } } } diff --git a/test/System.Web.Http.OData.Test/OData/Formatter/Serialization/ODataPrimitiveSerializerTests.cs b/test/System.Web.Http.OData.Test/OData/Formatter/Serialization/ODataPrimitiveSerializerTests.cs index 2601a2f3..20b74ebd 100644 --- a/test/System.Web.Http.OData.Test/OData/Formatter/Serialization/ODataPrimitiveSerializerTests.cs +++ b/test/System.Web.Http.OData.Test/OData/Formatter/Serialization/ODataPrimitiveSerializerTests.cs @@ -112,6 +112,17 @@ namespace System.Web.Http.OData.Formatter.Serialization Assert.Equal(odataProperty.Value, 20); } + [Fact] + public void CreateProperty_ReturnsODataProperty_ForNullValue() + { + IEdmPrimitiveTypeReference edmPrimitiveType = EdmLibHelpers.GetEdmPrimitiveTypeReferenceOrNull(typeof(string)); + var serializer = new ODataPrimitiveSerializer(edmPrimitiveType); + var property = serializer.CreateProperty(null, "elementName", new ODataSerializerContext()); + + Assert.NotNull(property); + Assert.Null(property.Value); + } + [Theory] [PropertyData("EdmPrimitiveData")] [PropertyData("NonEdmPrimitiveData")] |