Skip to content

Enhance validation error handling in ValidationEndpointFilterFactory using IProblemDetailsService #62066

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 3 commits into
base: main
Choose a base branch
from

Conversation

marcominerva
Copy link
Contributor

@marcominerva marcominerva commented May 22, 2025

Modify validation error handling to utilize IProblemDetailsService for response writing

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

This PR updates the ValidationEndpointFilterFactory to improve how HTTP validation errors are handled, specifically by integrating with the IProblemDetailsService for enhanced response customization. The changes include adjustments to the response logic and dependency usage.

Description

  • Integration with IProblemDetailsService: Added logic to check for and utilize IProblemDetailsService to write custom problem details responses if available. This allows for more flexible and centralized error handling.
  • Fallback to default implementation: Ensured a fallback mechanism to return HttpValidationProblemDetails if IProblemDetailsService is not present.

Fixes #61219

Modify validation error handling to utilize IProblemDetailsService for response writing.
Fallback to default behavior if the service is unavailable.
@marcominerva marcominerva requested a review from halter73 as a code owner May 22, 2025 08:33
@github-actions github-actions bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label May 22, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label May 22, 2025
@martincostello martincostello added area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-validation Issues related to model validation in minimal and controller-based APIs and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels May 22, 2025
@martincostello
Copy link
Member

I imagine the team will want a test for this.

@marcominerva
Copy link
Contributor Author

At the moment, there are no tests at all for the ValidationEndpointFilterFactory class. I need to figure out the correct way to add them, but in the meantime, I decided to open the PR because I’d like to understand whether this is the right approach.
Specifically, I’m referring to returning EmptyHttpResult.Instance: I’ve verified that it works, but I’m wondering if there’s a better solution.

In the meantime, I’ll think about adding a test.

@martincostello
Copy link
Member

Just suggested it as it wasn't a draft 😃

@marcominerva marcominerva marked this pull request as draft May 22, 2025 09:35
@marcominerva
Copy link
Contributor Author

marcominerva commented May 22, 2025

You're right, @martincostello — I just converted the PR to a draft until the tests are done. 😉 But in the meantime, as said I would love to know if I'm going in the right direction.

Introduces the `ValidationFilterEndpointFactoryTests` class with two unit tests. These tests validate HTTP request behavior with and without the `ProblemDetails` service registered, ensuring correct status codes, content types, and the structure of the returned `ProblemDetails` object for validation errors.
Rename validation test class for clarity
@marcominerva marcominerva marked this pull request as ready for review May 22, 2025 14:01
@marcominerva
Copy link
Contributor Author

I finished earlier than expected. I’ve added a couple of tests, so the PR is now ready for review.

@captainsafia
Copy link
Member

@marcominerva Thanks for taking the time to working on this! Sorry I never got back to your question on the issue.

At the moment, there are no tests at all for the ValidationEndpointFilterFactory class. I need to figure out the correct way to add them, but in the meantime, I decided to open the PR because I’d like to understand whether this is the right approach.
Specifically, I’m referring to returning EmptyHttpResult.Instance: I’ve verified that it works, but I’m wondering if there’s a better solution.

There's coverage for the filter in the ValidationsGenerator tests which execute the end-to-end but adding individual tests is good.

One thing to note is that since we don't have a runtime type resolver (see #61220), we won't be able to test validation against complex types but that might be fine for the purpose of this test.

ProblemDetails = problemDetails
}))
{
// We need to prevent further execution, because the actual
Copy link
Member

Choose a reason for hiding this comment

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

I think with this pattern, we've lost the ability for users to add endpoint filters after this one that can inspect what the validation errors that were returned were. Since we lack ModelState in minimal APIs, this might be the only way for users to understand the error state.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In fact, this is the point I mentioned I was unsure about.

However, I have verified that, even with the current implementation, custom endpoint filters are not invoked when validation fails, or am I missing something?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc community-contribution Indicates that the PR has been added by a community member feature-validation Issues related to model validation in minimal and controller-based APIs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Respect ProblemDetailsService in default model validation filter
3 participants