Skip to content

Tolerate null ValidationContext 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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

captainsafia
Copy link
Member

This pull request improves the validation logic in the Microsoft.AspNetCore.Http.Validation namespace by ensuring that a ValidationContext is initialized when it is not pre-populated. It also adds corresponding unit tests to verify this behavior.

Fixes #61738

Validation Logic Enhancements:

  • Updated ValidateAsync methods in ValidatableParameterInfo, ValidatablePropertyInfo, and ValidatableTypeInfo to initialize a ValidationContext if it is not already set. This ensures validation can proceed even when a ValidationContext is not pre-populated. (src/Http/Http.Abstractions/src/Validation/ValidatableParameterInfo.cs - [1] src/Http/Http.Abstractions/src/Validation/ValidatablePropertyInfo.cs - [2] src/Http/Http.Abstractions/src/Validation/ValidatableTypeInfo.cs - [3]

  • Removed unnecessary Debug.Assert statements that previously assumed a non-null ValidationContext. (src/Http/Http.Abstractions/src/Validation/ValidatableParameterInfo.cs - [1] src/Http/Http.Abstractions/src/Validation/ValidatablePropertyInfo.cs - [2] src/Http/Http.Abstractions/src/Validation/ValidatableTypeInfo.cs - [3]

Test Coverage Improvements:

Code Cleanup:

  • Removed unused using System.Diagnostics; directives from multiple files to streamline the codebase. (src/Http/Http.Abstractions/src/Validation/ValidatableParameterInfo.cs - [1] src/Http/Http.Abstractions/src/Validation/ValidatablePropertyInfo.cs - [2] src/Http/Http.Abstractions/src/Validation/ValidatableTypeInfo.cs - [3]

@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

await paramInfo.ValidateAsync(null, context, default);

// Assert – a ValidationContext should have been created and the error recorded
Assert.NotNull(context.ValidationContext);
Copy link
Member

Choose a reason for hiding this comment

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

Any concerns with the internally created ValidationContext "leaking" outside of the validate call?

if (value == null)
{
return;
}

// Although classes can be annotated with [DisplayName], we only process display names when producing
Copy link
Member

Choose a reason for hiding this comment

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

Seems fine. Future looking, any reason we might prefer [DisplayName] here?

// 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?

// 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.

Curious about the serviceProvider: null here. Is there any case where validation logic using this context instance would require the context to be able to resolve services?

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