Skip to content

Honor validation attributes for null endpoint parameters#67034

Closed
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-validation-attributes-nullable-types
Closed

Honor validation attributes for null endpoint parameters#67034
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-validation-attributes-nullable-types

Conversation

Copilot AI commented Jun 5, 2026

Copy link
Copy Markdown
Contributor

Honor validation attributes for null endpoint parameters

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Run validation attributes for null-bound endpoint parameters

Description

Minimal API endpoint validation skipped parameter validation entirely when model binding produced null. This caused custom validation attributes on nullable value-type parameters to be ignored for omitted query values.

  • Behavior change

    • Remove the endpoint-filter early exit for null arguments so parameter validation still runs.
    • Preserve ValidationContext creation for null-bound parameters by supplying a stable placeholder instance.
  • Coverage

    • Add a routing unit test for a nullable value-type endpoint parameter with a custom ValidationAttribute that always fails when the query value is omitted.
  • Example

    app.MapGet("/get", ([FromQuery, MyVal] int? id) => "ok");
    
    internal sealed class MyValAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
            => new("Always failing attribute.");
    }

    Before this change, GET /get bypassed validation and reached the handler. With this change, it produces the expected validation problem response.

Co-authored-by: Youssef1313 <31348972+Youssef1313@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Validation attributes are ignored for nullable value types when passing a null value

2 participants