Skip to content

Commit

Permalink
fix(76268): Fix invalid behavior for ignored properties with "new" mo…
Browse files Browse the repository at this point in the history
…difier of derived class.
  • Loading branch information
Maksim Golev authored and eiriktsarpalis committed Jun 5, 2023
1 parent 2bf8f1a commit 5f6670a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ private static void CacheMember(
if (propGenSpec.DefaultIgnoreCondition == JsonIgnoreCondition.Always)
{
ignoredMembers ??= new();
ignoredMembers.Add(propGenSpec.MemberName, memberInfo);
ignoredMembers[propGenSpec.MemberName] = memberInfo;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1405,7 +1405,8 @@ public void AddPropertyWithConflictResolution(JsonPropertyInfo jsonPropertyInfo,

if (jsonPropertyInfo.IsIgnored)
{
(state.IgnoredProperties ??= new()).Add(memberName, jsonPropertyInfo);
state.IgnoredProperties ??= new();
state.IgnoredProperties[memberName] = jsonPropertyInfo;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,22 @@ public async Task Ignore_BasePublicPropertyIgnored_ConflictWithDerivedPrivate()
Assert.Equal("NewDefaultValue", ((ClassWithIgnoredPublicPropertyAndNewSlotPrivate)obj).MyString);
}

[Fact]
public async void Ignore_BasePublicPropertyIgnored_ConflictWithDerivedPublicPropertyIgnored()
{
var obj = new ClassWithIgnoredPublicPropertyAndNewSlotPublicAndIgnoredToo();

string json = await Serializer.SerializeWrapper(obj);

Assert.Equal(@"{}", json);

json = @"{""MyString"":""NewValue""}";
obj = await Serializer.DeserializeWrapper<ClassWithIgnoredPublicPropertyAndNewSlotPublicAndIgnoredToo>(json);

Assert.Equal("DefaultValue", ((ClassWithIgnoredPublicProperty)obj).MyString);
Assert.Equal("NewDefaultValue", obj.MyString);
}

[Fact]
public async Task Ignore_VerifyNoReferenceToGetterAndSetter()
{
Expand Down Expand Up @@ -812,6 +828,12 @@ public class ClassWithIgnoredPublicPropertyAndNewSlotPrivate : ClassWithIgnoredP
internal new string MyString { get; set; } = "NewDefaultValue";
}

public class ClassWithIgnoredPublicPropertyAndNewSlotPublicAndIgnoredToo : ClassWithIgnoredPublicProperty
{
[JsonIgnore]
public new string MyString { get; set; } = "NewDefaultValue";
}

public class ClassWithIgnoredPropertyNamingConflictPrivate
{
[JsonIgnore]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public override async Task HonorJsonPropertyName_PrivateSetter()
[JsonSerializable(typeof(Class_PropertyWith_ProtectedInitOnlySetter))]
[JsonSerializable(typeof(ClassWithIgnoredPublicProperty))]
[JsonSerializable(typeof(ClassWithIgnoredPublicPropertyAndNewSlotPrivate))]
[JsonSerializable(typeof(ClassWithIgnoredPublicPropertyAndNewSlotPublicAndIgnoredToo))]
[JsonSerializable(typeof(ClassWithIgnoredPropertyPolicyConflictPublic))]
[JsonSerializable(typeof(ClassWithIgnoredPropertyNamingConflictPrivate))]
[JsonSerializable(typeof(ClassWithIgnoredNewSlotProperty))]
Expand Down Expand Up @@ -419,6 +420,7 @@ public void PublicContextAndJsonSerializerOptions()
[JsonSerializable(typeof(Class_PropertyWith_ProtectedInitOnlySetter))]
[JsonSerializable(typeof(ClassWithIgnoredPublicProperty))]
[JsonSerializable(typeof(ClassWithIgnoredPublicPropertyAndNewSlotPrivate))]
[JsonSerializable(typeof(ClassWithIgnoredPublicPropertyAndNewSlotPublicAndIgnoredToo))]
[JsonSerializable(typeof(ClassWithIgnoredPropertyPolicyConflictPublic))]
[JsonSerializable(typeof(ClassWithIgnoredPropertyNamingConflictPrivate))]
[JsonSerializable(typeof(ClassWithIgnoredNewSlotProperty))]
Expand Down

0 comments on commit 5f6670a

Please sign in to comment.