edgedb / edgedb-net

The official .NET client library for EdgeDB
https://edgedb.com
Apache License 2.0
82 stars 9 forks source link

Driver wants to convert shape to wrong type #47

Closed rs-idesis closed 1 year ago

rs-idesis commented 1 year ago

Describe the bug A select query with links to shapes of different types but the of the same base type, like author and director of movies being all persons, results in conversion errors as long as the shapes' fields are queried in exactly the same way (same fields in the same order):

Exception source: EdgeDB.Net.Driver
EdgeDB.EdgeDBException: Failed to deserialize object to DB.Model.Movies.Movie
 ---> System.ArgumentException: Cannot convert DB.Model.Movies.Director to type DB.Model.Movies.Actor
   at EdgeDB.ObjectBuilder.ConvertTo(Type type, Object value)
   at ...
   --- End of inner exception stack trace ---
   at EdgeDB.Binary.Codecs.ObjectCodec.Deserialize(PacketReader& reader, CodecContext context)
   at EdgeDB.TypeBuilder.BuildObject(EdgeDBBinaryClient client, Type type, ObjectCodec codec, Data& data)
  ...

Reproduction The following query, where actors and directors are defined in the same way, cause the exception. As soon as the order of one shape is changed, everything is working.

        var query = $$"""
            select Movie {
                id,
                title,
                year,
                description,
                actors: {
                    id,
                    full_name,
                    first_name,
                    middle_name,
                    last_name,
                    image,
                    dod,
                    dob,
                    bio,
                    @list_order
                },
                directors: {
                    id,
                    full_name,
                    first_name,
                    middle_name,
                    last_name,
                    image,
                    dod,
                    dob,
                    bio,
                    @list_order
                }
            };
            """;
        await _Client.QueryAsync<Movie?>(query);

A full example with schema and db content can be found here.

Expected behavior Shapes should be converted to the correct object type, regardless of "field randomisation".

Versions (please complete the following information):

quinchs commented 1 year ago

This bug is caused by the codec cache, essentially:

My initial thoughts on how to fix this is to clone non-scalar codecs that show up more than once under different property names, I'll continue looking for a fix