Repository navigation
Remove DataAnnotations' ValidationContext from MEV public API - #67549
Conversation
691e402 to
f1b2170
Compare
c4802fe to
20c9328
Compare
5e84a5c to
48a92bf
Compare
48a92bf to
63912cd
Compare
There was a problem hiding this comment.
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.ValidationContextwithValidateContext.ServiceProviderand makeValidationOptionsinit-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
EditContextDataAnnotations) 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. |
1ef3bad to
96de6b4
Compare
| public virtual async Task ValidateAsync(object? value, ValidateContext context, CancellationToken cancellationToken) | ||
| { | ||
| if (value == null) | ||
| ValidateDepth(context); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
|
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. |
|
Will this fix also resolve the issue where I might be running validation over a List of objects marked with the ValidatableType attribute? |
Fixes #67004
Fixes #67550