-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[5.0-preview5] Loosen property name collision detection involving hidden properties #37105
Conversation
…otnet#36936) * Loosen property name collision detection involving hidden properties * Delay ignored prop cache creation; add more tests * Clarify comments
|
||
if (jsonPropertyInfo.IsIgnored) | ||
{ | ||
(ignoredProperties ??= new HashSet<string>()).Add(propertyName); |
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.
Style (no need to change for this PR) -- where a collection's key is of type string, providing the StringComparer is more explicit and clear (StringComparer.Ordinal just calls string.Equals as does the generic comparer you get by default so it comes to the same thing)
Approved offline by tactics. |
Libraries Build iOS x64 Release run was canceled. |
@layomia you need a reviewer to sign off before merging a PR. Leave it merged now but please get a reviewer. |
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.
Lgtm
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.
LGTM (direct port)
Description
Ports #36936 to preview 5.
Minor clean-up logic from #36648 was also in-lined here to mitigate a merge conflict.
Description from original PR (click to view)
The change in #32107 allowed the (de)serialization of hidden properties when the properties in the derived classes are ignored/non-public.
As a result of the change, the serializer now examines properties at all levels of the inheritance chain, rather than just the ones visible from the type to be serialized. This means that
InvalidOperationException
could be thrown if there is a JSON property name conflict between inheritance levels, if the property higher in the inheritance chain was not hidden by the conflicting property in the more derived class (but by another property). A property is "hidden" if it is virtual and overridden in a derived class with polymorphism or with thenew
keyword.The breaking change was flagged in ASP.NET Core preview 5 validation: dotnet/aspnetcore#22132.
To fix the break, this change loosens property name collision detection involving hidden properties. Rather than throw
InvalidOperationException
, the serializer will a ignore hidden property when the property that hid it is ignored and there's a property name conflict. This aligns with Newtonsoft.Json behavior.Customer Impact
Prevents a regression between previews 4 & 5 where
JsonSerializer
throwsInvalidOperationException
in scenarios that were previously working. Fixes issue caught during preview 5 validation that could affect real world apps - dotnet/aspnetcore#22132.Risk
Tests have been added for this scenario. Also, the fix was tested in an exact repro for the scenario: https://github.com/layomia/OpenMURepro.