OData / odata.net

ODataLib: Open Data Protocol - .NET Libraries and Frameworks
https://docs.microsoft.com/odata
Other
687 stars 349 forks source link

OData v4 client does not support null values for properties of ICollection<TComplexObject> #1641

Open newclaus opened 4 years ago

newclaus commented 4 years ago

OData Web Api Client throws exception in case of passing null value to property of ICollection during executing action. Although my EdmModel supports null-value

Assemblies affected

*Which assemblies and versions are known to be affected e.g. Microsoft.OData.Client 7.6.2

Reproduce steps

Call service action and pass object with null-value property

Expected result

Null values allowed

Actual result

Exception is thrown

Additional detail

Node Name="Collection" has no attribute 'Nullable', so it is nullable by default

` ...

` Exception has the following info _Timestamp: 2020-01-16 11:54:14.572 +07:00 Message: The value of the property 'Collection' is null. Properties that are a collection type of primitive or complex types cannot be null. Level: Error Exception: System.InvalidOperationException: The value of the property 'Collection' is null. Properties that are a collection type of primitive or complex types cannot be null. at Microsoft.OData.Client.WebUtil.ValidateCollection(Type collectionItemType, Object propertyValue, String propertyName, Boolean isDynamicProperty) at Microsoft.OData.Client.ODataPropertyConverter.CreateODataResourceSetWrapperForComplexCollection(Type collectionItemType, String propertyName, Object value, HashSet`1 visitedComplexTypeObjects, Boolean isDynamicProperty, Boolean setTypeAnnotation) at Microsoft.OData.Client.ODataPropertyConverter.CreateODataComplexCollectionPropertyResourceSet(ClientPropertyAnnotation property, Object propertyValue, String serverTypeName, HashSet`1 visitedComplexTypeObjects) at Microsoft.OData.Client.ODataPropertyConverter.TryConvertPropertyToResourceOrResourceSet(ClientPropertyAnnotation property, Object propertyValue, String serverTypeName, HashSet`1 visitedComplexTypeObjects, ODataItemWrapper& odataItem) at Microsoft.OData.Client.ODataPropertyConverter.PopulateNestedComplexProperties(Object resource, String serverTypeName, IEnumerable`1 properties, HashSet`1 visitedComplexTypeObjects) at Microsoft.OData.Client.ODataPropertyConverter.CreateODataResourceWrapper(Type entityType, Object value, ClientPropertyAnnotation[] properties) at Microsoft.OData.Client.Serializer.WriteBodyOperationParameters(List`1 operationParameters, ODataRequestMessageWrapper requestMessage) at Microsoft.OData.Client.DataServiceRequest.CreateExecuteResult(Object source, DataServiceContext context, AsyncCallback callback, Object state, String method) at Microsoft.OData.Client.DataServiceContext.InnerBeginExecute[TElement](Uri requestUri, AsyncCallback callback, Object state, String httpMethod, String method, Nullable`1 singleResult, OperationParameter[] operationParameters) at Microsoft.OData.Client.DataServiceContext.BeginExecute[TElement](Uri requestUri, AsyncCallback callback, Object state, String httpMethod, Boolean singleResult, OperationParameter[] operationParameters) at Microsoft.OData.Client.DataServiceActionQuerySingle`1.BeginGetValue(AsyncCallback callback, Object state) at System.Threading.Tasks.TaskFactory`1.FromAsyncImpl(Func`3 beginMethod, Func`2 endFunction, Action`1 endAction, Object state, TaskCreationOptions creationOptions) at System.Threading.Tasks.TaskFactory`1.FromAsync(Func`3 beginMethod, Func`2 endMethod, Object state) ..._
newclaus commented 4 years ago

The same problem has occurred during deserialization of complex types

Sreejithpin commented 4 years ago

@newclaus Did you explicitly specify nullable true for the collection property? As per spec you cannot asume nullable =true by default As per spec: If no value is specified for a collection-valued property, the client cannot assume any default value http://docs.oasis-open.org/odata/odata-csdl-xml/v4.01/odata-csdl-xml-v4.01.html#_Toc26368806

newclaus commented 4 years ago

@Sreejithpin, yes, I did. Furthermore, the property in client EDM model has Type

Microsoft.OData.Edm.Csdl.CsdlSemantics.CsdlSemanticsCollectionTypeExpression

and debugger displays value like

{[Collection([ItemModel Nullable=True]) Nullable=True]}