Support type-level JsonIgnoreCondition via [JsonIgnore] on classes, structs, and interfaces#124646
Conversation
|
Tagging subscribers to this area: @dotnet/area-system-text-json |
…n resolver, source generator) Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
…on value types Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
…dd inheritance and deserialization tests Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs
Outdated
Show resolved
Hide resolved
…YSLIB1226 diagnostic Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
…tion Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
|
@copilot Please rebase changes and fix merge conflicts. |
|
@copilot Please rebase changes and fix merge conflicts. |
…econdition-type-level
Merged latest main and resolved the conflict in |
eiriktsarpalis
left a comment
There was a problem hiding this comment.
@copilot add testing showing that the attribute annotation overrides the global JSO setting.
There was a problem hiding this comment.
Pull request overview
Adds support for applying [JsonIgnore(Condition = ...)] at the type level to provide a default JsonIgnoreCondition for all members, with consistent behavior across reflection-based metadata and the System.Text.Json source generator.
Changes:
- Expanded
JsonIgnoreAttributeto allow usage onclass,struct, andinterface, with API compat/package validation suppressions. - Implemented type-level
JsonIgnoreConditionfallback in both reflection resolver and source generator, including invalid-configuration handling forAlways. - Added new source-generator warning
SYSLIB1226and comprehensive tests for the new behavior.
Reviewed changes
Copilot reviewed 28 out of 28 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/apicompat/ApiCompatBaseline.NetCoreAppLatestStable.xml | Suppresses API compat diagnostic for the AttributeUsage change. |
| src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Attributes/JsonIgnoreAttribute.cs | Extends attribute targets and updates XML docs to describe type-level behavior. |
| src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/DefaultJsonTypeInfoResolver.Helpers.cs | Adds reflection-path fallback to type-level ignore condition and rejects Always. |
| src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs | Adds generator support for type-level ignore condition fallback + emits SYSLIB1226 for invalid Always. |
| src/libraries/System.Text.Json/gen/JsonSourceGenerator.DiagnosticDescriptors.cs | Introduces diagnostic descriptor for SYSLIB1226. |
| src/libraries/System.Text.Json/gen/Helpers/KnownTypeSymbols.cs | Adds cached symbol lookup for JsonIgnoreAttribute. |
| src/libraries/System.Text.Json/gen/Resources/Strings.resx | Adds resource strings for SYSLIB1226 title/message. |
| src/libraries/System.Text.Json/gen/Resources/xlf/Strings.*.xlf | Adds localization entries for the new diagnostic strings. |
| docs/project/list-of-diagnostics.md | Documents SYSLIB1226 meaning in the diagnostics list. |
| src/libraries/System.Text.Json/ref/System.Text.Json.cs | Updates reference assembly metadata for new attribute usage targets. |
| src/libraries/System.Text.Json/src/CompatibilitySuppressions.xml | Adds package validation baseline suppressions for the AttributeUsage change across target TFMs. |
| src/libraries/System.Text.Json/tests/Common/PropertyVisibilityTests.cs | Adds reflection-path tests for type-level ignore semantics, overrides, structs, and inheritance. |
| src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/Serialization/PropertyVisibilityTests.cs | Adds source-gen-path test ensuring type-level Always is ignored (serialized normally). |
| src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/System.Text.Json.SourceGeneration.Tests.targets | Suppresses SYSLIB1226 in source-gen integration tests (covered explicitly elsewhere). |
| src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/CompilationHelper.cs | Adds compilation helper for SYSLIB1226 unit-test scenario. |
| src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/JsonSourceGeneratorDiagnosticsTests.cs | Adds unit test validating SYSLIB1226 warning and location. |
You can also share your feedback on Copilot code review. Take the survey.
...ests/System.Text.Json.SourceGeneration.Tests/System.Text.Json.SourceGeneration.Tests.targets
Show resolved
Hide resolved
...Text.Json/src/System/Text/Json/Serialization/Metadata/DefaultJsonTypeInfoResolver.Helpers.cs
Outdated
Show resolved
Hide resolved
…faultIgnoreCondition Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
Added |
tarekgh
left a comment
There was a problem hiding this comment.
LGTM. I added one comment on issue originally raised by copilot if you want to consider it.
… per-property methods Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
|
/ba-g unrelated mono build failures. |
Description
[JsonIgnore(Condition = ...)]can now be applied at the type level to set a defaultJsonIgnoreConditionfor all properties and fields, eliminating per-property duplication:Property-level
[JsonIgnore]overrides the type-level setting. The type-level annotation also overrides the globalJsonSerializerOptions.DefaultIgnoreCondition— for example, a type-levelWhenWritingNulltakes precedence over a globalWhenWritingDefault. For non-nullable value types whereWhenWritingNullis inapplicable, the ignore condition is set toNeverto ensure the type-level annotation still overrides the global JSO policy.JsonIgnoreCondition.Alwaysis rejected at the type level as invalid configuration — the reflection path throwsInvalidOperationException, and the source generator emitsSYSLIB1226warning diagnostic and ignores the attribute.Changes
JsonIgnoreAttributeAttributeUsageto includeClass | Struct | Interface, with API compat suppressions for theAttributeUsagechange in both the shared framework baseline (ApiCompatBaseline.NetCoreAppLatestStable.xml) and the package-level baseline (CompatibilitySuppressions.xmlfornet10.0,net462,netstandard2.0)DefaultJsonTypeInfoResolver.Helpers.cs): Resolves type-level[JsonIgnore]once per type inPopulatePropertiesand passes the resolvedJsonIgnoreCondition?parametrically throughAddMembersDeclaredBySuperType→AddMember→CreatePropertyInfo, avoiding redundant per-member attribute lookups. UsestypeInfo.Type(root serialized type) so type-level attributes on derived classes apply to inherited base-class properties. ThrowsInvalidOperationExceptionforJsonIgnoreCondition.Alwaysat the type level. ForWhenWritingNullon non-nullable value types, sets the ignore condition toNeverto prevent the globalDefaultIgnoreConditionfrom taking over.JsonSourceGenerator.Parser.cs): Reads type-level[JsonIgnore]inProcessTypeCustomAttributesusing cachedJsonIgnoreAttributeTypesymbol fromKnownTypeSymbols.cs, threads it through to property parsing as a fallback. EmitsSYSLIB1226diagnostic forJsonIgnoreCondition.Alwaysat the type level. Applies the sameWhenWritingNull→Nevermapping for non-nullable value types.SYSLIB1226diagnostic descriptor, resource strings (including XLF translations), and updatedlist-of-diagnostics.mdWhenWritingNull,WhenWritingDefault,Always(invalid config), property-level override, struct, inheritance, deserialization, and type-level annotation overriding globalJsonSerializerOptions.DefaultIgnoreConditionscenarios across both reflection and source gen paths. Includes source generator unit test for theSYSLIB1226diagnostic.Original prompt
JsonIgnoreCondition's on the type level #108231💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.