Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: null representation for AvroField 'default' property #201

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -20,8 +20,8 @@

[Display("ignore")]
Ignore,
}

Check warning on line 23 in src/LEGO.AsyncAPI/Models/Avro/AvroField.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

/// <summary>

Check warning on line 24 in src/LEGO.AsyncAPI/Models/Avro/AvroField.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Check warning on line 24 in src/LEGO.AsyncAPI/Models/Avro/AvroField.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Check warning on line 24 in src/LEGO.AsyncAPI/Models/Avro/AvroField.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

/// Represents a field within an Avro record schema.
/// </summary>
public class AvroField : IAsyncApiSerializable
Expand Down Expand Up @@ -67,7 +67,18 @@
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
Loading