Skip to content

Commit 8498584

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

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,56 @@ public void CanDeserializeDictionaryKeys_WithSystemTextJsonProvider()
141141
Assert.True(deserialized.Values.ContainsKey(guid));
142142
Assert.Equal("My Value", deserialized.Values[guid]);
143143
}
144+
145+
[Fact]
146+
public void CanSerializeToGuid_WithSystemTextJsonProvider_WithSourceGenerator()
147+
{
148+
var foo = GuidId1.New();
149+
150+
var serializedFoo = SystemTextJsonSerializer.Serialize(foo, SystemTextJsonSerializerContext.Custom.GuidId1);
151+
var serializedGuid = SystemTextJsonSerializer.Serialize(foo.Value);
152+
153+
Assert.Equal(serializedFoo, serializedGuid);
154+
}
155+
156+
[Fact]
157+
public void CanDeserializeFromGuid_WithSystemTextJsonProvider_WithSourceGenerator()
158+
{
159+
var value = Guid.NewGuid();
160+
var foo = new GuidId1(value);
161+
var serializedGuid = SystemTextJsonSerializer.Serialize(value);
162+
163+
var deserializedFoo = SystemTextJsonSerializer.Deserialize<GuidId1>(serializedGuid, SystemTextJsonSerializerContext.Custom.GuidId1);
164+
165+
Assert.Equal(foo, deserializedFoo);
166+
}
167+
168+
[Fact(Skip = "This one doesn't seem to work, but I'm not sure if it's a source-generator limitation or a bug...")]
169+
public void CanDeserializeDictionaryKeys_WithSystemTextJsonProvider_WithSourceGenerator()
170+
{
171+
var value = new TypeWithDictionaryKeys()
172+
{
173+
Values = new()
174+
};
175+
var guid = new GuidId1(Guid.Parse("78104553-f1cd-41ec-bcb6-d3a8ff8d994d"));
176+
value.Values.Add(guid, "My Value");
177+
var serialized = SystemTextJsonSerializer.Serialize(value, SystemTextJsonSerializerContext.Web.TypeWithDictionaryKeys);
178+
179+
var expected = $$"""
180+
{
181+
"values": {
182+
"78104553-f1cd-41ec-bcb6-d3a8ff8d994d": "My Value"
183+
}
184+
}
185+
""";
186+
Assert.Equal(serialized, expected);
187+
188+
var deserialized = SystemTextJsonSerializer.Deserialize<TypeWithDictionaryKeys>(serialized, SystemTextJsonSerializerContext.Web.TypeWithDictionaryKeys);
189+
190+
Assert.NotNull(deserialized.Values);
191+
Assert.True(deserialized.Values.ContainsKey(guid));
192+
Assert.Equal("My Value", deserialized.Values[guid]);
193+
}
144194
#endif
145195

146196
[Fact]
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)