diff --git a/src/SystemTextJsonNullTests/UnitTest1.cs b/src/SystemTextJsonNullTests/UnitTest1.cs index cba1a0d..e941b6c 100644 --- a/src/SystemTextJsonNullTests/UnitTest1.cs +++ b/src/SystemTextJsonNullTests/UnitTest1.cs @@ -2,7 +2,6 @@ using System.Text.Json; using System.Text.Json.Serialization; using System.Text.Json.Serialization.Metadata; -using Newtonsoft.Json.Linq; namespace SystemTextJsonNullTests; @@ -62,7 +61,7 @@ where setProperty is not null } [Fact] - public void RequireValueNull() + public void RequireValueNullShouldBeException() { var json = """ { @@ -70,192 +69,220 @@ public void RequireValueNull() } """; - var exception = Assert.Throws(() => JsonSerializer.Deserialize(json, JsonSerializerOptions)); + var bcl = JsonSerializer.Deserialize(json); + Assert.Null(bcl.RequireValue); - Assert.Equal("Null value not allowed for 'requireValue'", exception.Message); + Assert.Throws(() => JsonSerializer.Deserialize(json, JsonSerializerOptions)); } [Fact] - public void RequireValueNull2() + public void RequireValueNullShouldBeException2() { var json = "{}"; - var exception = Assert.Throws(() => JsonSerializer.Deserialize(json, JsonSerializerOptions)); + Assert.Throws(() => JsonSerializer.Deserialize(json)); - Assert.Equal("JSON deserialization for type 'SystemTextJsonNullTests.Dto' was missing required properties, including the following: requireValue", exception.Message); + Assert.Throws(() => JsonSerializer.Deserialize(json, JsonSerializerOptions)); } [Fact] - public void NotNullDefaultValueNull() + public void NullableValueShouldBeNull() { var json = """ { - "RequireValue": "", - "NotNullDefaultValue": null + "RequireValue": "...", + "NullableValue": null } """; - var sut = JsonSerializer.Deserialize(json, JsonSerializerOptions); + var bcl = JsonSerializer.Deserialize(json); + Assert.Null(bcl.NullableValue); - Assert.Equal(sut.RequireValue, ""); - Assert.Equal(sut.NotNullDefaultValue, ""); + var aums = JsonSerializer.Deserialize(json, JsonSerializerOptions); + Assert.Null(aums.NullableValue); } [Fact] - public void NotNullDefaultValueNull2() + public void NullableValueShouldBeNull2() { var json = """ { - "RequireValue": "" + "RequireValue": "..." } """; - var sut = JsonSerializer.Deserialize(json, JsonSerializerOptions); + var bcl = JsonSerializer.Deserialize(json); + Assert.Null(bcl.NullableValue); - Assert.Equal(sut.RequireValue, ""); - Assert.Equal(sut.NotNullDefaultValue, ""); + var aums = JsonSerializer.Deserialize(json, JsonSerializerOptions); + Assert.Null(aums.NullableValue); } [Fact] - public void NullableDefaultValueExpectDefault() + public void NullableValueExpectCCC() { var json = """ { - "RequireValue": "" + "RequireValue": "...", + "NullableValue": "CCC" } """; - var sut = JsonSerializer.Deserialize(json, JsonSerializerOptions); + var bcl = JsonSerializer.Deserialize(json); + Assert.Equal("CCC", bcl.NullableValue); - Assert.Equal(sut.RequireValue, ""); - Assert.Equal("", sut.NullableDefaultValue); + var aums = JsonSerializer.Deserialize(json, JsonSerializerOptions); + Assert.Equal("CCC", aums.NullableValue); } [Fact] - public void NullableDefaultValueExpectNull() + public void NotNullDefaultValueShouldBeDefault() { var json = """ { - "RequireValue": "", - "NullableDefaultValue": null + "RequireValue": "...", + "NotNullDefaultValue": null } """; - var sut = JsonSerializer.Deserialize(json, JsonSerializerOptions); + var bcl = JsonSerializer.Deserialize(json); + Assert.Null(bcl.NotNullDefaultValue); - Assert.Equal(sut.RequireValue, ""); - Assert.Equal(sut.NullableDefaultValue, null); + var aums = JsonSerializer.Deserialize(json, JsonSerializerOptions); + Assert.Equal("initialized", aums.NotNullDefaultValue); } [Fact] - public void NullableDefaultValueExpectAAA() + public void NotNullDefaultValueShouldBeDefault2() { var json = """ { - "RequireValue": "", - "NullableDefaultValue": "AAA" + "RequireValue": "..." } """; - var sut = JsonSerializer.Deserialize(json, JsonSerializerOptions); + var bcl = JsonSerializer.Deserialize(json); + Assert.Equal("initialized", bcl.NotNullDefaultValue); - Assert.Equal("", sut.RequireValue); - Assert.Equal("AAA", sut.NullableDefaultValue); + var aums = JsonSerializer.Deserialize(json, JsonSerializerOptions); + Assert.Equal("initialized", aums.NotNullDefaultValue); } [Fact] - public void NullableValueExpectDefault() + public void NotNullDefaultValueShouldBeKKK() { var json = """ { - "RequireValue": "" + "RequireValue": "...", + "NotNullDefaultValue": "KKK" } """; - var sut = JsonSerializer.Deserialize(json, JsonSerializerOptions); + var bcl = JsonSerializer.Deserialize(json); + Assert.Equal("KKK", bcl.NotNullDefaultValue); - Assert.Equal(sut.RequireValue, ""); - Assert.Equal(sut.NullableValue, null); + var aums = JsonSerializer.Deserialize(json, JsonSerializerOptions); + Assert.Equal("KKK", aums.NotNullDefaultValue); } [Fact] - public void NullableValueExpectNull() + public void NullableDefaultValueShouldBeNull() { var json = """ { - "RequireValue": "", - "NullableValue": null + "RequireValue": "...", + "NullableDefaultValue": null } """; - var sut = JsonSerializer.Deserialize(json, JsonSerializerOptions); + var bcl = JsonSerializer.Deserialize(json); + Assert.Equal("initialized", bcl.NotNullDefaultValue); - Assert.Equal(sut.RequireValue, ""); - Assert.Equal(sut.NullableValue, null); + var sut = JsonSerializer.Deserialize(json, JsonSerializerOptions); + Assert.Null(sut.NullableDefaultValue); } [Fact] - public void NullableValueExpectCCC() + public void NullableDefaultValueShouldBeDefault() { var json = """ { - "RequireValue": "", - "NullableValue": "CCC" + "RequireValue": "..." } """; - var sut = JsonSerializer.Deserialize(json, JsonSerializerOptions); + var bcl = JsonSerializer.Deserialize(json); + Assert.Equal("initialized", bcl.NotNullDefaultValue); - Assert.Equal("", sut.RequireValue); - Assert.Equal(sut.NullableValue, "CCC"); + var aums = JsonSerializer.Deserialize(json, JsonSerializerOptions); + Assert.Equal("initialized", aums.NullableDefaultValue); } + [Fact] + public void NullableDefaultValueShouldBeAAA() + { + var json = """ + { + "RequireValue": "...", + "NullableDefaultValue": "AAA" + } + """; + + var bcl = JsonSerializer.Deserialize(json, JsonSerializerOptions); + Assert.Equal("AAA", bcl.NullableDefaultValue); + + var aums = JsonSerializer.Deserialize(json, JsonSerializerOptions); + Assert.Equal("AAA", aums.NullableDefaultValue); + } [Fact] - public void StringValuesExpectDefault() + public void StringValuesShouldBeEmpty() { var json = """ { - "RequireValue": "" + "RequireValue": "..." } """; - var sut = JsonSerializer.Deserialize(json, JsonSerializerOptions); + var bcl = JsonSerializer.Deserialize(json); + Assert.Equal([], bcl.StringValues); - Assert.Equal(sut.RequireValue, ""); - Assert.Equal(sut.StringValues, []); + var aums = JsonSerializer.Deserialize(json, JsonSerializerOptions); + Assert.Equal([], aums.StringValues); } [Fact] - public void StringValuesExpectNull() + public void StringValuesShouldBeEmpty2() { var json = """ { - "RequireValue": "", + "RequireValue": "...", "StringValues": null } """; - var sut = JsonSerializer.Deserialize(json, JsonSerializerOptions); + var bcl = JsonSerializer.Deserialize(json); + Assert.Null(bcl.StringValues); - Assert.Equal(sut.RequireValue, ""); - Assert.Equal(sut.StringValues, []); + var aums = JsonSerializer.Deserialize(json, JsonSerializerOptions); + Assert.Equal([], aums.StringValues); } [Fact] - public void StringValuesExpectCCC() + public void StringValuesShouldBeCCC() { var json = """ { - "RequireValue": "", + "RequireValue": "...", "StringValues": ["CCC"] } """; - var sut = JsonSerializer.Deserialize(json, JsonSerializerOptions); + var bcl = JsonSerializer.Deserialize(json); + Assert.Equal(["CCC"], bcl.StringValues); - Assert.Equal("", sut.RequireValue); - Assert.Equal(sut.StringValues, ["CCC"]); + var aums = JsonSerializer.Deserialize(json, JsonSerializerOptions); + Assert.Equal(["CCC"], aums.StringValues); } } @@ -263,8 +290,8 @@ public void StringValuesExpectCCC() public record Dto { public required string RequireValue { get; init; } - public string NotNullDefaultValue { get; init; } = ""; - public string? NullableDefaultValue { get; init; } = ""; public string? NullableValue { get; init; } + public string NotNullDefaultValue { get; init; } = "initialized"; + public string? NullableDefaultValue { get; init; } = "initialized"; public IEnumerable StringValues { get; init; } = []; }