Invalid JSON when using List<List<?>> property
Opened this issue · 2 comments
rafaelmotaalves commented
I've been running into issues when trying to specify a model with a property with a List<List> property. This is the model I'm defining:
public class WeatherForecast
{
public string ID { get; set; }
public List<List<int>> Data2D { get; set; }
}
But when I try to request to an API that returns this model, I get a malformed JSON and an error is thrown.
Assemblies affected
Microsoft.AspNetCore.OData 7.7.4
Reproduce steps
- Define a model to be returned from OData with a property of type List<List>
- Return it from a GET API.
- Execute a request to this API.
Expected result
{"@odata.context":"https://localhost:7060/odata/$metadata#WeatherForecast/$entity","ID":"Id","Data2D":[[1, 2, 3]] }
Actual result
{"@odata.context":"https://localhost:7060/odata/$metadata#WeatherForecast/$entity","ID":"Id","Data2D":[
And get the following exception:
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
An unhandled exception has occurred while executing the request.
System.Runtime.Serialization.SerializationException: ODataResourceSerializer cannot write an object of type 'Collection(Edm.Int32)'.
at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSerializer.GetResourceType(Object graph, ODataSerializerContext writeContext)
at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSerializer.WriteResourceAsync(Object graph, ODataWriter writer, ODataSerializerContext writeContext, IEdmTypeReference expectedType)
at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSetSerializer.WriteResourceSetAsync(IEnumerable enumerable, IEdmTypeReference resourceSetType, ODataWriter writer, ODataSerializerContext writeContext)
at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSerializer.WriteComplexAndExpandedNavigationPropertyAsync(IEdmProperty edmProperty, SelectItem selectItem, ResourceContext resourceContext, ODataWriter writer)
at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSerializer.WriteComplexPropertiesAsync(SelectExpandNode selectExpandNode, ResourceContext resourceContext, ODataWriter writer)
at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSerializer.WriteResourceAsync(Object graph, ODataWriter writer, ODataSerializerContext writeContext, IEdmTypeReference expectedType)
at Microsoft.AspNet.OData.Formatter.Serialization.ODataResourceSerializer.WriteObjectAsync(Object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
xuzhg commented
@rafaelmotaalves Collection of Collection is not supported in OData.
By the way, you can have this functionality using 'Edm.Untyped' if you use Microsoft.AspNetCore.OData 8 version. If you are interested it, you can refer to this