-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed serialization of hidden base class members #32107
Conversation
ad02d69
to
19c48b1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a few more test cases to consider.
src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonClassInfo.cs
Outdated
Show resolved
Hide resolved
@@ -11,6 +11,231 @@ namespace System.Text.Json.Serialization.Tests | |||
{ | |||
public static class PropertyVisibilityTests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can you use PascalCase
for the tests names where possible and capitalize the first letter after an underscore where needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a naming used by EF Core for long test names, but am okay with changing this if you want, but first compare names.
Before: Serialize_base_public_property_on_conflict_with_derived_private
After: SerializeBasePublicPropertyOnConflictWithDerivedPrivate
The first one is more readable while the second looks like encoded data and is hard to read. So are you sure that pascal case is better? I can rename other tests and give them meaningful names instead of current (:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do underscore-delimited pascal case chunks in such cases, e.g. Serialize_BasePublicProperty_OnConflictWithDerivedPrivate
. The before pattern isn't used anywhere else in this codebase.
src/libraries/System.Text.Json/tests/Serialization/PropertyVisibilityTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/tests/Serialization/PropertyVisibilityTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/tests/Serialization/PropertyVisibilityTests.cs
Outdated
Show resolved
Hide resolved
public string MyString { get; set; } = "DefaultValue"; | ||
|
||
[JsonPropertyName(nameof(MyString))] | ||
internal string ConflictingString { get; set; } = "ConflictingValue"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need a class variant where this property is public; and a test variant where the conflict spans across two inheritance levels.
@@ -11,6 +11,231 @@ namespace System.Text.Json.Serialization.Tests | |||
{ | |||
public static class PropertyVisibilityTests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need a couple more tests involving 3 levels of inheritance, particularly where we have collisions on public properties defined at different levels.
90e8a96
to
d08435e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR should fix #30964 as well.
We need tests for the scenarios in #30964 (comment), particularly this case:
public class MyClass
{
// This should be ignored.
public int MyNumber { get; set; }
}
public class MyDerivedClass : MyClass
{
// This should be (de)serialized.
new public double MyNumber { get; set; }
}
@@ -11,6 +11,231 @@ namespace System.Text.Json.Serialization.Tests | |||
{ | |||
public static class PropertyVisibilityTests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do underscore-delimited pascal case chunks in such cases, e.g. Serialize_BasePublicProperty_OnConflictWithDerivedPrivate
. The before pattern isn't used anywhere else in this codebase.
66b899a
to
6d95cdd
Compare
@layomia I finally found some time to finish this, but there is one question. Is there any reason to create a This will help with two things:
If an member exists, then it's visible already (at least supports serialization or deserialization separately). If it's in a base class, we will overwrite it. Otherwise, throw an exception. |
6d95cdd
to
6c4c9b4
Compare
dd06a54
to
9748e61
Compare
9748e61
to
9c90500
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#32107 (review) is yet to be addressed.
src/libraries/System.Text.Json/tests/Serialization/PropertyVisibilityTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/tests/Serialization/PropertyVisibilityTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Text.Json/tests/Serialization/PropertyVisibilityTests.cs
Show resolved
Hide resolved
Creating a Perhaps we can remove instances where |
Thanks, saw this and fixed already (: |
08d24f9
to
dc20062
Compare
@layomia tests fail, but for unrelated to this PR issue. Should I address it here? |
It seems to me like the test failures are related to this PR since those doesn't fail on master, only here. |
The failing test should not be affected by this PR since it deserializes an object from an empty string. That area isn't touched, but will take a deeper look. |
You removed the changes to JsonClassInfo recently introduced by #34675 |
4213c34
to
d600e91
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a ton!
This is a breaking change between .NET 3.1 Core/System.Text.Json 4.7.1 and .NET 5/System.Text.Json 5.0.0. Rather than only observing properties visible from the type to be serialized, the serializer looks at the properties for all the base types in the inheritance chain. This means that The conflict would have to be fixed to avoid this exception. The most common way might be to place |
Follow up: the breaking change this PR introduced was mitigated with #36936. |
@layomia this is labeled breaking change. Can you please open an issue if necessary with https://github.com/dotnet/docs/issues/new?template=dotnet-breaking-change.md I don't see an existing one |
Removing the breaking-change labels from this PR since the breaking change it introduced (#32107 (comment)) has been mitigated/reversed in follow up PRs #36936 & #37720 to avoid breaking the OpenMU app from internal AppCompat runs - #37640. |
Closes #32106.
Closes #30964.