-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
base: main
Are you sure you want to change the base?
Conversation
Modify validation error handling to utilize IProblemDetailsService for response writing. Fallback to default behavior if the service is unavailable.
I imagine the team will want a test for this. |
At the moment, there are no tests at all for the In the meantime, I’ll think about adding a test. |
Just suggested it as it wasn't a draft 😃 |
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
I finished earlier than expected. I’ve added a couple of tests, so the PR is now ready for review. |
@marcominerva Thanks for taking the time to working on this! Sorry I never got back to your question on the issue.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
Modify validation error handling to utilize IProblemDetailsService for response writing
This PR updates the
ValidationEndpointFilterFactory
to improve how HTTP validation errors are handled, specifically by integrating with theIProblemDetailsService
for enhanced response customization. The changes include adjustments to the response logic and dependency usage.Description
IProblemDetailsService
: Added logic to check for and utilizeIProblemDetailsService
to write custom problem details responses if available. This allows for more flexible and centralized error handling.HttpValidationProblemDetails
ifIProblemDetailsService
is not present.Fixes #61219