diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/NullabilityInfoContext.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/NullabilityInfoContext.cs index b523dcaf94f72..ea66d630c89dc 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/NullabilityInfoContext.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/NullabilityInfoContext.cs @@ -191,6 +191,10 @@ public NullabilityInfo Create(PropertyInfo propertyInfo) CheckNullabilityAttributes(nullability, getter.ReturnParameter.GetCustomAttributesData()); } + else + { + nullability.ReadState = NullabilityState.Unknown; + } if (setter != null) { @@ -201,6 +205,10 @@ public NullabilityInfo Create(PropertyInfo propertyInfo) CheckNullabilityAttributes(nullability, setter.GetParameters()[0].GetCustomAttributesData()); } + else + { + nullability.WriteState = NullabilityState.Unknown; + } return nullability; } diff --git a/src/libraries/System.Runtime/tests/System/Reflection/NullabilityInfoContextTests.cs b/src/libraries/System.Runtime/tests/System/Reflection/NullabilityInfoContextTests.cs index f027e09c2245c..07f244e9172f7 100644 --- a/src/libraries/System.Runtime/tests/System/Reflection/NullabilityInfoContextTests.cs +++ b/src/libraries/System.Runtime/tests/System/Reflection/NullabilityInfoContextTests.cs @@ -72,6 +72,7 @@ public void EventTest(string eventName, NullabilityState readState, NullabilityS public static IEnumerable PropertyTestData() { yield return new object[] { "PropertyNullable", NullabilityState.Nullable, NullabilityState.Nullable, typeof(TypeWithNotNullContext) }; + yield return new object[] { "PropertyNullableReadOnly", NullabilityState.Nullable, NullabilityState.Unknown, typeof(TypeWithNotNullContext) }; yield return new object[] { "PropertyUnknown", NullabilityState.Unknown, NullabilityState.Unknown, typeof(string) }; yield return new object[] { "PropertyNonNullable", NullabilityState.NotNull, NullabilityState.NotNull, typeof(NullabilityInfoContextTests) }; yield return new object[] { "PropertyValueTypeUnknown", NullabilityState.NotNull, NullabilityState.NotNull, typeof(short) }; @@ -100,7 +101,10 @@ public void PropertyTest(string propertyName, NullabilityState readState, Nullab Assert.Equal(readState, nullability.ReadState); Assert.Equal(readState, nullabilityContext.Create(property.GetMethod.ReturnParameter).ReadState); Assert.Equal(writeState, nullability.WriteState); - Assert.Equal(writeState, nullabilityContext.Create(property.SetMethod.GetParameters()[0]).WriteState); + if (property.SetMethod != null) + { + Assert.Equal(writeState, nullabilityContext.Create(property.SetMethod.GetParameters()[0]).WriteState); + } Assert.Equal(type, nullability.Type); Assert.Empty(nullability.GenericTypeArguments); Assert.Null(nullability.ElementType); @@ -116,7 +120,7 @@ public static IEnumerable ArrayPropertyTestData() } [Theory] - [SkipOnMono("Temporarily disable on Mono")] + [SkipOnMono("Disabling NullablePublicOnly feature not work for Mono tests")] [MemberData(nameof(ArrayPropertyTestData))] public void ArrayPropertyTest(string propertyName, NullabilityState elementState, NullabilityState propertyState) { @@ -138,7 +142,7 @@ public static IEnumerable GenericArrayPropertyTestData() } [Theory] - [SkipOnMono("Temporarily disable on Mono")] + [SkipOnMono("Disabling NullablePublicOnly feature not work for Mono tests")] [MemberData(nameof(GenericArrayPropertyTestData))] public void GenericArrayPropertyTest(string propertyName, NullabilityState elementState, NullabilityState propertyState) { @@ -162,7 +166,7 @@ public static IEnumerable JaggedArrayPropertyTestData() } [Theory] - [SkipOnMono("Temporarily disable on Mono")] + [SkipOnMono("Disabling NullablePublicOnly feature not work for Mono tests")] [MemberData(nameof(JaggedArrayPropertyTestData))] public void JaggedArrayPropertyTest(string propertyName, NullabilityState innermodtElementState, NullabilityState elementState, NullabilityState propertyState) { @@ -187,7 +191,7 @@ public static IEnumerable TuplePropertyTestData() } [Theory] - [SkipOnMono("Temporarily disable on Mono")] + [SkipOnMono("Disabling NullablePublicOnly feature not work for Mono tests")] [MemberData(nameof(TuplePropertyTestData))] public void TuplePropertyTest(string propertyName, NullabilityState genericParam1, NullabilityState genericParam2, NullabilityState genericParam3, NullabilityState propertyState) { @@ -212,7 +216,7 @@ public static IEnumerable GenericTuplePropertyTestData() } [Theory] - [SkipOnMono("Temporarily disable on Mono")] + [SkipOnMono("Disabling NullablePublicOnly feature not work for Mono tests")] [MemberData(nameof(GenericTuplePropertyTestData))] public void GenericTuplePropertyTest(string propertyName, NullabilityState genericParam1, NullabilityState genericParam2, NullabilityState genericParam3, NullabilityState propertyState) { @@ -238,7 +242,7 @@ public static IEnumerable DictionaryPropertyTestData() } [Theory] - [SkipOnMono("Temporarily disable on Mono")] + [SkipOnMono("Disabling NullablePublicOnly feature not work for Mono tests")] [MemberData(nameof(DictionaryPropertyTestData))] public void DictionaryPropertyTest(string propertyName, NullabilityState keyState, NullabilityState valueElement, NullabilityState valueState, NullabilityState propertyState) { @@ -264,7 +268,7 @@ public static IEnumerable GenericDictionaryPropertyTestData() } [Theory] - [SkipOnMono("Temporarily disable on Mono")] + [SkipOnMono("Disabling NullablePublicOnly feature not work for Mono tests")] [MemberData(nameof(GenericDictionaryPropertyTestData))] public void GenericDictionaryPropertyTest(string propertyName, NullabilityState keyState, NullabilityState valueElement, NullabilityState valueState, NullabilityState propertyState) { @@ -428,6 +432,7 @@ public void GenericStructConstraintTest(string fieldName, NullabilityState readS } [Fact] + [SkipOnMono("Nullability attributes trimmed on Mono")] public void GenericListTest() { Type listNullable = typeof(List); @@ -450,6 +455,7 @@ public void GenericListTest() } [Fact] + [SkipOnMono("Nullability attributes trimmed on Mono")] public void GenericListAndDictionaryFieldTest() { Type typeNullable = typeof(GenericTest); @@ -500,7 +506,7 @@ public static IEnumerable MethodReturnParameterTestData() } [Theory] - [SkipOnMono("Temporarily disable on Mono")] + [SkipOnMono("Disabling NullablePublicOnly feature not work for Mono tests")] [MemberData(nameof(MethodReturnParameterTestData))] public void MethodReturnParameterTest(string methodName, NullabilityState elementState, NullabilityState readState) { @@ -525,7 +531,7 @@ public static IEnumerable MethodGenericReturnParameterTestData() } [Theory] - [SkipOnMono("Temporarily disable on Mono")] + [SkipOnMono("Disabling NullablePublicOnly feature not work for Mono tests")] [MemberData(nameof(MethodGenericReturnParameterTestData))] public void MethodGenericReturnParameterTest(string methodName, NullabilityState readState, NullabilityState elementState) { @@ -548,7 +554,7 @@ public static IEnumerable MethodParametersTestData() } [Theory] - [SkipOnMono("Temporarily disable on Mono")] + [SkipOnMono("Disabling NullablePublicOnly feature not work for Mono tests")] [MemberData(nameof(MethodParametersTestData))] public void MethodParametersTest(string methodName, NullabilityState stringState, NullabilityState dictKey, NullabilityState dictValueElement, NullabilityState dictValue, NullabilityState dictionaryState) { @@ -572,7 +578,7 @@ public static IEnumerable MethodGenericParametersTestData() } [Theory] - [SkipOnMono("Temporarily disable on Mono")] + [SkipOnMono("Disabling NullablePublicOnly feature not work for Mono tests")] [MemberData(nameof(MethodGenericParametersTestData))] public void MethodGenericParametersTest(string methodName, NullabilityState param1State, NullabilityState dictKey, NullabilityState dictValue, NullabilityState dictionaryState) { @@ -594,7 +600,7 @@ public static IEnumerable StringTypeTestData() } [Theory] - [SkipOnMono("Temporarily disable on Mono")] + [SkipOnMono("Nullability attributes trimmed on Mono")] [MemberData(nameof(StringTypeTestData))] public void NullablePublicOnlyStringTypeTest(string methodName, NullabilityState param1State, NullabilityState param2State, NullabilityState param3State, Type[] types) { @@ -612,6 +618,7 @@ public void NullablePublicOnlyStringTypeTest(string methodName, NullabilityState } [Fact] + [SkipOnMono("Nullability attributes trimmed on Mono")] public void NullablePublicOnlyOtherTypesTest() { Type type = typeof(Type); @@ -628,7 +635,7 @@ public void NullablePublicOnlyOtherTypesTest() PropertyInfo publicNullableProperty = type.GetProperty("DeclaringType", flags)!; info = nullabilityContext.Create(publicNullableProperty); Assert.Equal(NullabilityState.Nullable, info.ReadState); - Assert.Equal(NullabilityState.Nullable, info.WriteState); + Assert.Equal(NullabilityState.Unknown, info.WriteState); PropertyInfo publicGetPrivateSetNullableProperty = typeof(FileSystemEntry).GetProperty("Directory", flags)!; info = nullabilityContext.Create(publicGetPrivateSetNullableProperty); @@ -828,6 +835,7 @@ public class TypeWithNotNullContext public void MethodParametersUnknown(string s, IDictionary dict) { } #nullable enable public TypeWithNotNullContext? PropertyNullable { get; set; } + public TypeWithNotNullContext? PropertyNullableReadOnly { get; } private NullabilityInfoContextTests PropertyNonNullable { get; set; } = null!; internal float PropertyValueType { get; set; } protected long? PropertyValueTypeNullable { get; set; }