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

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

Open
rafaelmotaalves opened this issue Apr 4, 2024 · 2 comments
Open

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

rafaelmotaalves opened this issue Apr 4, 2024 · 2 comments
Assignees

Comments

@rafaelmotaalves
Copy link

rafaelmotaalves commented Apr 4, 2024

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)
    

@habbes habbes self-assigned this Apr 9, 2024
@xuzhg
Copy link
Member

xuzhg commented May 6, 2024

@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

@habbes
Copy link
Contributor

habbes commented May 9, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants