Skip to content

Commit bfa8222

Browse files
committed
Fix Z.DynamicLinq.SystemTextJson when property is null
1 parent aa47b50 commit bfa8222

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

src/System.Linq.Dynamic.Core.SystemTextJson/Config/NormalizationNonExistingPropertyBehavior.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace System.Linq.Dynamic.Core.SystemTextJson.Config;
22

33
/// <summary>
4-
/// Specifies the behavior to use when setting a property vlue that does not exist or is missing during normalization.
4+
/// Specifies the behavior to use when setting a property value that does not exist or is missing during normalization.
55
/// </summary>
66
public enum NormalizationNonExistingPropertyBehavior
77
{

src/System.Linq.Dynamic.Core.SystemTextJson/Utils/NormalizeUtils.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,11 @@ private static JsonObject NormalizeObject(JsonObject source, Dictionary<string,
104104
{
105105
if (source.ContainsKey(key))
106106
{
107+
var value = source[key];
107108
#if NET8_0_OR_GREATER
108-
result[key] = source[key]!.DeepClone();
109+
result[key] = value?.DeepClone();
109110
#else
110-
result[key] = JsonNode.Parse(source[key]!.ToJsonString());
111+
result[key] = value != null ? JsonNode.Parse(value.ToJsonString()) : null;
111112
#endif
112113
}
113114
else

test/System.Linq.Dynamic.Core.NewtonsoftJson.Tests/NewtonsoftJsonTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Linq.Dynamic.Core.Exceptions;
2-
using System.Linq.Dynamic.Core.NewtonsoftJson.Config;
1+
using System.Linq.Dynamic.Core.NewtonsoftJson.Config;
32
using FluentAssertions;
43
using Newtonsoft.Json.Linq;
54
using Xunit;
@@ -13,11 +12,13 @@ public class NewtonsoftJsonTests
1312
[
1413
{
1514
"Name": "John",
16-
"Age": 30
15+
"Age": 30,
16+
"IsNull": null
1717
},
1818
{
1919
"Name": "Doe",
20-
"Age": 40
20+
"Age": 40,
21+
"AlsoNull": null
2122
}
2223
]
2324
""";

test/System.Linq.Dynamic.Core.SystemTextJson.Tests/SystemTextJsonTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ public class SystemTextJsonTests
1717
[
1818
{
1919
"Name": "John",
20-
"Age": 30
20+
"Age": 30,
21+
"IsNull": null
2122
},
2223
{
2324
"Name": "Doe",
24-
"Age": 40
25+
"Age": 40,
26+
"AlsoNull": null
2527
}
2628
]
2729
""";

0 commit comments

Comments
 (0)