Skip to content

Commit 43dff93

Browse files
committed
Add source generator tests for GuidIds
1 parent 12f7728 commit 43dff93

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,58 @@ public void CanDeserializeDictionaryKeys_WithSystemTextJsonProvider()
143143
}
144144
#endif
145145

146+
[Fact]
147+
public void CanSerializeToGuid_WithSystemTextJsonProvider_WithSourceGenerator()
148+
{
149+
var foo = GuidId1.New();
150+
151+
var serializedFoo = SystemTextJsonSerializer.Serialize(foo, SystemTextJsonSerializerContext.Custom.GuidId1);
152+
var serializedGuid = SystemTextJsonSerializer.Serialize(foo.Value);
153+
154+
Assert.Equal(serializedFoo, serializedGuid);
155+
}
156+
157+
[Fact]
158+
public void CanDeserializeFromGuid_WithSystemTextJsonProvider_WithSourceGenerator()
159+
{
160+
var value = Guid.NewGuid();
161+
var foo = new GuidId1(value);
162+
var serializedGuid = SystemTextJsonSerializer.Serialize(value, SystemTextJsonSerializerContext.Custom.GuidId1);
163+
164+
var deserializedFoo = SystemTextJsonSerializer.Deserialize<GuidId1>(serializedGuid, SystemTextJsonSerializerContext.Custom.GuidId1);
165+
166+
Assert.Equal(foo, deserializedFoo);
167+
}
168+
169+
#if NET6_0_OR_GREATER
170+
[Fact]
171+
public void CanDeserializeDictionaryKeys_WithSystemTextJsonProvider_WithSourceGenerator()
172+
{
173+
var value = new TypeWithDictionaryKeys()
174+
{
175+
Values = new()
176+
};
177+
var guid = new GuidId1(Guid.Parse("78104553-f1cd-41ec-bcb6-d3a8ff8d994d"));
178+
value.Values.Add(guid, "My Value");
179+
var serialized = SystemTextJsonSerializer.Serialize(value, SystemTextJsonSerializerContext.Web.TypeWithDictionaryKeys);
180+
181+
var expected = $$"""
182+
{
183+
"values": {
184+
"78104553-f1cd-41ec-bcb6-d3a8ff8d994d": "My Value"
185+
}
186+
}
187+
""";
188+
Assert.Equal(serialized, expected);
189+
190+
var deserialized = SystemTextJsonSerializer.Deserialize<TypeWithDictionaryKeys>(serialized, SystemTextJsonSerializerContext.Web.TypeWithDictionaryKeys);
191+
192+
Assert.NotNull(deserialized.Values);
193+
Assert.True(deserialized.Values.ContainsKey(guid));
194+
Assert.Equal("My Value", deserialized.Values[guid]);
195+
}
196+
#endif
197+
146198
[Fact]
147199
public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty()
148200
{
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#if NET6_0_OR_GREATER
2+
using System.Text.Json;
3+
using System.Text.Json.Serialization;
4+
using StronglyTypedIds.IntegrationTests.Types;
5+
6+
namespace StronglyTypedIds.IntegrationTests;
7+
8+
[JsonSerializable(typeof(GuidId1))]
9+
[JsonSerializable(typeof(ConvertersGuidId))]
10+
[JsonSerializable(typeof(ConvertersGuidId2))]
11+
[JsonSerializable(typeof(GuidIdTests.TypeWithDictionaryKeys))]
12+
internal partial class SystemTextJsonSerializerContext : JsonSerializerContext
13+
{
14+
internal static SystemTextJsonSerializerContext Custom
15+
=> new(new JsonSerializerOptions
16+
{
17+
Converters =
18+
{
19+
new GuidId1.GuidId1SystemTextJsonConverter(),
20+
new ConvertersGuidId.ConvertersGuidIdSystemTextJsonConverter(),
21+
new ConvertersGuidId2.ConvertersGuidId2SystemTextJsonConverter(),
22+
}
23+
});
24+
25+
internal static SystemTextJsonSerializerContext Web
26+
=> new(new JsonSerializerOptions()
27+
{
28+
WriteIndented = true,
29+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
30+
Converters =
31+
{
32+
new GuidId1.GuidId1SystemTextJsonConverter(),
33+
new ConvertersGuidId.ConvertersGuidIdSystemTextJsonConverter(),
34+
new ConvertersGuidId2.ConvertersGuidId2SystemTextJsonConverter(),
35+
}
36+
});
37+
}
38+
#endif

0 commit comments

Comments
 (0)