You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This issue is related to this dotnet/runtime issue.
The examples below use a vanilla ASP.NET Core application with this controller:
namespaceApplicationName.Controllers{usingSystem;usingSystem.Threading.Tasks;usingMicrosoft.AspNetCore.Mvc;// This of course belongs to a different file and namespace:publicclassModel{publicGuidId{get;set;}publicintInteger{get;set;}publicstringString{get;set;}}[Produces("application/json")][Consumes("application/json")][Route("test")][ApiController]publicclassController:ControllerBase{[HttpPost]publicasyncTask<IActionResult>PostAsync([FromBody]Modelmodel){returnOk(model);}}}
In each example below, the body for a POST request is listed first. Then comes the response when using Newtonsoft.JSON for doing the model binding, and then the response when using System.Text.Json.
{
"errors": {
"id": [
"Error converting value \"00000000-0000-0000-0000-00000000000x\" to type 'System.Guid'. Path 'id', line 2, position 48."
]
},
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|ac238c1a-4b2c24d7c287f27d."
}
Response using System.Text.Json:
{
"errors": {
"$": [
"The JSON value could not be converted to ApplicationName.Controllers.Model. Path: $ | LineNumber: 1 | BytePositionInLine: 48."
]
},
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"detail": null,
"instance": null,
"traceId": "|29c80116-4896648215ca6161."
}
{
"errors": {
"integer": [
"Input string '2x' is not a valid integer. Path 'integer', line 3, position 17."
]
},
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|ac238c1b-4b2c24d7c287f27d."
}
Response using System.Text.Json:
{
"errors": {
"$": [
"'x' is an invalid end of a number. Expected a delimiter. Path: $ | LineNumber: 2 | BytePositionInLine: 16."
]
},
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"detail": null,
"instance": null,
"traceId": "|29c80117-4896648215ca6161."
}
Example 3:
{
"id": "00000000-0000-0000-0000-00000000000a",
"integer": 2,
"string": 2 x
}
Response using Newtonsoft.JSON:
{
"errors": {
"string": [
"After parsing a value an unexpected character was encountered: x. Path 'string', line 4, position 16."
]
},
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|ac238c1d-4b2c24d7c287f27d."
}
Response using System.Text.Json:
{
"errors": {
"$": [
"The JSON value could not be converted to ApplicationName.Controllers.Model. Path: $ | LineNumber: 3 | BytePositionInLine: 15."
]
},
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"detail": null,
"instance": null,
"traceId": "|a88bc7ad-44774eaf80acd428."
}
Response using System.Text.Json (strict is better, but this could still be improved):
{
"errors": {
"$": [
"The JSON value could not be converted to ApplicationName.Controllers.Model. Path: $ | LineNumber: 3 | BytePositionInLine: 15."
]
},
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"detail": null,
"instance": null,
"traceId": "|29c80118-4896648215ca6161."
}
The responses when using System.Text.Json consistently makes it harder for the end user to figure out what is wrong. It would be great if the validation errors when using System.Text.Json would be of the same quality as when using Newtonsoft.Json.
The text was updated successfully, but these errors were encountered:
What is causing Path to change from $.Id to $ (and the type from System.Guid to ApplicationName.Controllers.Model) in the first example when comparing #38269 with this issue?
This issue is related to this dotnet/runtime issue.
The examples below use a vanilla ASP.NET Core application with this controller:
In each example below, the body for a
POST
request is listed first. Then comes the response when usingNewtonsoft.JSON
for doing the model binding, and then the response when usingSystem.Text.Json
.Example 1:
Response using
Newtonsoft.JSON
:Response using
System.Text.Json
:Example 2:
Response using
Newtonsoft.JSON
:Response using
System.Text.Json
:Example 3:
Response using
Newtonsoft.JSON
:Response using
System.Text.Json
:Example 4:
Response using
Newtonsoft.JSON
(no error in this case due to lenient parsing):Response using
System.Text.Json
(strict is better, but this could still be improved):The responses when using
System.Text.Json
consistently makes it harder for the end user to figure out what is wrong. It would be great if the validation errors when usingSystem.Text.Json
would be of the same quality as when usingNewtonsoft.Json
.The text was updated successfully, but these errors were encountered: