From 72d68e5ad106bbd05127b5d50b551767f8a8cdad Mon Sep 17 00:00:00 2001 From: Steve Harter Date: Mon, 30 Aug 2021 18:09:10 -0500 Subject: [PATCH] Pass a null getter\setter with [JsonIgnore] and src gen --- .../gen/JsonSourceGenerator.Emitter.cs | 6 +++-- .../tests/Common/PropertyVisibilityTests.cs | 25 +++++++++++++++++++ .../Serialization/PropertyVisibilityTests.cs | 4 +++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs index ca93f97e70585e..4b7dabd4e605f2 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 string GeneratePropMetadataInitFunc(TypeGenerationSpec typeGenerationSpe ? @$"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 32f19eb120c39c..32189f4beb462b 100644 --- a/src/libraries/System.Text.Json/tests/Common/PropertyVisibilityTests.cs +++ b/src/libraries/System.Text.Json/tests/Common/PropertyVisibilityTests.cs @@ -241,6 +241,24 @@ public async Task Ignore_BasePublicPropertyIgnored_ConflictWithDerivedPrivate() Assert.Equal("NewDefaultValue", ((ClassWithIgnoredPublicPropertyAndNewSlotPrivate)obj).MyString); } + [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() { @@ -782,6 +800,13 @@ public class ClassWithIgnoredPublicProperty 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 cc9b3d617107cc..e3cc69ede77eb0 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 @@ public override async Task HonorJsonPropertyName() [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 @@ public override async Task JsonIgnoreCondition_WhenWritingNull_OnValueType_Fail_ [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))]