Description
Validation message is not informative in case of deserialization error.
Assemblies affected
<PackageReference Include="Microsoft.AspNetCore.OData" Version="8.1.0" />
Reproduce steps
Given simple model
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
and controller method with straight-forward validation like:
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
when sending non-valid data, like
{
"id": 0,
"name": "string",
"age": "12Y3"
}
Expected result
I expect, similar to non-odata validation, informative message, with path, and some hint to where the problem is actually happening, for example:
{
"error": {
"code": "",
"message": "The input was not valid.\r\n\r\ncustomer:\r\nThe customer field is required.",
"details": [
{
"code": "",
"target": "customer.age",
"message": "The input was not valid."
},
{
"code": "",
"target": "customer",
"message": "The customer field is required."
}
]
}
}
important part here is : "target": "customer.age",
, it indicates where exactly problem happening. It can be something else, similar to non-odata validation error message:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-1fe2f2e1f3ead0709ff5e445e1dffdc9-c1c420ab67a12d0c-00",
"errors": {
"customer": [
"The customer field is required."
],
"$.age": [
"The JSON value could not be converted to System.Int32. Path: $.age | LineNumber: 3 | BytePositionInLine: 15."
]
}
}
Actual result
With OData enabled controllers, I got:
{
"error": {
"code": "",
"message": "The input was not valid.\r\n\r\ncustomer:\r\nThe customer field is required.",
"details": [
{
"code": "",
"message": "The input was not valid."
},
{
"code": "",
"target": "customer",
"message": "The customer field is required."
}
]
}
}
So, there no clue or hint that problem actually in the age
property. In trivial models like presented it's relatively simple to understand what happened, but in case of complex models - it could be really challenging to figure out what is causing this.
Something similar to above
"$.age": [
"The JSON value could not be converted to System.Int32. Path: $.age | LineNumber: 3 | BytePositionInLine: 15."
]
is expected, and would really help.
Full example https://github.com/pil0t/ODataValidation