ch-robinson / dotnet-avro

An Avro implementation for .NET
https://engineering.chrobinson.com/dotnet-avro/
MIT License
135 stars 51 forks source link

Exception when building a serializer for type with a missing field from the schema #255

Closed nicodeslandes closed 1 year ago

nicodeslandes commented 1 year ago

I'm getting an error when attempting to create a serializer for my schema.

Here's the schema, which include a new field called Values, which is an array of records with a default value of []:

{
  "type": "record",
  "name": "Order",
  "namespace": "test",
  "fields": [
    { "name": "Id", "type": "int" },
    {
      "name": "Values",
      "type": {
        "type": "array",
        "items": {
          "type": "record",
          "name": "InnerObject",
          "fields": [
            { "name": "Value", "type": "int" }
          ]
        }
      },
      "default": []
    }
  ]
}

Things work fine if I the C# type I use contains the new field:

class InnerObject
{
    public int Value { get; set; }
}

class Order
{
    public int Id { get; set; }
    public InnerObject[] Values { get; set; } = Array.Empty<InnerObject>();
}

// OK
var serializer = new BinarySerializerBuilder().BuildDelegate<Order>(schema);

But if I attempt to generate the serializer with the "old" version of my C# type, ie without the Values property, the serializer builder fails with this error: System.InvalidOperationException: 'variable '' of type 'System.Collections.Generic.IEnumerator'1[System.Object]' referenced from scope 'InnerObject serializer', but it is not defined'


class OrderLegacy
{
    public int Id { get; set; }
}

// FAIL: System.InvalidOperationException
var serializer = new BinarySerializerBuilder().BuildDelegate<Order>(schema);
dstelljes commented 1 year ago

Thanks again for the fix; will release with 9.5.1 later today.

nicodeslandes commented 1 year ago

No problem. Thank you so much for merging that PR so quickly!