Skip to content

Commit

Permalink
relax WriteMember type constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Jul 2, 2024
1 parent 125d23f commit ee5c684
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591;CS0649;xUnit1026;xUnit1013;CS1573;VerifyTestsProjectDir;VerifySetParameters;PolyFillTargetsForNuget</NoWarn>
<Version>25.1.0</Version>
<Version>25.1.1</Version>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>preview</LangVersion>
<AssemblyVersion>1.0.0</AssemblyVersion>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"withDefault": {},
"withNonDefault": {
"Name": "Value"
"Name": "Value",
"DateTimeKind": "Local"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
withDefault: {},
withNonDefault: {
Name: Value
Name: Value,
DateTimeKind: Local
}
}
20 changes: 14 additions & 6 deletions src/Verify.Tests/Serialization/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConverterTarget>
WriteOnlyJsonConverter<ConverterIgnoreDefaultTarget>
{
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() =>
Expand Down
3 changes: 1 addition & 2 deletions src/Verify/Serialization/VerifyJsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,13 @@ public override void WriteValue(Guid value)
/// Writes a property name and value while respecting other custom serialization settings.
/// </summary>
public void WriteMember<T>(object target, T? value, string name, T defaultIgnore)
where T : IEquatable<T?>
{
if (value is null)
{
return;
}

if (value.Equals(defaultIgnore))
if (EqualityComparer<T>.Default.Equals(value, defaultIgnore))
{
return;
}
Expand Down

0 comments on commit ee5c684

Please sign in to comment.