Skip to content
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

When endpoint return ValidationProblem and has errors the property errors doesn't have values. #59052

Open
1 task done
jvsancho80 opened this issue Nov 19, 2024 · 3 comments
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc

Comments

@jvsancho80
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When endpoint return ValidationProblem and has errors the property errors doesn't have values in .Net 8 the same code works ok. I believe return TypedResults.Problem instead of TypedResults.ValidationProblem. My code return TypedResults.ValidationProblem. I have checked it and my code is correct.

Net 8
Image

Net 9
Image

My code works in Net 8
Image

Expected Behavior

The code return this because my code return TypedResults.ValidationProblems. Net8 the same code works ok.

Image

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

Net 9

Anything else?

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Nov 19, 2024
@martincostello martincostello added area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Nov 19, 2024
@jvsancho80
Copy link
Author

jvsancho80 commented Dec 18, 2024

When will resolve this issue @martincostello ? I think is an important because is not possible use ValidationProblemDetails.

Thanks

@sguryev
Copy link

sguryev commented Jan 6, 2025

I have found the source of the issue.
In the NET8 we didn't have IProblemDetailsService usage in the ValidationProblem.ExecuteAsync() method. So it was writing HttpValidationProblemDetails object to the JSON.
Image

In the NET9 we have a new approach with IProblemDetailsService

if (problemDetailsService is null || !await problemDetailsService.TryWriteAsync(new() { HttpContext = httpContext, ProblemDetails = ProblemDetails }))

The AddProblemDetails() extension method adds DefaultProblemDetailsWriter in addition to the DefaultApiProblemDetailsWriter which is added by AddMvcCoreServices()

In case the wrong order DefaultApiProblemDetailsWriter will be called first by ProblemDetailsService it will handle the problem, create basic ProblemDetails and forget about Errors property from the derived HttpValidationProblemDetails or ValidationProblemDetails
Image
As the result: Errors property is missing.

But placing AddProblemDetails() before AddControllers(), AddMvc(), etc will place the DefaultProblemDetailsWriter before DefaultApiProblemDetailsWriter and it will be used by ProblemDetailsService. DefaultProblemDetailsWriter preserves the original problem details object from the ProblemDetailsContext instead of creating a new one.
Image

I have found one mention about the order here: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-9.0#custom-iproblemdetailswriter
But it's related to the custom IProblemDetailsWriter not the AddProblemDetails() call in general.
Image

I'm not sure if current DefaultApiProblemDetailsWriter behavior is correct.
Same guide has a sample with incorrect order of calls which makes me think that DefaultApiProblemDetailsWriter has to be fixed.
Image

@jvsancho80
Copy link
Author

Now it works. Thanks

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
Projects
None yet
Development

No branches or pull requests

3 participants