? @$"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!"
}
[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
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";
[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))]
[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))]