diff --git a/src/LEGO.AsyncAPI/Models/Avro/AvroField.cs b/src/LEGO.AsyncAPI/Models/Avro/AvroField.cs index 6e8f9ed8..659f1a4a 100644 --- a/src/LEGO.AsyncAPI/Models/Avro/AvroField.cs +++ b/src/LEGO.AsyncAPI/Models/Avro/AvroField.cs @@ -67,7 +67,18 @@ public void SerializeV2(IAsyncApiWriter writer) writer.WriteOptionalProperty("name", this.Name); writer.WriteOptionalObject("type", this.Type, (w, s) => s.SerializeV2(w)); writer.WriteOptionalProperty("doc", this.Doc); - writer.WriteOptionalObject("default", this.Default, (w, s) => w.WriteAny(s)); + writer.WriteOptionalObject("default", this.Default, (w, s) => + { + if (s.TryGetValue(out string value) && value == "null") + { + w.WriteNull(); + } + else + { + w.WriteAny(s); + } + }); + if (this.Order != AvroFieldOrder.None) { writer.WriteOptionalProperty("order", this.Order.GetDisplayName()); diff --git a/test/LEGO.AsyncAPI.Tests/Models/AvroSchema_Should.cs b/test/LEGO.AsyncAPI.Tests/Models/AvroSchema_Should.cs index 0ba1e80e..4dcce166 100644 --- a/test/LEGO.AsyncAPI.Tests/Models/AvroSchema_Should.cs +++ b/test/LEGO.AsyncAPI.Tests/Models/AvroSchema_Should.cs @@ -10,6 +10,29 @@ namespace LEGO.AsyncAPI.Tests.Models public class AvroSchema_Should { + [Test] + public void Serialize_WithDefaultNull_SetJsonNull() + { + var input = """ + type: record + name: User + namespace: Producer + doc: ESP Schema validation test + fields: + - name: userId + type: int + - name: userEmail + type: + - null + - string + default: null + """; + + var model = new AsyncApiStringReader().ReadFragment(input, AsyncApiVersion.AsyncApi2_0, out var diag); + var reserialized = model.SerializeAsJson(AsyncApiVersion.AsyncApi2_0); + reserialized.Should().Contain("default\": null"); + } + [Test] public void Deserialize_WithMetadata_CreatesMetadata() {