Web API error response for malformed json using Text.Json is misleading and is exposing internals.
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Describe the bug
When a malformed Json is sent to a route the default serializer Text.Json creates a misleadig error message and exposes internal information. For example a malformed Guid or DateTime produces an error like:
"$.accountId": [
"The JSON value could not be converted to Elwis.Orders.Application.CreateGroupDTO.
Path: $.accountId | LineNumber: 1 | BytePositionInLine: 52."
]
Additionally, the variable name of the DTO is exposed and considered a field:
"createGroupDTO": [
"The createGroupDTO field is required."
],
Expected Behavior
The error message for a malformed value must state that type only: "The JSON value could not be converted to “Guid” or "The JSON value could not be converted to “DateTime”
The name of the body variable should not be exposed in the error message. Only JSON values that can not be converted should be listed in the message.
Steps To Reproduce
Define DTO:
public record CreateGroupDTO(
Guid AccountId,
DateTime Start
);
Create method in GroupsController
[HttpPost]
[Route("")]
public ActionResult<GroupDTO> CreateGroup(CreateGroupDTO createGroupDTO) {...}
Post with malformed Json body
1) Guid
{
"accountId": "4-5717-4562-b3fc-2c963f66afa6",
"start": "2022-02-14T12:39:59.244Z"
}
response body:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-44f0bdb2706fd407dbfa1282f04b66e1-5e64394e46fa6152-00",
"errors": {
"createGroupDTO": [
"The createGroupDTO field is required."
],
"$.accountId": [
"The JSON value could not be converted to Elwis.Orders.Application.CreateGroupDTO. Path: $.accountId | LineNumber: 1 | BytePositionInLine: 46."
]
}
}
2) DateTime
{
"accountId": "3fa85f64-5717-4562-b3fc-2c963f66afa6",
"start": "2022-02-30"
}
response body:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-8f49393621db7dea28f797f4ee69f203-2612b8bd5fe0aa0d-00",
"errors": {
"createGroupDTO": [
"The createGroupDTO field is required."
],
"$.start": [
"The JSON value could not be converted to Elwis.Orders.Application.CreateGroupDTO. Path: $.start | LineNumber: 2 | BytePositionInLine: 23."
]
}
}
Exceptions (if any)
No response
.NET Version
6.0.101
Anything else?
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (6 by maintainers)
Top GitHub Comments
@brunolins16 Can you investigate this issue? You may have some more context based on your recent experience fixing a similar issue.
This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.
See our Issue Management Policies for more information.