Skip to content

Commit

Permalink
fix: null default value
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualBean committed Jan 9, 2025
1 parent f7d96d8 commit d8836e3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/LEGO.AsyncAPI/Models/Avro/AvroField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
23 changes: 23 additions & 0 deletions test/LEGO.AsyncAPI.Tests/Models/AvroSchema_Should.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AvroSchema>(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()
{
Expand Down

0 comments on commit d8836e3

Please sign in to comment.