Skip to content

Random model validation errors occur during POST request when Request.ContentLength exceeds 4096. #17029

@RomanTymchyshyn

Description

@RomanTymchyshyn

I get the following error, when executing my POST request:

{
  "errors": {
    "string[216]": [
      "Unexpected character encountered while parsing value: S. Path 'string[216]', line 1, position 3808."
    ],
    "string[216].string": [
      "The input was not valid."
    ]
  },
  "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "traceId": "00-a8a345166aff6f41a0b3b59526a20044-12f52af2db5e0748-00"
}

Sometimes error looks like this (request is similar, it just has more or fewer strings in it). See the request below.

{
  "errors": {
    "strings[216]": [
      "The input was not valid.",
      "Unexpected character encountered while parsing value: S. Path 'strings[216]', line 1, position 3810.",
      "Unexpected character encountered while parsing value: o. Path 'strings[216]', line 1, position 3810."
    ],
    "strings[216].strings": [
      "Infinite loop detected from error handling. Path 'strings[216]', line 1, position 3810."
    ]
  },
  "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "traceId": "00-d3e8019bfcf3fe4888488ab0438f3910-8626179ab7e6e94a-00"
}

I created the following sample controller and model to reproduce the issue:

[ApiController]
[Route("api/[controller]")]
public class TestController: ControllerBase
{
    [HttpPost("post")]
    public IActionResult Post(Model model)
    {
        return Ok();
    }
}

public class Model
{
    public string[] Strings { get; set; }
}

This is the request I use: https://jsonblob.com/faf7720e-0559-11ea-a4bb-cf0a978731bb
There is an array strings with 235 strings in it. The interesting part is that it works with 230 strings.
I noticed, that with 235 it exceeds 4096 ContentLength.
Do you have such limits anywhere?

However, if I do manual deserialization of request body it works fine:

[ApiController]
[Route("api/[controller]")]
public class TestController: ControllerBase
{
    [HttpPost("post")]
    public async Task<IActionResult> Post()
    {
        var model = await DeserializeInputStream<Model>(Request.Body);
        return Ok();
    }

    protected static async Task<T> DeserializeInputStream<T>(Stream body)
    {
        var jsonRequest = await new StreamReader(body).ReadToEndAsync();
        return JsonConvert.DeserializeObject<T>(jsonRequest);
    }
}

public class Model
{
    public string[] Strings { get; set; }
}

We use version 3.0.
Also, we use AddNewtonsoftJson and EnableBuffering.

Metadata

Metadata

Assignees

Labels

Needs: Author FeedbackThe author of this issue needs to respond in order for us to continue investigating this issue.area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions