From bdfb937e5a0755123b1b6ee089a051733fd91290 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Sat, 15 Aug 2026 14:02:34 +1000 Subject: [PATCH] Honor explicit DefaultValueHandling for bools bool members were unconditionally given DefaultValueHandling.Include at the property level, which overrides the global setting. So an explicit AddExtraSettings(_ => _.DefaultValueHandling = Ignore) could never drop false. Track whether an extra setting explicitly assigns DefaultValueHandling (using null as a sentinel, since the property is nullable) and skip the bool special case when it has. Default behavior is unchanged. Fixes #1660 --- docs/dates.md | 26 +++++----- docs/guids.md | 10 ++-- docs/mdsource/serializer-settings.source.md | 2 + docs/members-throw.md | 8 ++-- docs/named-tuples.md | 4 +- docs/obsolete-members.md | 6 +-- docs/scrubbers.md | 18 +++---- docs/serializer-settings.md | 48 ++++++++++--------- ...lFalseWithDefaultValueIgnore.verified.json | 3 ++ ...FalseWithDefaultValueInclude.verified.json | 4 ++ ...olFalseWithDefaultValueIgnore.verified.txt | 3 ++ ...lFalseWithDefaultValueInclude.verified.txt | 4 ++ .../Serialization/SerializationTests.cs | 24 ++++++++++ .../Serialization/CustomContractResolver.cs | 4 +- .../Serialization/SerializationSettings.cs | 28 ++++++++++- 15 files changed, 130 insertions(+), 62 deletions(-) create mode 100644 src/StrictJsonTests/SerializationTests.BoolFalseWithDefaultValueIgnore.verified.json create mode 100644 src/StrictJsonTests/SerializationTests.BoolFalseWithDefaultValueInclude.verified.json create mode 100644 src/Verify.Tests/Serialization/SerializationTests.BoolFalseWithDefaultValueIgnore.verified.txt create mode 100644 src/Verify.Tests/Serialization/SerializationTests.BoolFalseWithDefaultValueInclude.verified.txt diff --git a/docs/dates.md b/docs/dates.md index de4b122203..ef81975f0e 100644 --- a/docs/dates.md +++ b/docs/dates.md @@ -29,7 +29,7 @@ var target = new DateTimeTarget await Verify(target); ``` -snippet source | anchor +snippet source | anchor Results in the following: @@ -70,7 +70,7 @@ settings.DontScrubDateTimes(); return Verify(target, settings); ``` -snippet source | anchor +snippet source | anchor @@ -87,7 +87,7 @@ var target = new return Verify(target) .DontScrubDateTimes(); ``` -snippet source | anchor +snippet source | anchor @@ -100,7 +100,7 @@ return Verify(target) public static void ModuleInitializer() => VerifierSettings.DontScrubDateTimes(); ``` -snippet source | anchor +snippet source | anchor @@ -124,7 +124,7 @@ settings.DisableDateCounting(); return Verify(target, settings); ``` -snippet source | anchor +snippet source | anchor @@ -141,7 +141,7 @@ var target = new return Verify(target) .DisableDateCounting(); ``` -snippet source | anchor +snippet source | anchor @@ -154,7 +154,7 @@ return Verify(target) public static void ModuleInitializer() => VerifierSettings.DisableDateCounting(); ``` -snippet source | anchor +snippet source | anchor @@ -201,7 +201,7 @@ public Task ScrubInlineDateTimesInstance() settings); } ``` -snippet source | anchor +snippet source | anchor @@ -215,7 +215,7 @@ public Task ScrubInlineDateTimesFluent() => Verify("content 2020-10-20 content") .ScrubInlineDateTimes("yyyy-MM-dd"); ``` -snippet source | anchor +snippet source | anchor @@ -231,7 +231,7 @@ public static class ModuleInitializer VerifierSettings.ScrubInlineDateTimes("yyyy-MM-dd"); } ``` -snippet source | anchor +snippet source | anchor @@ -252,7 +252,7 @@ settings.AddNamedDateTime(new(2030, 1, 2), "instanceNamedDateTime"); settings.AddNamedDateTimeOffset(new DateTime(2030, 1, 2), "instanceNamedTimeOffset"); await Verify(target, settings); ``` -snippet source | anchor +snippet source | anchor @@ -267,7 +267,7 @@ await Verify(target) .AddNamedDateTime(new(2030, 1, 2), "instanceNamedDateTime") .AddNamedDateTimeOffset(new DateTime(2030, 1, 2), "instanceNamedTimeOffset"); ``` -snippet source | anchor +snippet source | anchor @@ -285,7 +285,7 @@ public static void NamedDatesAndTimesGlobal() VerifierSettings.AddNamedDateTimeOffset(new(new(2030, 1, 1)), "namedDateTimeOffset"); } ``` -snippet source | anchor +snippet source | anchor diff --git a/docs/guids.md b/docs/guids.md index 973e108467..69e9f778f4 100644 --- a/docs/guids.md +++ b/docs/guids.md @@ -23,7 +23,7 @@ var target = new GuidTarget await Verify(target); ``` -snippet source | anchor +snippet source | anchor Results in the following: @@ -79,7 +79,7 @@ await Verify(target) ```cs VerifierSettings.DontScrubGuids(); ``` -snippet source | anchor +snippet source | anchor @@ -105,7 +105,7 @@ public Task ScrubInlineGuidsInstance() settings); } ``` -snippet source | anchor +snippet source | anchor @@ -119,7 +119,7 @@ public Task ScrubInlineGuidsFluent() => Verify("content 651ad409-fc30-4b12-a47e-616d3f953e4c content") .ScrubInlineGuids(); ``` -snippet source | anchor +snippet source | anchor @@ -135,7 +135,7 @@ public static class ModuleInitializer VerifierSettings.ScrubInlineGuids(); } ``` -snippet source | anchor +snippet source | anchor diff --git a/docs/mdsource/serializer-settings.source.md b/docs/mdsource/serializer-settings.source.md index 764aaf4f18..acc4f5d868 100644 --- a/docs/mdsource/serializer-settings.source.md +++ b/docs/mdsource/serializer-settings.source.md @@ -68,6 +68,8 @@ The default `JsonSerializerSettings` are: snippet: defaultSerialization +`DefaultValueHandling` is `Ignore`, but bool members are an exception, so `false` is still rendered. Explicitly assigning `DefaultValueHandling` (using any of the below approaches) disables that exception, and `false` is then treated as any other default value. + ### Modify Defaults diff --git a/docs/members-throw.md b/docs/members-throw.md index eb2059d95d..e304557017 100644 --- a/docs/members-throw.md +++ b/docs/members-throw.md @@ -35,7 +35,7 @@ public Task CustomExceptionPropFluent() .IgnoreMembersThatThrow(); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -45,7 +45,7 @@ Or globally: ```cs VerifierSettings.IgnoreMembersThatThrow(); ``` -snippet source | anchor +snippet source | anchor Result: @@ -82,7 +82,7 @@ public Task ExceptionMessagePropFluent() .IgnoreMembersThatThrow(_ => _.Message == "Ignore"); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -92,7 +92,7 @@ Or globally: ```cs VerifierSettings.IgnoreMembersThatThrow(_ => _.Message == "Ignore"); ``` -snippet source | anchor +snippet source | anchor Result: diff --git a/docs/named-tuples.md b/docs/named-tuples.md index 837c7b9b15..7a0dc56136 100644 --- a/docs/named-tuples.md +++ b/docs/named-tuples.md @@ -19,7 +19,7 @@ Given a method that returns a named tuple: static (bool Member1, string Member2, string Member3) MethodWithNamedTuple() => (true, "A", "B"); ``` -snippet source | anchor +snippet source | anchor Can be verified: @@ -29,7 +29,7 @@ Can be verified: ```cs await VerifyTuple(() => MethodWithNamedTuple()); ``` -snippet source | anchor +snippet source | anchor Resulting in: diff --git a/docs/obsolete-members.md b/docs/obsolete-members.md index 930d59c109..0a3b68bc87 100644 --- a/docs/obsolete-members.md +++ b/docs/obsolete-members.md @@ -31,7 +31,7 @@ public Task WithObsoleteProp() return Verify(target); } ``` -snippet source | anchor +snippet source | anchor Result: @@ -79,7 +79,7 @@ public Task WithObsoletePropIncludedFluent() .IncludeObsoletes(); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -89,7 +89,7 @@ Or globally: ```cs VerifierSettings.IncludeObsoletes(); ``` -snippet source | anchor +snippet source | anchor Result: diff --git a/docs/scrubbers.md b/docs/scrubbers.md index 606ea92f7e..e4f94b52a5 100644 --- a/docs/scrubbers.md +++ b/docs/scrubbers.md @@ -44,7 +44,7 @@ verifySettings.ScrubWindow( return null; }); ``` -snippet source | anchor +snippet source | anchor `ScrubLines` and `ScrubLinesWithReplace` also accept span based delegates (`LineMatch` / `LineReplace`) that avoid allocating a string per line. Use an explicitly typed lambda parameter to select them, e.g. `ScrubLines((ReadOnlySpan line) => ...)`; untyped lambdas bind the string overloads. @@ -69,7 +69,7 @@ Instead of being executed by the engine, `AddScrubber(Action)` mu ```cs verifySettings.AddScrubber(_ => _.Remove(0, 100)); ``` -snippet source | anchor +snippet source | anchor Since these run after every engine scrubber, they can also modify text that the engine has already replaced. @@ -118,7 +118,7 @@ For example remove lines containing `text`: ```cs verifySettings.ScrubLines(line => line.Contains("text")); ``` -snippet source | anchor +snippet source | anchor @@ -133,7 +133,7 @@ For example remove lines containing `text1` or `text2` ```cs verifySettings.ScrubLinesContaining("text1", "text2"); ``` -snippet source | anchor +snippet source | anchor Case insensitive by default (`StringComparison.OrdinalIgnoreCase`). @@ -145,7 +145,7 @@ Case insensitive by default (`StringComparison.OrdinalIgnoreCase`). ```cs verifySettings.ScrubLinesContaining(StringComparison.Ordinal, "text1", "text2"); ``` -snippet source | anchor +snippet source | anchor @@ -160,7 +160,7 @@ For example converts lines to upper case: ```cs verifySettings.ScrubLinesWithReplace(line => line.ToUpper()); ``` -snippet source | anchor +snippet source | anchor @@ -173,7 +173,7 @@ Replaces `Environment.MachineName` with `TheMachineName`. ```cs verifySettings.ScrubMachineName(); ``` -snippet source | anchor +snippet source | anchor @@ -186,7 +186,7 @@ Replaces `Environment.UserName` with `TheUserName`. ```cs verifySettings.ScrubUserName(); ``` -snippet source | anchor +snippet source | anchor @@ -788,7 +788,7 @@ Scrubbers can be scoped to verified files with a matching extension by passing t ```cs verifySettings.ScrubReplace("json", "abc", "xyz"); ``` -snippet source | anchor +snippet source | anchor A scrubber registered this way runs only for verified files with that extension, while a scrubber registered without an extension runs for all of them. Extension scoping is available at every [level](#Scrubber-levels), and for the legacy `AddScrubber(Action)` overloads. diff --git a/docs/serializer-settings.md b/docs/serializer-settings.md index 02205d9ee4..c8ae8faeb0 100644 --- a/docs/serializer-settings.md +++ b/docs/serializer-settings.md @@ -135,6 +135,8 @@ var settings = new JsonSerializerSettings snippet source | anchor +`DefaultValueHandling` is `Ignore`, but bool members are an exception, so `false` is still rendered. Explicitly assigning `DefaultValueHandling` (using any of the below approaches) disables that exception, and `false` is then treated as any other default value. + ### Modify Defaults @@ -204,7 +206,7 @@ To disable this behavior globally use: ```cs VerifierSettings.DontIgnoreEmptyCollections(); ``` -snippet source | anchor +snippet source | anchor @@ -495,7 +497,7 @@ public Task ScopedSerializerFluent() .AddExtraSettings(_ => _.TypeNameHandling = TypeNameHandling.All); } ``` -snippet source | anchor +snippet source | anchor Result: @@ -623,7 +625,7 @@ public Task IgnoreTypeFluent() .IgnoreMembersWithType(); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -633,7 +635,7 @@ Or globally: ```cs VerifierSettings.IgnoreMembersWithType(); ``` -snippet source | anchor +snippet source | anchor Result: @@ -770,7 +772,7 @@ public Task ScrubTypeFluent() .ScrubMembersWithType(); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -780,7 +782,7 @@ Or globally: ```cs VerifierSettings.ScrubMembersWithType(); ``` -snippet source | anchor +snippet source | anchor Result: @@ -859,7 +861,7 @@ public Task AddIgnoreInstanceFluent() .IgnoreInstance(_ => _.Property == "Ignore"); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -869,7 +871,7 @@ Or globally: ```cs VerifierSettings.IgnoreInstance(_ => _.Property == "Ignore"); ``` -snippet source | anchor +snippet source | anchor Result: @@ -931,7 +933,7 @@ public Task AddScrubInstanceFluent() .ScrubInstance(_ => _.Property == "Ignore"); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -941,7 +943,7 @@ Or globally: ```cs VerifierSettings.ScrubInstance(_ => _.Property == "Ignore"); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1004,7 +1006,7 @@ public Task IgnoreMemberByExpressionFluent() _ => _.PropertyThatThrows); } ``` -snippet source | anchor +snippet source | anchor Or globally @@ -1019,7 +1021,7 @@ VerifierSettings.IgnoreMembers( _ => _.GetOnlyProperty, _ => _.PropertyThatThrows); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1079,7 +1081,7 @@ public Task ScrubMemberByExpressionFluent() _ => _.PropertyThatThrows); } ``` -snippet source | anchor +snippet source | anchor Or globally @@ -1094,7 +1096,7 @@ VerifierSettings.ScrubMembers( _ => _.GetOnlyProperty, _ => _.PropertyThatThrows); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1173,7 +1175,7 @@ public Task IgnoreMemberByNameFluent() .IgnoreMember(_ => _.PropertyThatThrows); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -1193,7 +1195,7 @@ VerifierSettings.IgnoreMember("Field"); // For a specific type with expression VerifierSettings.IgnoreMember(_ => _.PropertyThatThrows); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1268,7 +1270,7 @@ public Task ScrubMemberByNameFluent() .ScrubMember(_ => _.PropertyThatThrows); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -1288,7 +1290,7 @@ VerifierSettings.ScrubMember("Field"); // For a specific type with expression VerifierSettings.ScrubMember(_ => _.PropertyThatThrows); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1381,7 +1383,7 @@ public Task IgnoreDictionaryByPredicate() return Verify(target, settings); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -1393,7 +1395,7 @@ VerifierSettings.IgnoreMembers( _=>_.DeclaringType == typeof(TargetClass) && _.Name == "Proprty"); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1482,7 +1484,7 @@ public Task ScrubDictionaryByPredicate() return Verify(target, settings); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -1494,7 +1496,7 @@ VerifierSettings.ScrubMembers( _=>_.DeclaringType == typeof(TargetClass) && _.Name == "Proprty"); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1548,7 +1550,7 @@ public Task MemberConverterByExpression() return Verify(input); } ``` -snippet source | anchor +snippet source | anchor diff --git a/src/StrictJsonTests/SerializationTests.BoolFalseWithDefaultValueIgnore.verified.json b/src/StrictJsonTests/SerializationTests.BoolFalseWithDefaultValueIgnore.verified.json new file mode 100644 index 0000000000..c0ce8db021 --- /dev/null +++ b/src/StrictJsonTests/SerializationTests.BoolFalseWithDefaultValueIgnore.verified.json @@ -0,0 +1,3 @@ +{ + "NullableBoolMember": false +} \ No newline at end of file diff --git a/src/StrictJsonTests/SerializationTests.BoolFalseWithDefaultValueInclude.verified.json b/src/StrictJsonTests/SerializationTests.BoolFalseWithDefaultValueInclude.verified.json new file mode 100644 index 0000000000..fac0118a04 --- /dev/null +++ b/src/StrictJsonTests/SerializationTests.BoolFalseWithDefaultValueInclude.verified.json @@ -0,0 +1,4 @@ +{ + "BoolMember": false, + "NullableBoolMember": null +} \ No newline at end of file diff --git a/src/Verify.Tests/Serialization/SerializationTests.BoolFalseWithDefaultValueIgnore.verified.txt b/src/Verify.Tests/Serialization/SerializationTests.BoolFalseWithDefaultValueIgnore.verified.txt new file mode 100644 index 0000000000..d6f36479d5 --- /dev/null +++ b/src/Verify.Tests/Serialization/SerializationTests.BoolFalseWithDefaultValueIgnore.verified.txt @@ -0,0 +1,3 @@ +{ + NullableBoolMember: false +} \ No newline at end of file diff --git a/src/Verify.Tests/Serialization/SerializationTests.BoolFalseWithDefaultValueInclude.verified.txt b/src/Verify.Tests/Serialization/SerializationTests.BoolFalseWithDefaultValueInclude.verified.txt new file mode 100644 index 0000000000..f114133d58 --- /dev/null +++ b/src/Verify.Tests/Serialization/SerializationTests.BoolFalseWithDefaultValueInclude.verified.txt @@ -0,0 +1,4 @@ +{ + BoolMember: false, + NullableBoolMember: null +} \ No newline at end of file diff --git a/src/Verify.Tests/Serialization/SerializationTests.cs b/src/Verify.Tests/Serialization/SerializationTests.cs index 2a695e6c0c..4d6a5c1cc0 100644 --- a/src/Verify.Tests/Serialization/SerializationTests.cs +++ b/src/Verify.Tests/Serialization/SerializationTests.cs @@ -1187,6 +1187,30 @@ public Task BoolTrue() return Verify(target); } + [Fact] + public Task BoolFalseWithDefaultValueIgnore() + { + var target = new BoolModel + { + BoolMember = false, + NullableBoolMember = false + }; + return Verify(target) + .AddExtraSettings(_ => _.DefaultValueHandling = DefaultValueHandling.Ignore); + } + + [Fact] + public Task BoolFalseWithDefaultValueInclude() + { + var target = new BoolModel + { + BoolMember = false, + NullableBoolMember = null + }; + return Verify(target) + .AddExtraSettings(_ => _.DefaultValueHandling = DefaultValueHandling.Include); + } + class BoolModel { public bool BoolMember; diff --git a/src/Verify/Serialization/CustomContractResolver.cs b/src/Verify/Serialization/CustomContractResolver.cs index 7d2a13d036..d90fc02b67 100644 --- a/src/Verify/Serialization/CustomContractResolver.cs +++ b/src/Verify/Serialization/CustomContractResolver.cs @@ -105,7 +105,9 @@ protected override JsonProperty CreateProperty(MemberInfo member, MemberSerializ property.TypeNameHandling = TypeNameHandling.All; } - if (property.PropertyType == typeof(bool)) + // bools are always included, unless DefaultValueHandling has been explicitly configured + if (property.PropertyType == typeof(bool) && + !settings.DefaultValueHandlingExplicit) { property.DefaultValueHandling = DefaultValueHandling.Include; } diff --git a/src/Verify/Serialization/SerializationSettings.cs b/src/Verify/Serialization/SerializationSettings.cs index ec93323242..866f4e6e7b 100644 --- a/src/Verify/Serialization/SerializationSettings.cs +++ b/src/Verify/Serialization/SerializationSettings.cs @@ -134,16 +134,40 @@ JsonSerializerSettings BuildSettings() converters.Add(combinationResultsConverter); foreach (var extraSetting in extraSettings) { - extraSetting(settings); + ApplyExtraSetting(settings, extraSetting); } return settings; } + /// + /// True if an extra setting has explicitly assigned . + /// Used to know when to stop always including bools. + /// + internal bool DefaultValueHandlingExplicit { get; private set; } + + void ApplyExtraSetting(JsonSerializerSettings settings, Action action) + { + // DefaultValueHandling is nullable, so null is used as a sentinel to detect an explicit assignment + var current = settings.DefaultValueHandling; + settings.DefaultValueHandling = null; + + action(settings); + + if (settings.DefaultValueHandling is null) + { + settings.DefaultValueHandling = current; + } + else + { + DefaultValueHandlingExplicit = true; + } + } + public void AddExtraSettings(Action action) { extraSettings.Add(action); - action(jsonSettings); + ApplyExtraSetting(jsonSettings, action); serializer = null; }