Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Validation message is not informative in case of deserialization error. #2768

Open
pil0t opened this issue Apr 4, 2023 · 1 comment
Open
Assignees

Comments

@pil0t
Copy link

pil0t commented Apr 4, 2023

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

@corranrogue9
Copy link
Contributor

We should investigate that this error is coming from WebApi/ODL.

@pil0t would you be interested in making a contribution for this change?

@corranrogue9 corranrogue9 self-assigned this Apr 4, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants