dotnet / dotnet/aspnetcore

How to properly serialize `ValidationProblemDetails` from middleware through `IProblemDetailsService`?

Open
#53,779 1 comment 6 reactions 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

When generating [`ValidationProblemDetails`](https://github.com/dotnet/aspnetcore/blob/v7.0.15/src/Mvc/Mvc.Core/src/ValidationProblemDetails.cs) using [`ProblemDetailsFactory.CreateValidationProblemDetails`](https://github.com/dotnet/aspnetcore/blob/v7.0.15/src/Mvc/Mvc.Core/src/Infrastructure/DefaultProblemDetailsFactory.cs) it contains an `Errors` property. This property contains custom validation errors from a `ModelStateDictionary`. But when we use [`IProblemDetailsService`](https://github.com/dotnet/aspnetcore/blob/v7.0.15/src/Http/Http.Abstractions/src/ProblemDetails/IProblemDetailsService.cs) to serialize it the `Errors` property is gone.
It seems that both [`DefaultApiProblemDetailsWriter`](https://github.com/dotnet/aspnetcore/blob/v7.0.15/src/Mvc/Mvc.Core/src/Infrastructure/DefaultApiProblemDetailsWriter.cs) and [`DefaultProblemDetailsWriter`](https://github.com/dotnet/aspnetcore/blob/v7.0.15/src/Http/Http.Extensions/src/DefaultProblemDetailsWriter.cs) can't handle it.

Not sure why but `DefaultApiProblemDetailsWriter` simply [recreates](https://github.com/dotnet/aspnetcore/blob/c49ccc8727c620ce1cb8fb431bb3fe8f2e747236/src/Mvc/Mvc.Core/src/Infrastructure/DefaultApiProblemDetailsWriter.cs#L56) the instance loosing `Errors` property on the way and `DefaultProblemDetailsWriter` somehow isn't able to "find" [`ValidationProblemDetailsJsonConverter`](https://github.com/dotnet/aspnetcore/blob/v7.0.15/src/Mvc/Mvc.Core/src/Infrastructure/ValidationProblemDetailsJsonConverter.cs) which should take care of properly serializing `Errors`.

We used `AddProblemDetails` before and after a call to `AddControllers` (to test both of writers) and doesn't make any difference.

### Expected Behavior

When serializing `ValidationProblemDetails` using `IProblemDetailsService` it should serialize all of it's properties.

### Steps To Reproduce

Here is a test repro made using built-in Visual Studio wizard using Net 7.x:
https://github.com/uhfath/TestProblemDetailsService

Simply start it. It will open a default browser on an endpoint and show a JSON output.
The code uses a simple [`ProblemDetailsMiddleware`](https://github.com/uhfath/TestProblemDetailsService/blob/master/TestProblemDetailsService/ProblemDetailsMiddleware.cs) which catches a specific exception and produces a `ValidationProblemDetails` out of it.

### Exceptions (if any)

_No response_

### .NET Version

7.x

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.