From e9f1fcd64f39380d305982e768ac6ea960e99dc4 Mon Sep 17 00:00:00 2001 From: Atanas Dzhilyanov Date: Tue, 5 Nov 2019 11:57:42 -0500 Subject: [PATCH] Fix EdmStructuredObject default value getter --- .../EdmStructuredObject.cs | 8 ++++++++ .../EdmStructuredObjectTest.cs | 14 ++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/Microsoft.AspNet.OData.Shared/EdmStructuredObject.cs b/src/Microsoft.AspNet.OData.Shared/EdmStructuredObject.cs index b902532bf2..2f59c8a0ea 100644 --- a/src/Microsoft.AspNet.OData.Shared/EdmStructuredObject.cs +++ b/src/Microsoft.AspNet.OData.Shared/EdmStructuredObject.cs @@ -221,6 +221,14 @@ internal static object GetDefaultValue(IEdmTypeReference propertyType) (isCollection && propertyType.AsCollection().ElementType().IsPrimitive())) { // primitive or primitive collection + if (clrType == typeof(string)) + { + return string.Empty; + } + else if (clrType.IsArray) + { + return Array.CreateInstance(clrType.GetElementType(), 0); + } return Activator.CreateInstance(clrType); } else diff --git a/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/EdmStructuredObjectTest.cs b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/EdmStructuredObjectTest.cs index 7a29defafa..34bbd9caae 100644 --- a/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/EdmStructuredObjectTest.cs +++ b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/EdmStructuredObjectTest.cs @@ -209,6 +209,10 @@ public static TheoryDataSet GetDefaultValueTestData { IEdmTypeReference nonnullableDouble = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Double, isNullable: false); IEdmTypeReference nullableDouble = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Double, isNullable: true); + IEdmTypeReference nonnullableString = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.String, isNullable: false); + IEdmTypeReference nullableString = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.String, isNullable: true); + IEdmTypeReference nonnullableBinary = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Binary, isNullable: false); + IEdmTypeReference nullableBinary = EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Binary, isNullable: true); IEdmTypeReference nullableEntity = new EdmEntityTypeReference(new EdmEntityType("NS", "Entity"), isNullable: true); IEdmTypeReference nullableComplex = new EdmComplexTypeReference(new EdmComplexType("NS", "Complex"), isNullable: true); EdmCollectionTypeReference entityCollection = new EdmCollectionTypeReference(new EdmCollectionType(nullableEntity)); @@ -223,10 +227,20 @@ public static TheoryDataSet GetDefaultValueTestData { EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Date, isNullable: false), default(Date) }, { EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.TimeOfDay, isNullable: true), null }, { EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.TimeOfDay, isNullable: false), default(TimeOfDay) }, + { EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Duration, isNullable: true), null }, + { EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Duration, isNullable: false), default(TimeSpan) }, + { EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.String, isNullable: true), null }, + { EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.String, isNullable: false), String.Empty }, + { EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Binary, isNullable: true), null }, + { EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Binary, isNullable: false), new byte[0] }, { new EdmComplexTypeReference(new EdmComplexType("NS", "Complex"), isNullable: true), null }, { new EdmEntityTypeReference(new EdmEntityType("NS", "Entity"), isNullable: true), null }, { new EdmCollectionTypeReference(new EdmCollectionType(nullableDouble)), new List() }, { new EdmCollectionTypeReference(new EdmCollectionType(nonnullableDouble)), new List() }, + { new EdmCollectionTypeReference(new EdmCollectionType(nullableString)), new List() }, + { new EdmCollectionTypeReference(new EdmCollectionType(nonnullableString)), new List() }, + { new EdmCollectionTypeReference(new EdmCollectionType(nullableBinary)), new List() }, + { new EdmCollectionTypeReference(new EdmCollectionType(nonnullableBinary)), new List() }, { new EdmCollectionTypeReference(new EdmCollectionType(nullableEntity)), new EdmEntityObjectCollection(entityCollection) }, }; }