ch-robinson / dotnet-avro

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

Unable to generate a schema with default values #199

Closed nicodeslandes closed 2 years ago

nicodeslandes commented 2 years ago

Hi,

I'm having trouble with the newly added support for default values in 8.0.1 when attempting to generate a schema. Here's a sample code that highlights the issue:

using Chr.Avro.Abstract;
using Chr.Avro.Representation;
using DefaultValueAttribute = System.ComponentModel.DefaultValueAttribute;

var schemaBuilder = new SchemaBuilder();
var schema = schemaBuilder.BuildSchema<MyRecord>();
var writer = new JsonSchemaWriter();

Console.WriteLine(writer.Write(schema));

public class MyRecord
{
    [DefaultValue(null)]
    public int? Value { get; set; }
}

The default value null set on the record property causes an exception in the JsonSchemaWriter:

Unhandled exception. Chr.Avro.UnsupportedSchemaException: The default value of the Value field on RecordSchema MyRecord must be a JSON value.
   at Chr.Avro.Representation.JsonRecordSchemaWriterCase.Write(Schema schema, Utf8JsonWriter json, Boolean canonical, JsonSchemaWriterContext context)
   at Chr.Avro.Representation.JsonSchemaWriter.Write(Schema schema, Utf8JsonWriter json, Boolean canonical, JsonSchemaWriterContext context)
   at Chr.Avro.Representation.JsonSchemaWriter.Write(Schema schema, Stream stream, Boolean canonical, JsonSchemaWriterContext context)
   at Chr.Avro.Representation.JsonSchemaWriter.Write(Schema schema, Boolean canonical, JsonSchemaWriterContext context)
   at Program.<Main>$(String[] args) in C:\Projects\Avro\AvroSchemaGeneration\AvroSchemaGeneration\Program.cs:line 9

The same error appears when running dotnet avro create, or when attempting to serialize the record.

dstelljes commented 2 years ago

Thanks for the detailed report; we somehow missed this in prerelease testing. 8.0.2 will be out shortly with a fix.

nicodeslandes commented 2 years ago

50 min for a merged PR! Pheew, that was fast! 😲 Thanks!