Skip to content

Remove DataAnnotations' ValidationContext from MEV public API - #67549

Merged
Youssef1313 merged 13 commits into
mainfrom
dev/ygerges/refactor-validation
Jul 21, 2026
Merged

Youssef1313 merged 13 commits into
mainfrom
dev/ygerges/refactor-validation

Conversation

@Youssef1313

@Youssef1313 Youssef1313 commented Jul 1, 2026 •

Copy link
Copy Markdown
Member

Fixes #67004
Fixes #67550

@Youssef1313
Youssef1313 force-pushed the dev/ygerges/refactor-validation branch 2 times, most recently from 691e402 to f1b2170 Compare July 1, 2026 20:30
@Youssef1313
Youssef1313 force-pushed the dev/ygerges/refactor-validation branch 6 times, most recently from c4802fe to 20c9328 Compare July 2, 2026 11:19
@Youssef1313
Youssef1313 force-pushed the dev/ygerges/refactor-validation branch 3 times, most recently from 5e84a5c to 48a92bf Compare July 4, 2026 05:02
@Youssef1313
Youssef1313 marked this pull request as ready for review July 9, 2026 17:42
Copilot AI review requested due to automatic review settings July 9, 2026 17:42
@Youssef1313
Youssef1313 requested a review from a team as a code owner July 9, 2026 17:42

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.

Pull request overview

This PR removes System.ComponentModel.DataAnnotations.ValidationContext from the Microsoft.Extensions.Validation public surface by replacing it with a ServiceProvider on ValidateContext, and refactors the validation pipeline to construct ValidationContext internally where needed. It also updates Minimal APIs, Blazor Forms integration, tests, and benchmarks to use the new API shape.

Changes:

  • Replace ValidateContext.ValidationContext with ValidateContext.ServiceProvider and make ValidationOptions init-only.
  • Refactor attribute validation to pass ValidationContext/display name explicitly through the pipeline (properties, parameters, and type-level validation).
  • Update integrations (Minimal API endpoint filter, Blazor EditContext DataAnnotations) plus tests/benchmarks for the new API.

Reviewed changes

Copilot reviewed 20 out of 20 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/Validation/src/ValidateContext.cs Replaces public ValidationContext with ServiceProvider; refactors attribute validation plumbing.
src/Validation/src/ValidateContextMutableState.cs Removes mutable state that previously mirrored ValidationContext display/member metadata.
src/Validation/src/IValidationErrorReporter.cs Updates error reporting contract to carry display name and member name explicitly.
src/Validation/src/ValidatableTypeInfo.cs Refactors type-level validation to work without storing ValidationContext on ValidateContext.
src/Validation/src/ValidatablePropertyInfo.cs Creates per-property ValidationContext from the containing object and passes display name explicitly.
src/Validation/src/ValidatableParameterInfo.cs Creates per-parameter ValidationContext (now derived from value + ServiceProvider) and passes display name explicitly.
src/Validation/src/PublicAPI.Unshipped.txt Records public API removals/additions (ValidationContext removal, ServiceProvider + init-only ValidationOptions).
src/Http/Routing/src/ValidationEndpointFilterFactory.cs Updates Minimal API validation filter to use ServiceProvider instead of creating/storing ValidationContext.
src/Http/Http/perf/Microbenchmarks/ValidatableTypesBenchmark.cs Updates benchmark setup to populate ServiceProvider.
src/Components/Forms/src/EditContextDataAnnotationsExtensions.cs Updates Blazor Forms integration to populate ServiceProvider instead of ValidationContext.
src/Validation/test/Microsoft.Extensions.Validation.Tests/ValidationLocalizationIntegrationTests.cs Updates tests to use ServiceProvider API.
src/Validation/test/Microsoft.Extensions.Validation.Tests/ValidatableTypeInfoTests.cs Updates tests to use ServiceProvider API.
src/Validation/test/Microsoft.Extensions.Validation.Tests/ValidatableParameterInfoTests.cs Updates tests to use ServiceProvider API.
src/Validation/test/Microsoft.Extensions.Validation.Tests/RuntimeValidatableParameterInfoResolverTests.cs Updates tests to use ServiceProvider API.
src/Validation/test/Microsoft.Extensions.Validation.Tests/DisplayNameInfoTests.cs Updates tests to use ServiceProvider API.
src/Validation/test/Microsoft.Extensions.Validation.Tests/AsyncValidationTests.cs Updates async validation tests to use ServiceProvider API.
src/Validation/Localization/test/ValidationLocalizationPipelineTests.cs Updates localization pipeline tests to use ServiceProvider API.
src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/ValidationsGenerator.ValidatableType.cs Updates generator tests to use ServiceProvider API.
src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/ValidationsGenerator.SkipValidation.cs Updates generator tests to use ServiceProvider API.
src/Validation/test/Microsoft.Extensions.Validation.GeneratorTests/ValidationsGenerator.ClassAttributes.cs Updates generator tests to use ServiceProvider API.

Comment thread src/Validation/src/ValidateContext.cs Resolved
Comment thread src/Validation/src/ValidateContext.cs Resolved
@Youssef1313
Youssef1313 requested a review from halter73 July 9, 2026 19:33
@Youssef1313
Youssef1313 force-pushed the dev/ygerges/refactor-validation branch from 1ef3bad to 96de6b4 Compare July 17, 2026 09:03
public virtual async Task ValidateAsync(object? value, ValidateContext context, CancellationToken cancellationToken)
{
if (value == null)
ValidateDepth(context);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Did we move this before the null check because earlier iterations validated type-level attributes for null values? Now that null validation has been reverted, can we move it back and add sync/async tests for null values at the MaxDepth boundary?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The ordering change was unintentional. Initially I removed the null check altogether, and when I introduced it again I didn't pay attention to where it was originally declared. I guess it doesn't matter that much but I can restore it as it was previously for now.

@Youssef1313

Copy link
Copy Markdown
Member Author

I handled RequiredAttribute specifically. Some tests are relying on that behavior (even though the tests were exercising something that wouldn't be reachable from our implementation on minimal API side).

I think it's not a bad idea to handle at least RequiredAttribute for now.

@Youssef1313
Youssef1313 merged commit 24a934e into main Jul 21, 2026
26 checks passed
@Youssef1313
Youssef1313 deleted the dev/ygerges/refactor-validation branch July 21, 2026 10:38

@leveyp94-hash leveyp94-hash left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Not done

@Youssef1313 Youssef1313 added the feature-validation Issues related to model validation in minimal and controller-based APIs label Sep 1, 2026
@stephajn

stephajn commented Oct 7, 2026

Copy link
Copy Markdown

Will this fix also resolve the issue where I might be running validation over a List of objects marked with the ValidatableType attribute?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature-validation Issues related to model validation in minimal and controller-based APIs

Projects

None yet

6 participants