Skip to content

Commit

Permalink
Fixed handling of public hidden prop by new slot
Browse files Browse the repository at this point in the history
  • Loading branch information
YohDeadfall committed Apr 30, 2020
1 parent 8f9653b commit 335c9d0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public JsonClassInfo(Type type, JsonSerializerOptions options)
if (propertyInfo.GetMethod?.IsPublic == true ||
propertyInfo.SetMethod?.IsPublic == true)
{
JsonPropertyInfo jsonPropertyInfo = AddProperty(propertyInfo.PropertyType, propertyInfo, type, options);
JsonPropertyInfo jsonPropertyInfo = AddProperty(propertyInfo.PropertyType, propertyInfo, currentType, options);
Debug.Assert(jsonPropertyInfo != null && jsonPropertyInfo.NameAsString != null);

// If the JsonPropertyNameAttribute or naming policy results in collisions, throw an exception.
Expand All @@ -140,11 +140,12 @@ public JsonClassInfo(Type type, JsonSerializerOptions options)
// Overwrite the one just added since it has [JsonIgnore].
cache[jsonPropertyInfo.NameAsString] = jsonPropertyInfo;
}
else if (jsonPropertyInfo.ShouldDeserialize == true || jsonPropertyInfo.ShouldSerialize == true)
else if (other.PropertyInfo?.Name != jsonPropertyInfo.PropertyInfo?.Name &&
(jsonPropertyInfo.ShouldDeserialize == true || jsonPropertyInfo.ShouldSerialize == true))
{
ThrowHelper.ThrowInvalidOperationException_SerializerPropertyNameConflict(Type, jsonPropertyInfo);
}
// else ignore jsonPropertyInfo since it has [JsonIgnore].
// else ignore jsonPropertyInfo since it has [JsonIgnore] or it's hidden by a new slot.
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,22 @@ public static void Serialize_public_property_on_conflict_with_private_due_to_pol
Assert.Equal("ConflictingValue", obj.myString);
}

[Fact]
public static void Serealize_new_slot_public_property_without_conflict_with_base_public_property()
{
// Serialize
var obj = new ClassWithNewSlotDecimalProperty();
string json = JsonSerializer.Serialize(obj);

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

// Deserialize
json = @"{""MyNumeric"":2.5}";
obj = JsonSerializer.Deserialize<ClassWithNewSlotDecimalProperty>(json);

Assert.Equal(2.5M, obj.MyNumeric);
}

[Fact]
public static void Ignore_non_public_property()
{
Expand Down Expand Up @@ -372,6 +388,16 @@ public class ClassWithIgnoredPropertyPolicyConflict
internal string myString { get; set; } = "ConflictingValue";
}

public class ClassWithHiddenByNewSlotIntProperty
{
public int MyNumeric { get; set; } = 1;
}

public class ClassWithNewSlotDecimalProperty : ClassWithHiddenByNewSlotIntProperty
{
public new decimal MyNumeric { get; set; } = 1.5M;
}

[Fact]
public static void NoSetter()
{
Expand Down

0 comments on commit 335c9d0

Please sign in to comment.