8

I just moved to ASP.NET Core 3.0 and it seems that either model validation with inheritance is broken, or I am missing something. When I post the following model with IlVal00 = null, it says:

title=One or more validation errors occurred. status=400, The IlVal00 field is required.

public class Stock : BaseClass
{
    [Required]
    public string Ref { get; set; } = default!;
}

public class BaseClass
{
    public string? IlVal00 { get; set; }
}

But if I change my model to the following, it works:

public class Stock : BaseClass
{
     [Required]
     public string Ref { get; set; } = default!;

     public new string? IlVal00 { get; set; }
}

public class BaseClass
{
     public string? IlVal00 { get; set; }
}

But I need this to support inheritance. Does anyone have an idea for how to make this work?

Thanks.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Guiguisp
  • 91
  • 2
  • 7
  • use partial class instead of inheritance with same name of class – Mustufa Oct 08 '19 at 06:16
  • 1
    Well @Mustufa that would remove the whole point of inheritance... – Twenty Oct 08 '19 at 06:24
  • Anyway you should create an issue over on the [GitHub](https://github.com/aspnet/AspNetCore/issues) page, if you are sure that you can't fix it and it is a bug. – Twenty Oct 08 '19 at 06:27
  • 1
    @Twenty I reported the bug [here](https://github.com/aspnet/AspNetCore/issues/14812), thanks ! For now, I have disabled automatic model state validation with `services.Configure(options => { options.SuppressModelStateInvalidFilter = true; });` – Guiguisp Oct 08 '19 at 08:25
  • 1
    @Guiguisp try setting this ``MvcOptions.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true``. Then you can specify ``[Required]`` attribute explicitly. – pavinan Oct 08 '19 at 14:18
  • FYI: Thanks to @Guiguisp ([and others](https://github.com/dotnet/aspnetcore/issues/16654)) for [reporting this](https://github.com/dotnet/aspnetcore/issues/14812), the ASP.NET team was able to resolve this as part of the ASP.NET 3.1 release. – Jeremy Caney Mar 14 '20 at 21:56

2 Answers2

1

As I went through a very close scenario, I will add here an answer just in case others run into the same or similar issues:

In my case, the problem involved a project with .NET Core 3.1 as Target Framework and inheritance was not part of the problem. My project had a NuGet package dll as one of its dependencies and such package had several models. Our controller endpoints had these models as parameters. However, requests sent to any of these endpoints (with any of those models as body) were giving rise to validation errors for non-nullable reference type properties.

The workaround to solve it was similar to the one suggested here: https://github.com/dotnet/aspnetcore/issues/14812. I added the following option to AddControllers method, from Startup's ConfigureServices method:

services.AddControllers(options => options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true);

Note that, by using this workaround, it will be necessary to manually add [Required] attributes for nullable reference type properties around your application.

I also opened an issue on their repository: https://github.com/dotnet/aspnetcore/issues/27448

Pedro Coelho
  • 1,411
  • 3
  • 18
  • 31
0

try to use virtual like this:

public virtual string? IlVal00 { get; set; }
Derviş Kayımbaşıoğlu
  • 28,492
  • 4
  • 50
  • 72
  • 1
    Adding some commentary will go a long way to explain why this solves the OP's question. Just a code dump alone is not that helpful. – SiKing Sep 22 '20 at 20:53