From 43dff93ad47e0bdf5a947f7d499b7318b3ab5894 Mon Sep 17 00:00:00 2001 From: Andrew Lock Date: Sat, 11 Nov 2023 21:09:05 +0000 Subject: [PATCH] Add source generator tests for GuidIds --- .../GuidIdTests.cs | 52 +++++++++++++++++++ .../SystemTextJsonSerializerContext.cs | 38 ++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 test/StronglyTypedIds.IntegrationTests/SystemTextJsonSerializerContext.cs diff --git a/test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs b/test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs index e9966328c..19dac47cf 100644 --- a/test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs +++ b/test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs @@ -143,6 +143,58 @@ public void CanDeserializeDictionaryKeys_WithSystemTextJsonProvider() } #endif + [Fact] + public void CanSerializeToGuid_WithSystemTextJsonProvider_WithSourceGenerator() + { + var foo = GuidId1.New(); + + var serializedFoo = SystemTextJsonSerializer.Serialize(foo, SystemTextJsonSerializerContext.Custom.GuidId1); + var serializedGuid = SystemTextJsonSerializer.Serialize(foo.Value); + + Assert.Equal(serializedFoo, serializedGuid); + } + + [Fact] + public void CanDeserializeFromGuid_WithSystemTextJsonProvider_WithSourceGenerator() + { + var value = Guid.NewGuid(); + var foo = new GuidId1(value); + var serializedGuid = SystemTextJsonSerializer.Serialize(value, SystemTextJsonSerializerContext.Custom.GuidId1); + + var deserializedFoo = SystemTextJsonSerializer.Deserialize(serializedGuid, SystemTextJsonSerializerContext.Custom.GuidId1); + + Assert.Equal(foo, deserializedFoo); + } + +#if NET6_0_OR_GREATER + [Fact] + public void CanDeserializeDictionaryKeys_WithSystemTextJsonProvider_WithSourceGenerator() + { + var value = new TypeWithDictionaryKeys() + { + Values = new() + }; + var guid = new GuidId1(Guid.Parse("78104553-f1cd-41ec-bcb6-d3a8ff8d994d")); + value.Values.Add(guid, "My Value"); + var serialized = SystemTextJsonSerializer.Serialize(value, SystemTextJsonSerializerContext.Web.TypeWithDictionaryKeys); + + var expected = $$""" + { + "values": { + "78104553-f1cd-41ec-bcb6-d3a8ff8d994d": "My Value" + } + } + """; + Assert.Equal(serialized, expected); + + var deserialized = SystemTextJsonSerializer.Deserialize(serialized, SystemTextJsonSerializerContext.Web.TypeWithDictionaryKeys); + + Assert.NotNull(deserialized.Values); + Assert.True(deserialized.Values.ContainsKey(guid)); + Assert.Equal("My Value", deserialized.Values[guid]); + } +#endif + [Fact] public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() { diff --git a/test/StronglyTypedIds.IntegrationTests/SystemTextJsonSerializerContext.cs b/test/StronglyTypedIds.IntegrationTests/SystemTextJsonSerializerContext.cs new file mode 100644 index 000000000..7a388704c --- /dev/null +++ b/test/StronglyTypedIds.IntegrationTests/SystemTextJsonSerializerContext.cs @@ -0,0 +1,38 @@ +#if NET6_0_OR_GREATER +using System.Text.Json; +using System.Text.Json.Serialization; +using StronglyTypedIds.IntegrationTests.Types; + +namespace StronglyTypedIds.IntegrationTests; + +[JsonSerializable(typeof(GuidId1))] +[JsonSerializable(typeof(ConvertersGuidId))] +[JsonSerializable(typeof(ConvertersGuidId2))] +[JsonSerializable(typeof(GuidIdTests.TypeWithDictionaryKeys))] +internal partial class SystemTextJsonSerializerContext : JsonSerializerContext +{ + internal static SystemTextJsonSerializerContext Custom + => new(new JsonSerializerOptions + { + Converters = + { + new GuidId1.GuidId1SystemTextJsonConverter(), + new ConvertersGuidId.ConvertersGuidIdSystemTextJsonConverter(), + new ConvertersGuidId2.ConvertersGuidId2SystemTextJsonConverter(), + } + }); + + internal static SystemTextJsonSerializerContext Web + => new(new JsonSerializerOptions() + { + WriteIndented = true, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + Converters = + { + new GuidId1.GuidId1SystemTextJsonConverter(), + new ConvertersGuidId.ConvertersGuidIdSystemTextJsonConverter(), + new ConvertersGuidId2.ConvertersGuidId2SystemTextJsonConverter(), + } + }); +} +#endif