ModelState validation changes when adding a new property to an existing model #41687
Closed
1 task done
Labels
feature-model-binding
old-area-web-frameworks-do-not-use
*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Is there an existing issue for this?
Describe the bug
Adding a new property to an existing model changes the required-ness of another existing (unmodified) property when nullable reference types are enabled. As a result, the outcome of ModelState validation changes.
Expected Behavior
Adding a new property should not change the validation of another existing (unmodified) property.
Steps To Reproduce
Controllers\WeatherForecastController.cs
:{}
and click Execute.Version
property from the first code snippet.Exceptions (if any)
No response
.NET Version
6.0.203
Anything else?
I traced this down to DataAnnotationsMetadataProvider. Uncommenting the
Version
property makes the C# compiler switch from[NullableAttribute]
to[NullableContextAttribute]
, causing the metadata provider to bail out early due to the use of generics here (or gets it wrong here, I don't remember exactly).This appears to be fixed in #39219. So I've tested with .NET 7 Preview4 and can confirm the problem no longer occurs there.
Feel free to close this issue, as the problem is already solved. I just thought it would be good to log this for awareness and traceability.
I don't think it makes sense to backport this to .NET 6, because
NullabilityInfoContext
doesn't behave correctly in the .NET 6 runtime version:The text was updated successfully, but these errors were encountered: