Skip to content

Commit 71dea01

Browse files
committed
fix: null default value
1 parent f7d96d8 commit 71dea01

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/LEGO.AsyncAPI/Models/Avro/AvroField.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,18 @@ public void SerializeV2(IAsyncApiWriter writer)
6767
writer.WriteOptionalProperty("name", this.Name);
6868
writer.WriteOptionalObject("type", this.Type, (w, s) => s.SerializeV2(w));
6969
writer.WriteOptionalProperty("doc", this.Doc);
70-
writer.WriteOptionalObject("default", this.Default, (w, s) => w.WriteAny(s));
70+
writer.WriteOptionalObject("default", this.Default, (w, s) =>
71+
{
72+
if (s.TryGetValue(out string value) && value == "null")
73+
{
74+
w.WriteNull();
75+
}
76+
else
77+
{
78+
w.WriteAny(s);
79+
}
80+
});
81+
7182
if (this.Order != AvroFieldOrder.None)
7283
{
7384
writer.WriteOptionalProperty("order", this.Order.GetDisplayName());

test/LEGO.AsyncAPI.Tests/Models/AvroSchema_Should.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,29 @@ namespace LEGO.AsyncAPI.Tests.Models
1010

1111
public class AvroSchema_Should
1212
{
13+
[Test]
14+
public void Serialize_WithDefaultNull_SetJsonNull()
15+
{
16+
var input = """
17+
type: record
18+
name: User
19+
namespace: Producer
20+
doc: ESP Schema validation test
21+
fields:
22+
- name: userId
23+
type: int
24+
- name: userEmail
25+
type:
26+
- null
27+
- string
28+
default: null
29+
""";
30+
31+
var model = new AsyncApiStringReader().ReadFragment<AvroSchema>(input, AsyncApiVersion.AsyncApi2_0, out var diag);
32+
var reserialized = model.SerializeAsJson(AsyncApiVersion.AsyncApi2_0);
33+
reserialized.Should().Contain("default\": null");
34+
}
35+
1336
[Test]
1437
public void Deserialize_WithMetadata_CreatesMetadata()
1538
{

0 commit comments

Comments
 (0)