Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.net Core API : ValidationVisitor exceeded the maximum configured validation depth '32'

AS am running an ASP.Net core web API from docker container , it throws a validation error :

System.InvalidOperationException: ValidationVisitor exceeded the maximum configured validation depth '32' when validating type 'ClassName'. This may indicate a very deep or infinitely recursive object graph. Consider modifying 'MvcOptions.MaxValidationDepth' or suppressing validation on the model type.

The only place I could find a discussion about this issue is in here , where it seems that a fix has been provided on the latest version of ASP.net core . I updated my .net core version to the latest , but still facing the same issue.
Here is the code of the class where the validation is causing the issue :

        [Required]
        [Range(1, long.MaxValue)]
        public long Id { get; set; }
        [Required(AllowEmptyStrings = false)]
        [StringLength(1000)]
        public string Name { get; set; }
        [Required(AllowEmptyStrings = false)]
        [StringLength(200)]
        public string Category { get; set; }
        [Required(AllowEmptyStrings = false)]
        [StringLength(13)]
        public string Division { get; set; }

Important : Am the only one facing the issue , as the rest of my team is running the project successfully , any help is highly appreciated.

like image 341
Ferhi Malek Avatar asked Jul 27 '20 09:07

Ferhi Malek


4 Answers

It is a bug in Asp.Net Core ModelBinding Validation affecting MVC and Web Api https://github.com/dotnet/aspnetcore/issues/13778

One workaround is to increase MaxModelValidationErrors in Startup.ConfigureServices:

services.AddMvc()
    .AddMvcOptions(options => {
    options.MaxModelValidationErrors = 999999;
})
like image 162
Wagner Avatar answered Oct 18 '22 19:10

Wagner


Increasing MaxModelValidationErrors didn't fix things for me, I had to change a different value (MaxValidationDepth) to get things to work. Wanted to add it here in case anyone had the same problem as I did.

.AddMvcOptions(options =>
        {
            options.MaxValidationDepth = 999;
        });
like image 6
Christopher Rice Avatar answered Oct 18 '22 19:10

Christopher Rice


You could increase MaxModelValidationErrors in Startup.ConfigureServices:

services.AddControllers(options =>
{
    options.MaxModelValidationErrors = 999999;
});
like image 2
Konstantin Nikolsky Avatar answered Oct 18 '22 19:10

Konstantin Nikolsky


I cannot recommend SuppressChildValidationMetadataProvider anymore.

But you can use this attribute:

using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
[ValidateNever]
public List<ToNotValidateSet> ToNotValidateSet { get; set; }

...on all the virtual properties that should not get validated.

like image 1
CodingYourLife Avatar answered Oct 18 '22 19:10

CodingYourLife