Skip to content

Commit 5880d4f

Browse files
committed
bug fix
1 parent 6c9ee08 commit 5880d4f

File tree

7 files changed

+144
-4
lines changed

7 files changed

+144
-4
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace EntityChange;
4+
5+
/// <summary>
6+
/// Provides metadata about a set of types that is relevant to JSON serialization.
7+
/// </summary>
8+
[JsonSerializable(typeof(ChangeRecord))]
9+
[JsonSerializable(typeof(List<ChangeRecord>))]
10+
[JsonSerializable(typeof(IReadOnlyCollection<ChangeRecord>))]
11+
[JsonSerializable(typeof(IEnumerable<ChangeRecord>))]
12+
public partial class EntityChangeSerializerContext : JsonSerializerContext
13+
{ }

src/EntityChange/ChangeRecord.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using System.Text.Json.Serialization;
2+
13
namespace EntityChange;
24

35
/// <summary>
@@ -11,6 +13,8 @@ public class ChangeRecord
1113
/// <value>
1214
/// The name of the property that was changed.
1315
/// </value>
16+
[JsonPropertyName("propertyName")]
17+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
1418
public string? PropertyName { get; set; }
1519

1620
/// <summary>
@@ -19,6 +23,8 @@ public class ChangeRecord
1923
/// <value>
2024
/// The display name for the changed property.
2125
/// </value>
26+
[JsonPropertyName("displayName")]
27+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
2228
public string? DisplayName { get; set; }
2329

2430
/// <summary>
@@ -27,6 +33,8 @@ public class ChangeRecord
2733
/// <value>
2834
/// The object graph change path.
2935
/// </value>
36+
[JsonPropertyName("path")]
37+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
3038
public string? Path { get; set; }
3139

3240
/// <summary>
@@ -35,6 +43,8 @@ public class ChangeRecord
3543
/// <value>
3644
/// The type of change operation.
3745
/// </value>
46+
[JsonPropertyName("operation")]
47+
[JsonConverter(typeof(JsonStringEnumConverter<ChangeOperation>))]
3848
public ChangeOperation Operation { get; set; }
3949

4050
/// <summary>
@@ -43,6 +53,8 @@ public class ChangeRecord
4353
/// <value>
4454
/// The original value.
4555
/// </value>
56+
[JsonPropertyName("originalValue")]
57+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
4658
public object? OriginalValue { get; set; }
4759

4860
/// <summary>
@@ -51,6 +63,8 @@ public class ChangeRecord
5163
/// <value>
5264
/// The current value.
5365
/// </value>
66+
[JsonPropertyName("currentValue")]
67+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
5468
public object? CurrentValue { get; set; }
5569

5670
/// <summary>
@@ -59,6 +73,8 @@ public class ChangeRecord
5973
/// <value>
6074
/// The original value formatted as a <see cref="string"/>.
6175
/// </value>
76+
[JsonPropertyName("originalFormatted")]
77+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
6278
public string? OriginalFormatted { get; set; }
6379

6480
/// <summary>
@@ -67,5 +83,7 @@ public class ChangeRecord
6783
/// <value>
6884
/// The current value formatted as a <see cref="string"/>.
6985
/// </value>
86+
[JsonPropertyName("currentFormatted")]
87+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
7088
public string? CurrentFormatted { get; set; }
7189
}

src/EntityChange/EntityChange.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
88
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
99
<PackageReference Include="System.ComponentModel.TypeConverter" Version="4.3.0" />
10+
<PackageReference Include="System.Text.Json" Version="9.0.3" />
1011
</ItemGroup>
1112

1213
</Project>

src/EntityChange/PathStack.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using System.Collections.Concurrent;
12
using System.IO;
3+
using System.Text.Json.Serialization;
24

35
using EntityChange.Extensions;
46

@@ -9,7 +11,7 @@ namespace EntityChange;
911
/// </summary>
1012
public class PathStack
1113
{
12-
private readonly Stack<PathValue> _pathStack = [];
14+
private readonly ConcurrentStack<PathValue> _pathStack = [];
1315

1416
/// <summary>
1517
/// Push a property name to the stack
@@ -52,10 +54,11 @@ public void Clear()
5254
/// <returns>The top path name</returns>
5355
public string CurrentName()
5456
{
55-
if (_pathStack.Count == 0)
57+
if (_pathStack.IsEmpty)
5658
return string.Empty;
5759

58-
var peeked = _pathStack.Peek();
60+
if (!_pathStack.TryPeek(out var peeked))
61+
return string.Empty;
5962

6063
// not an indexer, use as is
6164
if (peeked.Indexer != true)
@@ -95,7 +98,8 @@ public string CurrentProperty()
9598
/// <inheritdoc/>
9699
public override string ToString()
97100
{
98-
return ToPath([.. _pathStack]);
101+
var array = _pathStack.ToArray();
102+
return ToPath(array);
99103
}
100104

101105

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System.Collections.Generic;
2+
using System.Text.Json;
3+
using System.Threading.Tasks;
4+
5+
using VerifyXunit;
6+
7+
using Xunit;
8+
9+
namespace EntityChange.Tests;
10+
11+
public class SerializationTests
12+
{
13+
[Fact]
14+
public async Task SerializeTest()
15+
{
16+
List<ChangeRecord> changes =
17+
[
18+
new ChangeRecord
19+
{
20+
PropertyName = "Name",
21+
DisplayName = "Name",
22+
Path = "Name",
23+
Operation = ChangeOperation.Replace,
24+
OriginalValue = "Name 1",
25+
OriginalFormatted = "Name 1",
26+
CurrentValue = "Name 2",
27+
CurrentFormatted = "Name 2"
28+
},
29+
new ChangeRecord
30+
{
31+
PropertyName = "Description",
32+
DisplayName = "Description",
33+
Path = "Description",
34+
Operation = ChangeOperation.Replace,
35+
OriginalValue = "Test 1",
36+
OriginalFormatted = "Test 1",
37+
CurrentValue = "Test 2",
38+
CurrentFormatted = "Test 2"
39+
},
40+
];
41+
42+
var json = JsonSerializer.Serialize(changes);
43+
44+
await Verifier.Verify(json).UseDirectory("Snapshots");
45+
}
46+
47+
[Fact]
48+
public async Task SerializeNullsTest()
49+
{
50+
List<ChangeRecord> changes =
51+
[
52+
new ChangeRecord
53+
{
54+
PropertyName = "Name",
55+
DisplayName = "Name",
56+
Path = "Name",
57+
Operation = ChangeOperation.Replace,
58+
OriginalValue = "Name 1",
59+
OriginalFormatted = "Name 1",
60+
CurrentValue = null,
61+
CurrentFormatted = null
62+
},
63+
new ChangeRecord
64+
{
65+
PropertyName = "Description",
66+
DisplayName = "Description",
67+
Path = "Description",
68+
Operation = ChangeOperation.Replace,
69+
OriginalValue = null,
70+
OriginalFormatted = null,
71+
CurrentValue = "Test 2",
72+
CurrentFormatted = "Test 2"
73+
},
74+
];
75+
76+
var json = JsonSerializer.Serialize(changes);
77+
78+
await Verifier.Verify(json).UseDirectory("Snapshots");
79+
}
80+
81+
[Fact]
82+
public void DeserializeTest()
83+
{
84+
var json = "[{\"propertyName\":\"Description\",\"displayName\":\"Description\",\"path\":\"Description\",\"operation\":2,\"originalValue\":\"Case Management 15 Min\",\"currentValue\":\"Case Management - 15 Min\",\"originalFormatted\":\"Case Management 15 Min\",\"currentFormatted\":\"Case Management - 15 Min\"},{\"propertyName\":\"CustomerType\",\"displayName\":\"Customer Type\",\"path\":\"CustomerType\",\"operation\":2,\"originalValue\":null,\"currentValue\":\"060\",\"originalFormatted\":\"\",\"currentFormatted\":\"060\"}]";
85+
86+
var changes = JsonSerializer.Deserialize<List<ChangeRecord>>(json);
87+
88+
Assert.NotNull(changes);
89+
Assert.Equal(2, changes.Count);
90+
}
91+
92+
[Fact]
93+
public void DeserializeEnumStringTest()
94+
{
95+
var json = "[{\"propertyName\":\"Name\",\"displayName\":\"Name\",\"path\":\"Name\",\"operation\":\"Replace\",\"originalValue\":\"Name 1\",\"currentValue\":\"Name 2\",\"originalFormatted\":\"Name 1\",\"currentFormatted\":\"Name 2\"},{\"propertyName\":\"Description\",\"displayName\":\"Description\",\"path\":\"Description\",\"operation\":\"Replace\",\"originalValue\":\"Test 1\",\"currentValue\":\"Test 2\",\"originalFormatted\":\"Test 1\",\"currentFormatted\":\"Test 2\"}]";
96+
97+
var changes = JsonSerializer.Deserialize<List<ChangeRecord>>(json);
98+
99+
Assert.NotNull(changes);
100+
Assert.Equal(2, changes.Count);
101+
}
102+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"propertyName":"Name","displayName":"Name","path":"Name","operation":"Replace","originalValue":"Name 1","originalFormatted":"Name 1"},{"propertyName":"Description","displayName":"Description","path":"Description","operation":"Replace","currentValue":"Test 2","currentFormatted":"Test 2"}]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"propertyName":"Name","displayName":"Name","path":"Name","operation":"Replace","originalValue":"Name 1","currentValue":"Name 2","originalFormatted":"Name 1","currentFormatted":"Name 2"},{"propertyName":"Description","displayName":"Description","path":"Description","operation":"Replace","originalValue":"Test 1","currentValue":"Test 2","originalFormatted":"Test 1","currentFormatted":"Test 2"}]

0 commit comments

Comments
 (0)