From: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 3 Sep 2021 00:13:52 +0000 (-0600) Subject: Pass a null getter\setter with [JsonIgnore] and src gen (#58572) X-Git-Tag: accepted/tizen/unified/20220110.054933~211 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=317dd69498a5b1f82622adbd1606c9430331dbd4;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Pass a null getter\setter with [JsonIgnore] and src gen (#58572) Co-authored-by: Steve Harter --- diff --git a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs index ca93f97..4b7dabd 100644 --- a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs +++ b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs @@ -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!" diff --git a/src/libraries/System.Text.Json/tests/Common/PropertyVisibilityTests.cs b/src/libraries/System.Text.Json/tests/Common/PropertyVisibilityTests.cs index 32f19eb..32189f4 100644 --- a/src/libraries/System.Text.Json/tests/Common/PropertyVisibilityTests.cs +++ b/src/libraries/System.Text.Json/tests/Common/PropertyVisibilityTests.cs @@ -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(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"; diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/Serialization/PropertyVisibilityTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/Serialization/PropertyVisibilityTests.cs index cc9b3d6..e3cc69e 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/Serialization/PropertyVisibilityTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/Serialization/PropertyVisibilityTests.cs @@ -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))]