Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix EdmStructuredObject default value getter #1949

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Microsoft.AspNet.OData.Shared/EdmStructuredObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,14 @@ internal static object GetDefaultValue(IEdmTypeReference propertyType)
(isCollection && propertyType.AsCollection().ElementType().IsPrimitive()))
{
// primitive or primitive collection
if (clrType == typeof(string))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edm.Primitive (System.String) is a built-in Primitive type.

EdmStructuredObject is used for complex or entity instance, the type of them is structured type. So, I don't know why should we add "primitive type" into "structured type"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, maybe the title is a bit misleading. It should be something like 'Fix EdmStructuredObject's properties default value getter'.
You are correct, but the EdmStructuredObject has properties, which can be primitives. So when you try to get a property from the EdmStructuredObject, if the property does not exist, a default value is returned. This PR aimed to fix how the default value for a property is obtained - fixing the GetDefaultValue method (which is used from TryGetPropertyValue). I don't know if this is the right place for the GetDefaultValue though.

{
return string.Empty;
}
else if (clrType.IsArray)
{
return Array.CreateInstance(clrType.GetElementType(), 0);
}
return Activator.CreateInstance(clrType);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ public static TheoryDataSet<IEdmTypeReference, object> 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));
Expand All @@ -223,10 +227,20 @@ public static TheoryDataSet<IEdmTypeReference, object> 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<double?>() },
{ new EdmCollectionTypeReference(new EdmCollectionType(nonnullableDouble)), new List<double>() },
{ new EdmCollectionTypeReference(new EdmCollectionType(nullableString)), new List<string>() },
{ new EdmCollectionTypeReference(new EdmCollectionType(nonnullableString)), new List<string>() },
{ new EdmCollectionTypeReference(new EdmCollectionType(nullableBinary)), new List<byte[]>() },
{ new EdmCollectionTypeReference(new EdmCollectionType(nonnullableBinary)), new List<byte[]>() },
{ new EdmCollectionTypeReference(new EdmCollectionType(nullableEntity)), new EdmEntityObjectCollection(entityCollection) },
};
}
Expand Down