Repository navigation
[release/11.0] Validate custom union type classifier results before deserialization - #137079
Merged
Merged
Conversation
…136226) <!-- --> `JsonUnionConverter<TUnion>` currently resolves metadata and invokes the converter for any non-null type returned by a custom classifier. Unlike polymorphic deserialization, it does not check the result against the configured case types before dispatch. Cache the final `UnionCases` in a `HashSet<Type>` for classifier-backed unions and throw `JsonException` for undeclared results before resolving their metadata. Membership is exact: an undeclared subtype is rejected even when assignable to a declared case. Default classification and JSON null handling are unchanged. The shared regression tests cover invalid and undeclared results, undeclared subtypes, cases added or removed through contract customization, and declared nullable cases. The rejection regressions were reproduced before the fix. All 154 new cases pass across reflection and source-generated serializers targeting `net11.0` and `net481`. The affected union, structural-classifier, and polymorphism test groups also pass: 7,676 reflection cases and 3,284 source-generation cases per target, with 8 existing skips per source-generation run. Release builds pass for all four library target frameworks. `HashSet<Type>` avoids adding a `System.Collections.Immutable` product dependency. Local lookup benchmarks found `FrozenSet<Type>` faster for 2-4 entries, but not consistently faster for 8-16 entries. End-to-end BenchmarkDotNet measurements on a Windows x64 Hyper-V VM were: | Operation | Before | After | Allocated, unchanged | | --- | ---: | ---: | ---: | | Scalar union deserialization | 105.5 ns | 108.3 ns | 48 B | | Object union deserialization | 429.8 ns | 425.9 ns | 568 B | The scalar case adds approximately 3% overhead; the object case is approximately unchanged in this local run. > [!NOTE] > This pull request was generated with GitHub Copilot. Copilot-Session: 60a19e82-e4d1-4d77-a6ed-cd039cd02cab
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-text-json |
This was referenced Nov 6, 2026
Closed
Member
|
Approved via email. |
Member
|
/ba-g test failure is unrelated |
ViveliDuCh
approved these changes
Nov 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backport of #136226 to release/11.0
/cc @eiriktsarpalis
Customer Impact
The newly introduced type classifier infrastructure intended to support union type deserialization did not include any validation for user-defined classifier delegates returning invalid types.
Regression
Testing
Added testing covering the missed scenario.
Risk
Low. Adds a straightforward validation step after invoking the user-defined delegate.