dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.21k stars 9.95k forks source link

Model validation causing stack overflow exception #2024

Closed IainmXray closed 6 years ago

IainmXray commented 7 years ago

I am facing issues with the model validation in aspnet.core 1.1.1 throwing a System.InsufficientExecutionStackException when processing an incoming Json payload. I have narrowed this down to the fact that I have a System.Data.Entity.Spatial.DBGeography object in the model. A cut down example for recreation:

Framework version "frameworks": { "net452": { "imports": [ "dotnet5.4", "portable-net451+win8" ] } }

Example Controller ` [Route("api/v1/[controller]")] public class TestController : Controller {

    [HttpPut()]
    public IActionResult Put([FromBody] Test entity)
    {
        if (!ModelState.IsValid || entity == null)
            return BadRequest(ModelState);
        return Ok(entity);
    }

}`

Example Entity ` public class Test {

    [JsonIgnore]
    public DbGeography Location2 { get; set; } = DbGeography.FromText("POINT(1 1)");

}`

Exception thrown (cut down for brevity) System.InsufficientExecutionStackException: Insufficient stack to continue executing the program safely. This can happen from having too many functions on the call stack or function on the stack using too much stack space. at System.Runtime.CompilerServices.RuntimeHelpers.EnsureSufficientExecutionStack() at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.Visit(ModelMetadata metadata, String key, Object model) at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitChildren(IValidationStrategy strategy) at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitComplexType() at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.Visit(ModelMetadata metadata, String key, Object model) at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitChildren(IValidationStrategy strategy) at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitComplexType() at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.Visit(ModelMetadata metadata, String key, Object model) at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitChildren(IValidationStrategy strategy) at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitComplexType()

Is there any way to intercept the model validation process? I tried creating a NoFormat attribute using the documentation at https://docs.microsoft.com/en-us/aspnet/core/mvc/models/validation but this was never called before the stack overflow.

rynowak commented 7 years ago

I'm not sure what [NoFormat] is - I have never heard of that before.

If there's no meaningful validation to do on this type, I would suggest adding it to the list of types for which we don't validate their properties.

services.AddMvc(options =>
{
    options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(DbGeography)));
});
aspnet-hello commented 6 years ago

This issue is being closed because it has not been updated in 3 months.

We apologize if this causes any inconvenience. We ask that if you are still encountering this issue, please log a new issue with updated information and we will investigate.

jdmallen commented 6 years ago

I ran into this same exact error. I'll create a new ticket.