Minimal API Validation should respect [JsonPropertyName]
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 276
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Describe the bug
Related to #61764.
If a parameter bound to a Minimal API endpoint from the request body uses explicit `[JsonPropertyName]` attributes to control serialization, this value is not used to generate responses when validation fails.
https://github.com/dotnet/aspnetcore/blob/89bd3386c750af8331900a26f18ebe54d5b9f2e7/src/Validation/gen/Parsers/ValidationsGenerator.TypesParser.cs#L201-L203
For example a model property such as this:
```csharp
[JsonPropertyName("plaintext")]
[Required]
public string Plaintext { get; set; } = string.Empty;
```
Generates an error such as the below when absent from a request:
```json
{
"title": "One or more validation errors occurred.",
"errors": {
"Plaintext": [
"The Plaintext field is required."
]
}
}
```
It's possible to override the value in the message using `[Display]` (at the cost of additional duplication), but it isn't possible to override the error property name:
```diff
+ [Display(Name = "plaintext")]
[JsonPropertyName("plaintext")]
[Required]
public string Plaintext { get; set; } = string.Empty;
```
### Expected Behavior
The `Name` value of `[JsonPropertyName]` is used as the default for the `Name` and `DisplayName` values if not overridden by `[Display]` (or some other mechanism that should take precedence) to generate a response like the following:
```json
{
"title": "One or more validation errors occurred.",
"errors": {
"plaintext": [
"The plaintext field is required."
]
}
}
```
### Steps To Reproduce
1. Clone https://github.com/martincostello/api/commit/5891d85d3ac895e5dee8ea9d129d1c256baa4580
2. Run `build.ps1` in the root of the repository.
### Exceptions (if any)
_No response_
### .NET Version
10.0.100-preview.7.25380.108
### Anything else?
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.