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 for null string values #390

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 20 additions & 4 deletions Tests/Schema.NET.Test/ValuesJsonConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ public void WriteJson_Values_ZeroCountWritesNull()
Assert.Equal("{}", json);
}

[Fact]
public void WriteJson_ValuesNull_WritesNull()
{
var value = new Values<int?, string?>((string?)null);
var json = SerializeObject(value);
Assert.Equal("{}", json);
}

[Fact]
public void WriteJson_Values_OneCountWritesSingle()
{
Expand Down Expand Up @@ -87,6 +95,14 @@ public void WriteJson_TimeSpan_ISO8601_TimeOfDay()
Assert.Equal("{\"Property\":\"12:34:56\"}", json);
}

[Fact]
public void ReadJson_Values_NullValue_String()
{
var json = "{\"Property\":null}";
var result = DeserializeObject<Values<int, string?>>(json);
Assert.Null(result.Value2.First());
}

[Fact]
public void ReadJson_Values_SingleValue_String()
{
Expand Down Expand Up @@ -564,12 +580,12 @@ public void ReadJson_ImplicitExternalTypes_AllowSharedNamespace()
}

private static string SerializeObject<T>(T value)
where T : struct, IValues
=> SchemaSerializer.SerializeObject(new TestModel<T> { Property = value });
where T : struct, IValues =>
SchemaSerializer.SerializeObject(new TestModel<T> { Property = value });

private static T DeserializeObject<T>(string json)
where T : struct, IValues
=> SchemaSerializer.DeserializeObject<TestModel<T>>(json)!.Property;
where T : struct, IValues =>
SchemaSerializer.DeserializeObject<TestModel<T>>(json)!.Property;

private class TestModel<T>
where T : struct, IValues
Expand Down