-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Treat ValidationContext as required in validation resolver APIs #61854
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This pull request improves validation logic by ensuring that a ValidationContext is initialized when it is not pre-populated, thereby tolerating a null context during validation.
- Initializes ValidationContext in ValidateAsync methods for parameters, properties, and types
- Removes Debug.Assert statements that assumed a non-null context
- Adds comprehensive unit tests to verify the new behavior
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
File | Description |
---|---|
src/Http/Http.Abstractions/test/Validation/ValidatableTypeInfoTests.cs | Adds new tests to verify error handling and successful validation with uninitialized ValidationContext |
src/Http/Http.Abstractions/test/Validation/ValidatableParameterInfoTests.cs | Introduces tests for required parameters where ValidationContext is initialized during validation |
src/Http/Http.Abstractions/src/Validation/ValidatableTypeInfo.cs | Removes Debug.Assert and initializes ValidationContext with a fallback display name |
src/Http/Http.Abstractions/src/Validation/ValidatablePropertyInfo.cs | Removes redundant Debug.Assert and adds contextual initialization of ValidationContext |
src/Http/Http.Abstractions/src/Validation/ValidatableParameterInfo.cs | Removes Debug.Assert and ensures a non-null value for ValidationContext initialization |
src/Http/Http.Abstractions/test/Validation/ValidatableParameterInfoTests.cs
Outdated
Show resolved
Hide resolved
src/Http/Http.Abstractions/src/Validation/ValidatableTypeInfo.cs
Outdated
Show resolved
Hide resolved
// ValidationContext requires a non-null value although the invocation pattern that we use | ||
// calls `GetValidationResult` and passes the value there. `GetValidationResult` tolerates | ||
// null values so we only need to set a non-null value to the ValidationContext here. | ||
context.ValidationContext ??= new ValidationContext(value ?? new object(), displayName: DisplayName, serviceProvider: null, items: null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we avoid allocating a new value each time in the case we don't need one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah, like only newing up in the getter?
src/Http/Http.Abstractions/src/Validation/ValidatableParameterInfo.cs
Outdated
Show resolved
Hide resolved
e69a60d
to
1ec6dcf
Compare
This pull request improves the validation logic in the
Microsoft.AspNetCore.Http.Validation
namespace by ensuring that aValidationContext
is a required property.Fixes #61738
This pull request introduces several updates to the validation framework in
Microsoft.AspNetCore.Http
, focusing on improving type safety, simplifying validation logic, and enhancing test coverage. The key changes include making theValidationContext
property inValidateContext
required, removing redundantDebug.Assert
statements, and updating tests to align with the newValidateContext
initialization pattern.Validation Framework Updates:
ValidationContext
Property Update:ValidationContext
property inValidateContext
is now marked asrequired
, ensuring it is always initialized. Additional documentation and examples have been added to clarify its usage. ([[1]](https://github.com/dotnet/aspnetcore/pull/61854/files#diff-b16fffb7593a94827187c81633f618740d0315cafe67c1ad148e74ee73d48369L19-R36)
,[[2]](https://github.com/dotnet/aspnetcore/pull/61854/files#diff-c25699c95b8367fb6f1c1c20b4566b5f15479fae809d04f1aee4faef7e36c883L27-R27)
)ValidationContext
initialization in tests has been updated to use object initializers, improving readability and consistency. ([[1]](https://github.com/dotnet/aspnetcore/pull/61854/files#diff-f089fa164925b2123ac8f2eb82a21c16006c355262f84596e6cbf32a0ab25a43L44-R53)
,[[2]](https://github.com/dotnet/aspnetcore/pull/61854/files#diff-f089fa164925b2123ac8f2eb82a21c16006c355262f84596e6cbf32a0ab25a43L99-R111)
,[[3]](https://github.com/dotnet/aspnetcore/pull/61854/files#diff-f089fa164925b2123ac8f2eb82a21c16006c355262f84596e6cbf32a0ab25a43R143-L159)
, and others)Simplification of Validation Logic:
Debug.Assert
statements that checked for the presence ofValidationContext
, as this is now enforced by therequired
modifier. ([[1]](https://github.com/dotnet/aspnetcore/pull/61854/files#diff-688f42bea373bd51787ca8ee43ca3a6456beba2ccf638189bc4d64a549627401L63-L64)
,[[2]](https://github.com/dotnet/aspnetcore/pull/61854/files#diff-c3a66d105b9c639c52f0ac3f111af08016219a354b470f31322b068cfadab37cL64-L65)
,[[3]](https://github.com/dotnet/aspnetcore/pull/61854/files#diff-4747a7dd84b487cb53a7e83e9402dac63077283371409e02b324106251c36d5bL48)
)Create
method inValidationEndpointFilterFactory
to initializeValidateContext
lazily, reducing unnecessary object creation. ([[1]](https://github.com/dotnet/aspnetcore/pull/61854/files#diff-5d39135cbdf8958ffced3a56b7d0b970c302cb2c24d32b8672b1c9c9c004c187L46-R46)
,[[2]](https://github.com/dotnet/aspnetcore/pull/61854/files#diff-5d39135cbdf8958ffced3a56b7d0b970c302cb2c24d32b8672b1c9c9c004c187L60-R81)
)Test Enhancements:
ValidatableTypeInfoTests
to reflect the newValidateContext
initialization pattern, ensuring consistency and improved maintainability. ([[1]](https://github.com/dotnet/aspnetcore/pull/61854/files#diff-f089fa164925b2123ac8f2eb82a21c16006c355262f84596e6cbf32a0ab25a43L308-L313)
,[[2]](https://github.com/dotnet/aspnetcore/pull/61854/files#diff-f089fa164925b2123ac8f2eb82a21c16006c355262f84596e6cbf32a0ab25a43R346-L362)
,[[3]](https://github.com/dotnet/aspnetcore/pull/61854/files#diff-f089fa164925b2123ac8f2eb82a21c16006c355262f84596e6cbf32a0ab25a43R381-L398)
, and others)