From ee5c684da90e367ee5ab84f2019cac2d8b583e28 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Tue, 2 Jul 2024 10:55:23 +1000 Subject: [PATCH] relax WriteMember type constraint --- src/Directory.Build.props | 2 +- ...s.WithConverterIgnoreDefault.verified.json | 3 ++- ...ts.WithConverterIgnoreDefault.verified.txt | 3 ++- .../Serialization/SerializationTests.cs | 20 +++++++++++++------ src/Verify/Serialization/VerifyJsonWriter.cs | 3 +-- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/Directory.Build.props b/src/Directory.Build.props index f7c9d3cfa0..f3ae9e08ed 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,7 +2,7 @@ CS1591;CS0649;xUnit1026;xUnit1013;CS1573;VerifyTestsProjectDir;VerifySetParameters;PolyFillTargetsForNuget - 25.1.0 + 25.1.1 enable preview 1.0.0 diff --git a/src/StrictJsonTests/SerializationTests.WithConverterIgnoreDefault.verified.json b/src/StrictJsonTests/SerializationTests.WithConverterIgnoreDefault.verified.json index 4623aaf854..1c07eea4ec 100644 --- a/src/StrictJsonTests/SerializationTests.WithConverterIgnoreDefault.verified.json +++ b/src/StrictJsonTests/SerializationTests.WithConverterIgnoreDefault.verified.json @@ -1,6 +1,7 @@ { "withDefault": {}, "withNonDefault": { - "Name": "Value" + "Name": "Value", + "DateTimeKind": "Local" } } \ No newline at end of file diff --git a/src/Verify.Tests/Serialization/SerializationTests.WithConverterIgnoreDefault.verified.txt b/src/Verify.Tests/Serialization/SerializationTests.WithConverterIgnoreDefault.verified.txt index 8dd7a5f763..72729ce61a 100644 --- a/src/Verify.Tests/Serialization/SerializationTests.WithConverterIgnoreDefault.verified.txt +++ b/src/Verify.Tests/Serialization/SerializationTests.WithConverterIgnoreDefault.verified.txt @@ -1,6 +1,7 @@ { withDefault: {}, withNonDefault: { - Name: Value + Name: Value, + DateTimeKind: Local } } \ No newline at end of file diff --git a/src/Verify.Tests/Serialization/SerializationTests.cs b/src/Verify.Tests/Serialization/SerializationTests.cs index 11be6001a2..16203b7451 100644 --- a/src/Verify.Tests/Serialization/SerializationTests.cs +++ b/src/Verify.Tests/Serialization/SerializationTests.cs @@ -3756,27 +3756,35 @@ public Task WithConverterIgnoreDefault() => Verify( new { - withDefault = new ConverterTarget + withDefault = new ConverterIgnoreDefaultTarget { - Name = "Ignore" + Name = "Ignore", + DateTimeKind = DateTimeKind.Utc }, - withNonDefault = new ConverterTarget + withNonDefault = new ConverterIgnoreDefaultTarget { - Name = "Value" + Name = "Value", + DateTimeKind = DateTimeKind.Local }, }) .AddExtraSettings(_ => _.Converters.Add(new ConverterIgnoreDefault())); class ConverterIgnoreDefault : - WriteOnlyJsonConverter + WriteOnlyJsonConverter { - public override void Write(VerifyJsonWriter writer, ConverterTarget target) + public override void Write(VerifyJsonWriter writer, ConverterIgnoreDefaultTarget target) { writer.WriteStartObject(); writer.WriteMember(target, target.Name, "Name", "Ignore"); + writer.WriteMember(target, target.DateTimeKind, "DateTimeKind", DateTimeKind.Utc); writer.WriteEnd(); } } + class ConverterIgnoreDefaultTarget + { + public string Name { get; set; } = null!; + public DateTimeKind DateTimeKind { get; set; } + } [Fact] public Task WithRecursiveConverter() => diff --git a/src/Verify/Serialization/VerifyJsonWriter.cs b/src/Verify/Serialization/VerifyJsonWriter.cs index 92b4a01186..52bea7c9cf 100644 --- a/src/Verify/Serialization/VerifyJsonWriter.cs +++ b/src/Verify/Serialization/VerifyJsonWriter.cs @@ -189,14 +189,13 @@ public override void WriteValue(Guid value) /// Writes a property name and value while respecting other custom serialization settings. /// public void WriteMember(object target, T? value, string name, T defaultIgnore) - where T : IEquatable { if (value is null) { return; } - if (value.Equals(defaultIgnore)) + if (EqualityComparer.Default.Equals(value, defaultIgnore)) { return; }