OData/WebApi

Invalid JSON when using List<List<?>> property

Opened this issue · 2 comments

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

  1. Define a model to be returned from OData with a property of type List<List>
  2. Return it from a GET API.
  3. Execute a request to this API.
  4. 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)
    

@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

@xuzhg we should also create an article in the official docs for this, maybe copy over the contents of the blog post?