-
-
Notifications
You must be signed in to change notification settings - Fork 492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WithEnforceNullabilty() has unpredictable behavior with classes having both nullable reference type and value type #1018
Comments
Update, the example will give an error even when I use the exact setup as in one of the tests, but with a
Result:
I think this is because way the compiler generate |
I’m not at a computer for a bit. But. In your second example, what happens when you put a non nullable, no question mark, string in there? What is generated? I wonder if there’s an optimization where if all properties/members are nullable then it only puts the nullable attributes at the class level and not on individual properties and fields. That’s just a guess though. |
I guess you might be right,
|
@EdwardCooke did you have any time to look at this? |
Thanks for the reminder. I just did some digging. Looks like there is some optimization that happens with nullable properties at the compiler level. I have implemented the required changes so we should be good once I cut a release for it. |
Describe the bug
When deserializing to a class with both nullable reference and value types members, YamlDotNet considers the nullabe value type to not accept null values.
I suspect it's coming from the reflection checks only checking if a member allows null using the presence of
NullableAttribute
, which fails if it's aTValueType?
(this won't generate aNullableAttribute
). But I've seen strange cases where the compiler doesn't even generateNullableAttribute
on a reference type, so I won't be too sure about that.To Reproduce
Using latest version: 16.2.1
Create a new console project with .net8
Program.cs
Result:
Unhandled exception. (Line: 2, Col: 1, Idx: 2) - (Line: 2, Col: 11, Idx: 12): Strict nullability enforcement error.
The text was updated successfully, but these errors were encountered: