Skip to content

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

Merged
merged 2 commits into from
May 14, 2025

Conversation

captainsafia
Copy link
Member

@captainsafia captainsafia commented May 9, 2025

This pull request improves the validation logic in the Microsoft.AspNetCore.Http.Validation namespace by ensuring that a ValidationContext 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 the ValidationContext property in ValidateContext required, removing redundant Debug.Assert statements, and updating tests to align with the new ValidateContext initialization pattern.

Validation Framework Updates:

  • ValidationContext Property Update:

    • The ValidationContext property in ValidateContext is now marked as required, 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))
    • The 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:

    • Removed Debug.Assert statements that checked for the presence of ValidationContext, as this is now enforced by the required 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))
    • Adjusted the Create method in ValidationEndpointFilterFactory to initialize ValidateContext 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:

  • Updated test cases in ValidatableTypeInfoTests to reflect the new ValidateContext 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)

@github-actions github-actions bot added the area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions label May 9, 2025
@captainsafia captainsafia requested a review from Copilot May 9, 2025 20:38
Copy link
Contributor

@Copilot Copilot AI left a 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

// 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);
Copy link
Member

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?

Copy link
Member

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?

@captainsafia captainsafia changed the title Tolerate null ValidationContext in validation resolver APIs Treat ValidationContext as required in validation resolver APIs May 13, 2025
@captainsafia captainsafia merged commit f544e7c into main May 14, 2025
27 checks passed
@captainsafia captainsafia deleted the fix-validate-nre branch May 14, 2025 21:16
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-preview5 milestone May 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Min API validation NRE when ValidationContext is not set on ValidateContext
3 participants