Pass a null getter\setter with [JsonIgnore] and src gen (#58572)
authorgithub-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Fri, 3 Sep 2021 00:13:52 +0000 (18:13 -0600)
committerGitHub <noreply@github.com>
Fri, 3 Sep 2021 00:13:52 +0000 (18:13 -0600)
Co-authored-by: Steve Harter <steveharter@users.noreply.github.com>
src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs
src/libraries/System.Text.Json/tests/Common/PropertyVisibilityTests.cs
src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/Serialization/PropertyVisibilityTests.cs

index ca93f97..4b7dabd 100644 (file)
@@ -683,12 +683,14 @@ private static {JsonPropertyInfoTypeRef}[] {propInitMethodName}({JsonSerializerC
                         ? @$"jsonPropertyName: ""{memberMetadata.JsonPropertyName}"""
                         : "jsonPropertyName: null";
 
-                    string getterNamedArg = memberMetadata.CanUseGetter
+                    string getterNamedArg = memberMetadata.CanUseGetter &&
+                        memberMetadata.DefaultIgnoreCondition != JsonIgnoreCondition.Always
                         ? $"getter: static (obj) => (({declaringTypeCompilableName})obj).{clrPropertyName}"
                         : "getter: null";
 
                     string setterNamedArg;
-                    if (memberMetadata.CanUseSetter)
+                    if (memberMetadata.CanUseSetter &&
+                        memberMetadata.DefaultIgnoreCondition != JsonIgnoreCondition.Always)
                     {
                         string propMutation = typeGenerationSpec.IsValueType
                             ? @$"{UnsafeTypeRef}.Unbox<{declaringTypeCompilableName}>(obj).{clrPropertyName} = value!"
index 32f19eb..32189f4 100644 (file)
@@ -242,6 +242,24 @@ namespace System.Text.Json.Serialization.Tests
         }
 
         [Fact]
+        public async Task Ignore_VerifyNoReferenceToGetterAndSetter()
+        {
+            // Serialize
+            var obj = new ClassWithObsoleteAndIgnoredProperty();
+            string json = await JsonSerializerWrapperForString.SerializeWrapper(obj);
+
+            Assert.Equal(@"{}", json);
+
+            // Deserialize
+            json = @"{""MyString_Obsolete"":""NewValue""}";
+            obj = await JsonSerializerWrapperForString.DeserializeWrapper<ClassWithObsoleteAndIgnoredProperty>(json);
+
+#pragma warning disable CS0618 // Type or member is obsolete
+            Assert.Equal("DefaultValue", obj.MyString_Obsolete);
+#pragma warning restore CS0618 // Type or member is obsolete
+        }
+
+        [Fact]
         public async Task Ignore_PublicProperty_ConflictWithPrivateDueAttributes()
         {
             // Serialize
@@ -782,6 +800,13 @@ namespace System.Text.Json.Serialization.Tests
             public string MyString { get; set; } = "DefaultValue";
         }
 
+        public class ClassWithObsoleteAndIgnoredProperty
+        {
+            [Obsolete("Src gen should not generate reference to getter or setter")]
+            [JsonIgnore]
+            public string MyString_Obsolete { get; set; } = "DefaultValue";
+        }
+
         public class ClassWithIgnoredPublicPropertyAndNewSlotPrivate : ClassWithIgnoredPublicProperty
         {
             internal new string MyString { get; set; } = "NewDefaultValue";
index cc9b3d6..e3cc69e 100644 (file)
@@ -161,10 +161,12 @@ namespace System.Text.Json.SourceGeneration.Tests
         [JsonSerializable(typeof(StructWithBadIgnoreAttribute))]
         [JsonSerializable(typeof(Class_PropertyWith_InternalInitOnlySetter))]
         [JsonSerializable(typeof(Class_PropertyWith_ProtectedInitOnlySetter))]
+        [JsonSerializable(typeof(ClassWithIgnoredPublicProperty))]
         [JsonSerializable(typeof(ClassWithIgnoredPublicPropertyAndNewSlotPrivate))]
         [JsonSerializable(typeof(ClassWithIgnoredPropertyPolicyConflictPublic))]
         [JsonSerializable(typeof(ClassWithIgnoredPropertyNamingConflictPrivate))]
         [JsonSerializable(typeof(ClassWithIgnoredNewSlotProperty))]
+        [JsonSerializable(typeof(ClassWithObsoleteAndIgnoredProperty))]
         [JsonSerializable(typeof(ClassWithPublicGetterAndPrivateSetter))]
         [JsonSerializable(typeof(ClassWithInitializedProps))]
         [JsonSerializable(typeof(ClassWithNewSlotInternalProperty))]
@@ -324,10 +326,12 @@ namespace System.Text.Json.SourceGeneration.Tests
         [JsonSerializable(typeof(StructWithBadIgnoreAttribute))]
         [JsonSerializable(typeof(Class_PropertyWith_InternalInitOnlySetter))]
         [JsonSerializable(typeof(Class_PropertyWith_ProtectedInitOnlySetter))]
+        [JsonSerializable(typeof(ClassWithIgnoredPublicProperty))]
         [JsonSerializable(typeof(ClassWithIgnoredPublicPropertyAndNewSlotPrivate))]
         [JsonSerializable(typeof(ClassWithIgnoredPropertyPolicyConflictPublic))]
         [JsonSerializable(typeof(ClassWithIgnoredPropertyNamingConflictPrivate))]
         [JsonSerializable(typeof(ClassWithIgnoredNewSlotProperty))]
+        [JsonSerializable(typeof(ClassWithObsoleteAndIgnoredProperty))]
         [JsonSerializable(typeof(ClassWithPublicGetterAndPrivateSetter))]
         [JsonSerializable(typeof(ClassWithInitializedProps))]
         [JsonSerializable(typeof(ClassWithNewSlotInternalProperty))]