dotnet / dotnet/aspnetcore

Facilitate deserializing ValidationProblemDetails out of HttpResponseMessage

Open
#56,486 0 comments 0 reactions 0 assignees View on GitHub
area-mvc
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

_No response_

### Describe the solution you'd like

When you return `TypedResults.Problem` from an API you are made to conform to structuring your errors with
a `IDictionary`

This uses the underlying type of [ValidationProblemDetails](https://github.com/dotnet/aspnetcore/blob/3f1acb59718cadf111a0a796681e3d3509bb3381/src/Mvc/Mvc.Core/src/ValidationProblemDetails.cs)

The resulting JSON like this:

```
{
"type": "https://tools.ietf.org/html/rfc9110#section-15.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"errors": {
"targetLocation": [
"Target location must be a 13-digit GLN",
],
"deliveryDate": [
"Delivery date must be at least two days in the future"
]
}
}

```

Seeing as the structure is enforced on one end it would make sense to have functionality built in to deserialize it on the receiving end, rather than each developer rolling their own implementation.

Currently I have to check the content headers media type for `application/problem+json` and deserialize it.

e.g.
```
if (ResponseMessage.Content.Headers.ContentType?.MediaType is "application/problem+json")
{
// ReadFromJsonAsync & System.Text.Json does not work for me - haven't looked into why
var errors = JsonConvert.DeserializeObject(jsonString);
}
```

It would be nice to have something to do this already, as all the information is there and you have the classes already. If it's too complicated to bake this into the response object perhaps a static class similar to `TypedResults` for retrieving the response.

### Additional context

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