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
{{ message }}
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
We were upgrading our project to RC1 and noticed model validation stopped working after the upgrade. The short version is that having a CancellationToken in your action method will cause all models to get marked with ModelValidationState.Skipped.
Here's a simple setup that demonstrates the problem:
[Route("api/foo")]
public class FooController : Controller
{
[Route("with-cancellation")]
[HttpPost]
public async Task<IActionResult> CreateWithCancellation([FromBody] Foo foo, CancellationToken cancellationToken)
{
if (!ModelState.IsValid)
return new BadRequestResult();
await DoStuff();
return Ok(foo);
}
[Route("without-cancellation")]
[HttpPost]
public async Task<IActionResult> CreateWithoutCancellation([FromBody] Foo foo)
{
if (!ModelState.IsValid)
return new BadRequestResult();
await DoStuff();
return Ok(foo);
}
public Task DoStuff()
{
return Task.FromResult(0);
}
}
public class Foo
{
[Required]
public string requiredProperty { get; set; }
}
If you post an invalid model to the without-cancellation, it will properly return a 400. If you post an invalid model to with-cancellation it will return a 200 with an invalid model.
After debugging through the problem, we found that the ValidationVisitor for the cancellation token was overwriting all of the model validation results to ModelValidationResult.Skipped.
In the CancellationToken case, it is falling through to the else block which sets ModelName to string.Empty when it really should be the old binding context's name.