WebApi icon indicating copy to clipboard operation
WebApi copied to clipboard

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

Open rafaelmotaalves opened this issue 10 months ago • 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.

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 avatar Apr 04 '24 20:04 rafaelmotaalves