Skip to content

Validate active union cases in MVC - #69834

Merged
DeagleGross merged 4 commits into
dotnet:mainfrom
DeagleGross:deaglegross-mvc-union-validation
Oct 19, 2026
Merged

DeagleGross merged 4 commits into
dotnet:mainfrom
DeagleGross:deaglegross-mvc-union-validation

Conversation

@DeagleGross

@DeagleGross DeagleGross commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

MVC currently skips validation of active union cases because IUnion.Value is declared as object. 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 IUnion implementations.

Fixes the reported MVC bug in #64599 (comment)
Fixes #68268

release/11.0 backport

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
@DeagleGross DeagleGross self-assigned this Oct 14, 2026
Copilot AI balanced review requested due to automatic review settings October 14, 2026 15:03
@github-actions github-actions Bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Oct 14, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

@halter73

Copy link
Copy Markdown
Member

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.

@DeagleGross
DeagleGross merged commit c7a1c5c into dotnet:main Oct 19, 2026
29 checks passed
@DeagleGross

Copy link
Copy Markdown
Member Author

/backport to release/11.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

@github-actions

Copy link
Copy Markdown
Contributor

✅ 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

  • Conceptual article: Not required — Bug fix in internal MVC metadata/validation code (DefaultModelMetadata, DefaultUnionValidationStrategy, ValidationVisitor); no public API or configuration added; no existing docs on union validation.
  • Migration guidance: Not required — No new recommended approach or opt-in/opt-out; validation simply works as expected.
  • Breaking change: Not required — No label or evidence of intentional breaking change; invalid union values now return 400 as expected for a reported bug.

wtgodbe pushed a commit that referenced this pull request Oct 19, 2026
* 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
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 30, 2026

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — c56a1c05 Deployed Oct 19, 2026 by DeagleGross via conclusion #71
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support unions in ValidationsGenerator

4 participants