Repository navigation
Validate active union cases in MVC - #69834
Conversation
Keep VisitChildren independent of union strategies and preserve explicit validation-state metadata and strategies. Identify the actual interface getter so unrelated public Value properties keep their own validation. Co-authored-by: Copilot App <[email protected]> Copilot-Session: 0373e779-bf39-4f34-b837-5c6bf3eda99a
There was a problem hiding this comment.
🟡 Changes recommended
Union-level metadata overrides are ignored, causing validation to traverse cases contrary to ValidationStateEntry.Metadata.
2 open findings
What changed in this PR
Adds runtime metadata resolution so MVC validates active C# union cases and returns automatic HTTP 400 responses.
Changes:
- Adds union-aware MVC validation traversal.
- Preserves validation attributes, suppression, and validation state.
- Adds integration and HTTP regression coverage.
| File | Description |
|---|---|
UnionTypes.cs |
Adds validated union test models. |
UnionsController.cs |
Adds validation test endpoints. |
UnionValidationIntegrationTest.cs |
Covers union validation behavior. |
SystemTextJsonInputFormatterTest.UnionValidation.cs |
Covers automatic HTTP 400 responses. |
ValidationVisitor.cs |
Resolves active union-case metadata. |
DefaultUnionValidationStrategy.cs |
Traverses union values. |
DefaultModelMetadata.cs |
Prevents union validation from being skipped. |
🧠 Review effort: Balanced
|
I’m in favor of taking this for .NET 11 RC 2. Active union cases should receive validation just like the DTOs they contain. Shipping .NET 11 without this would make a later fix change which requests receive automatic 400 responses, so I’d rather establish that behavior before RTM. The pre-existing Minimal API gaps can be tracked separately and don’t need to block this PR or its backport. |
|
/backport to release/11.0 |
|
Started backporting to |
|
✅ No documentation PR was created. Confidence that documentation is needed: 25% No docs needed: bug fix restoring expected validation of active union cases in MVC. Human review is optional if maintainers consider the newly enforced validation noteworthy. Documentation surfaces considered
|
* init * Resolve MVC union case metadata before validation traversal Keep VisitChildren independent of union strategies and preserve explicit validation-state metadata and strategies. Identify the actual interface getter so unrelated public Value properties keep their own validation. Co-authored-by: Copilot App <[email protected]> * address PR review * address PR comment --------- Co-authored-by: Korolev Dmitry <[email protected]> Copilot-Session: 0373e779-bf39-4f34-b837-5c6bf3eda99a


MVC currently skips validation of active union cases because
IUnion.Valueis declared asobject. Resolve runtime metadata before validation so invalid cases produce automatic HTTP 400 responses under[ApiController].Preserves property validation attributes, suppression, and validation-state overrides. Adds integration and HTTP regression coverage for nested unions, collections, nullable unions, and custom
IUnionimplementations.Fixes the reported MVC bug in #64599 (comment)
Fixes #68268
release/11.0 backport