0

Web API 2.0 uses partial validation to validate the request body "Unexpected character encountered while parsing value"

If I send invalid JSON:

{RequiredString_AllowEmptyStrings:, NotRequiredBool: }

It returns:

{
   "message": "The request is invalid.",
   "modelState":    {
      "requestParams.RequiredString_AllowEmptyStrings": ["Error reading string. Unexpected token: Undefined. Path 'RequiredString_AllowEmptyStrings', line 1, position 34."],
      "requestParams.NotRequiredBool":       [
         "Unexpected character encountered while parsing value: }. Path 'NotRequiredBool', line 1, position 53.",
         "Unexpected character encountered while parsing value: }. Path 'NotRequiredBool', line 1, position 53."
      ]
   }
}

and I I turn it around:

{NotRequiredBool: , RequiredString_AllowEmptyStrings: }

I get:

{
   "message": "The request is invalid.",
   "modelState": {"requestParams.RequiredString_AllowEmptyStrings":    [
      "Unexpected character encountered while parsing value: }. Path 'RequiredString_AllowEmptyStrings', line 1, position 54.",
      "Unexpected character encountered while parsing value: }. Path 'RequiredString_AllowEmptyStrings', line 1, position 54."
   ]}
}

The Unexpected character encountered Error occurs only on the last invalid entry.

If the same invalid string is not in last place, Error is Error reading string.

Here is yet another version of the error parsing with a valid entry 2nd.

{RequiredString_AllowEmptyStrings: , NotRequiredBool: false}

returns..

{
   "message": "The request is invalid.",
   "modelState":    {
      "requestParams.RequiredString_AllowEmptyStrings":       [
         "Error reading string. Unexpected token: Undefined. Path 'RequiredString_AllowEmptyStrings', line 1, position 35.",
         "The RequiredString_AllowEmptyStrings field is required."
      ],

All of the above JSON is invalid if I use an online JSON Validator. The Newtonsoft.JSON parser here seems to do a partial validation by key/value pairs, and then stops when fails.

So, my question: Can I pre-parse the JSON Body and add a value in the empty fields so that I can reconstruct valid JSON before the model is validated? Is there some JSON Validator that I can use that does this? I could add this to the WebApi config as a global Validator.

Andrew Roberts
  • 990
  • 2
  • 12
  • 26

2 Answers2

2

If you post invalid JSON, the JSON media formatter will fail. No surprises there.

If you want to be able to handle another file format - such as the quasi-but-invalid-JSON examples you've posted above, you can always create a custom System.Net.Http.Formatting.MediaTypeFormatter.

yaakov
  • 5,552
  • 35
  • 48
  • why would one end up writing a custom mediatypeformatter, just to play around invalid json objects? – Amit Kumar Ghosh Apr 21 '16 at 09:38
  • Our test team wants to be able to write Acceptance Criteria with invalid JSON and have a known output. Example: Send Invalid JSON which doesn't parse, but if the empty string in question was replaced with a null, it would parse and return a Required Error. – Andrew Roberts Apr 21 '16 at 10:58
  • @AndrewRoberts I feel like there may be a communications breakdown between you and your test team. A missing value in JSON is not an empty string or a null. It's just invalid syntax. The explanation and examples you've given make no sense. – yaakov Apr 21 '16 at 11:23
  • If the missing (string) value is not the last key value pair, the JSON Serializer throws a parsing error and if the field is Required, it throws a Required error too. If not required, just throws the parsing error. If the missing (non string) value is not the last key value pair, the JSON Serializer throws a different parsing error and if the field is Required, it throws a Required error too. If not required, just throws the parsing error. If the missing value is the last key value pair, the JSON Serializer only throws a parsing error. – Andrew Roberts Apr 21 '16 at 22:56
  • (Continued) So, I can ignore these not last pair parsing errors, and just use the Required Error. I can catch the last pair parsing errors. – Andrew Roberts Apr 21 '16 at 22:58
0

What I ended up doing was calling a JSON Validator that I call at the beginning of the Model Validation.

I posted the code here. https://stackoverflow.com/a/36857080/284169

Community
  • 1
  • 1
Andrew Roberts
  • 990
  • 2
  • 12
  • 26