Repository navigation
Add tests for validating null values of minimal API parameters - #67395
Conversation
…nto 67033-validation-attributes
…nto 67033-validation-attributes
|
Thanks for your PR, @surya3655. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
…nto 67033-validation-attributes
…nto 67033-validation-attributes # Conflicts: # src/Validation/src/ValidatableParameterInfo.cs
…nto 67033-validation-attributes
…nto 67033-validation-attributes # Conflicts: # src/Http/Routing/src/ValidationEndpointFilterFactory.cs
…nto 67033-validation-attributes
| { | ||
| // A small struct to hold the validatable parameter details to avoid allocating arrays for parameters that don't need validation | ||
| private readonly record struct ValidatableParameterEntry(int Index, IValidatableParameterInfo Parameter, string Name); | ||
| private readonly record struct ValidatableParameterEntry(int Index, IValidatableParameterInfo Parameter, string Name, ParameterValidationTarget ValidationTarget); |
There was a problem hiding this comment.
I don't think a new class (ParameterValidationTarget) is necessary here.
After few internal discussions, we decided to always use a throwaway object as the ObjectInstance for parameter validation (regardless of whether the value is or isn't null)
…nto 67033-validation-attributes
|
@Youssef1313 , I tested the Minimal API validation with the latest main branch, but the validation is not working. The same validation works correctly in my existing source. |
|
There is a build error here. Overall, I'm not really sure if the added tests actually reach the ValidationEndpointFilter code path. In any cases, the new tests should belong to So please ensure the code builds correctly and the tests are passing. Once you get to a point where the tests are passing, try to revert the product change to validate if or not they are still passing. Even if they pass without a product change, we can happily merge a test-only change here (assuming no other tests cover the scenario). |
|
@Youssef1313 I updated/rebased the branch with latest main and reverted my product changes to verify whether the issue is already fixed. After resolving the build issue and testing the latest source again, I am still seeing the same behavior: Minimal API validation is not triggered for the sample. In the sample, I’ll move the regression tests to |
|
@surya3655 The current branch still doesn't build. It's best to get it to build successfully with tests passing to discuss this further. |
| }; | ||
| } | ||
|
|
||
| private static ValidationContext CreateValidationContext(HttpContext httpContext, ValidatableParameterEntry entry, object? argument) |
There was a problem hiding this comment.
FYI, we no longer create a ValidationContext here at all because of recent Microsoft.Extensions.Validation API changes.
…nto 67033-validation-attributes
…nto 67033-validation-attributes
…nto 67033-validation-attributes
|
@surya3655 The CI is failing because there are test failures. For example: |
…nto 67033-validation-attributes
There was a problem hiding this comment.
Pull request overview
This PR addresses minimal API validation for nullable parameters so that validation attributes are still evaluated even when the bound argument is null (e.g., no query value supplied), and adds regression coverage for the affected scenarios.
Changes:
- Added unit tests covering validation behavior for nullable value-type parameters with
[Required],[Range], and a customValidationAttribute. - Added a regression test ensuring validation/filtering behavior is consistent when parameters are omitted vs. explicitly provided.
…nto 67033-validation-attributes
|
@Youssef1313, I tested the Minimal API null value validation with the latest source code, and I can confirm that the issue has been resolved. I also verified that there were no existing test cases covering this null value validation scenario. Therefore, I added new test cases for this validation, and all of them passed successfully in CI. Could you please review the test cases and share your feedback? |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.
Suppressed comments (4)
src/Http/Http.Extensions/test/ValidationFilterEndpointFactoryTests.cs:361
- This theory uses
nullvalues in[InlineData]for bothqueryStringandexpectedError, but both parameters are declared non-nullablestring. Update these to nullable to avoid nullability warnings under#nullable enable.
public async Task ValidatesCustomAttributeOnNullableQueryParameter(
string queryString,
int expectedStatusCode,
bool expectedHandlerExecuted,
string expectedError)
src/Http/Http.Extensions/test/ValidationFilterEndpointFactoryTests.cs:521
expectedMessagedefaults tonull, but is declared as non-nullablestring. This can trigger CS8625 under nullable. Make itstring?since null is an allowed value (used to mean "don't assert on message").
private static async Task AssertSingleValidationErrorAsync(MemoryStream body, string expectedKey, string expectedMessage = null)
src/Http/Http.Extensions/test/ValidationFilterEndpointFactoryTests.cs:332
- The test passes
nullvia[InlineData(null, ...)], but the parameter is declared as non-nullablestring. With nullable enabled this will produce warnings (and may fail the build if warnings are treated as errors). Make the test parameter nullable to match the data.
This issue also appears in the following locations of the same file:
- line 357
- line 521
public async Task ValidatesRequiredNullableQueryParameter(string queryString, int expectedStatusCode, bool expectedHandlerExecuted)
src/Http/Http.Extensions/test/ValidationFilterEndpointFactoryTests.cs:560
ValidationAttribute.IsValidis nullable-aware (object?input,ValidationResult?output). The current override uses non-nullable types but returnsValidationResult.Success(which isnull), which can produce nullability warnings and misrepresents the contract. Align the override signature (and include the member name like the other local attribute) to keep the test deterministic.
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
=> value is 1
? ValidationResult.Success
: new ValidationResult($"Value must equal 1, but was {value?.ToString() ?? "null"}.");
}
|
Thanks for the contribution @surya3655 🎉 |
Description
This change fixes validation behavior for nullable parameters in minimal APIs.
Previously, validatable parameters could be skipped when the bound argument was
null, which meant validation attributes were not evaluated for nullable parameters when no value was provided. In addition,ValidatableParameterInfo.ValidateAsynccould return early fornullvalues even when validation attributes were present.This update changes the validation flow so that:
nullValidationContextis created with a non-null placeholder instanceValidateAsynconly skipsnullvalues when there are no validation attributes to applyThis ensures validation attributes continue to run for nullable parameters, including when no query value is supplied.
Output
Before:


After



Fixes #67033