Fix KeyValuePair nullability detection on Mono #120910
Open
+19
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix KeyValuePair nullability detection on Mono
Summary
Fixes incorrect nullability detection for
KeyValuePair<TKey, TValue>
generic parameters on Mono, which was causing ASP.NET Core DataAnnotations tests to fail.Problem
On Mono,
KeyValuePair<TKey, TValue>
generic parameters (TKey
andTValue
) were being detected asNotNull
instead ofNullable
, while CoreCLR correctly detects them asNullable
.Root Cause: Mono's
KeyValuePair<TKey, TValue>
generic parameters are missing theNullableAttribute(2)
that CoreCLR has. This causesTryUpdateGenericParameterNullability
to returnfalse
and fall back toNullableContextAttribute(1)
=NotNull
.Failing Tests
Microsoft.AspNetCore.Mvc.DataAnnotations.DataAnnotationsMetadataProviderTest.IsNullableReferenceType_ReturnsFalse_ForKeyValuePairWithoutNullableConstraints
Microsoft.AspNetCore.Mvc.DataAnnotations.DataAnnotationsMetadataProviderTest.IsNullableReferenceType_ReturnsTrue_ForKeyValuePairWithNullableConstraints
Solution
Added special handling in
TryUpdateGenericParameterNullability
to treatKeyValuePair<TKey, TValue>
generic parameters as nullable by default when nullable attributes are missing.The fix:
System.Collections.Generic.KeyValuePair
2`ReadState
andWriteState
toNullable
Testing
Reproduction Case
Test Results
Files Changed
src/libraries/System.Private.CoreLib/src/System/Reflection/NullabilityInfoContext.cs
Verification
The fix has been tested with the full ASP.NET Core DataAnnotations test suite and successfully resolves the failing tests without introducing regressions.
cc: @giritrivedi