'Nullable ->enabled, getter of [JsonIgnore] propety called when post

Hello I am trying the simple asp.net 6 code bellow.

When I set the project nullable setting to disable, and call person controller post method, the getter of Person.Surname isn't calling. That's OK. It's calling only the Person.Name getter.

When I set in project nullable setting to enable,and call person controller post method, the getter of Person.Surname is calling. And the getter of Person.Name is called two times.

Here is the reason from the stack trace: \TestNet6.WebApplication\Person.cs:line 30 at Microsoft.Extensions.Internal.PropertyHelper.CallNullSafePropertyGetter[TDeclaringType,TValue](Func`2 getter, Object target) at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.DefaultComplexObjectValidationStrategy.Enumerator.<>c__DisplayClass13_1.b__1() at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationEntry.get_Model() at Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ValidationVisitor.VisitChildren(IValidationStrategy strategy) ....

When i set nullable to disable then is no getter, CallNullSafePropertyGetter, and model validation called.

That means,the project nullable enable/disable setting change application behavior at runtime.

repository: https://github.com/PetoLuc/TestNet6

controller:

[ApiController]
[Route("[controller]/[action]")]
public class PersonController : ControllerBase
{       
    [HttpPost]
    public void Post(Person person)
    {

    }   
}

model:

public class Person
{           
    private string? name;       
    private string? surname;

    public string? Name 
    {
        get
        {
            Trace.WriteLine("get name is calling");
            return name;
        }
        set 
        {
            Trace.WriteLine("set name is calling");
            name = value;
        }
    }

    [JsonIgnore]        
    public string? Surname 
    {
        get
        {
            Trace.WriteLine("get surname is calling");
            return surname;
        }
        set
        {
            Trace.WriteLine("set surname is calling");
            surname = value;
        }
    }
}

client:

await new HttpClient().PostAsJsonAsync("http://localhost:5032/person/post", new Dol.Person { Name = "test" });              


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source