Skip to content
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

Merged
merged 1 commit into from
May 28, 2020

Conversation

layomia
Copy link
Contributor

@layomia layomia commented May 28, 2020

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 the new 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 throws InvalidOperationException 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.

…otnet#36936)

* Loosen property name collision detection involving hidden properties

* Delay ignored prop cache creation; add more tests

* Clarify comments
@layomia layomia added this to the 5.0 milestone May 28, 2020
@layomia layomia requested a review from jozkee as a code owner May 28, 2020 00:27
@layomia layomia self-assigned this May 28, 2020
@layomia layomia added the Servicing-consider Issue for next servicing release review label May 28, 2020

if (jsonPropertyInfo.IsIgnored)
{
(ignoredProperties ??= new HashSet<string>()).Add(propertyName);
Copy link
Member

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)

@layomia
Copy link
Contributor Author

layomia commented May 28, 2020

Approved offline by tactics.

@layomia layomia added api-approved API was approved in API review, it can be implemented Servicing-approved Approved for servicing release and removed Servicing-consider Issue for next servicing release review api-approved API was approved in API review, it can be implemented labels May 28, 2020
@layomia
Copy link
Contributor Author

layomia commented May 28, 2020

Libraries Build iOS x64 Release run was canceled.

@layomia layomia merged commit 4ae4e2f into dotnet:release/5.0-preview5 May 28, 2020
@layomia layomia deleted the collisions-port branch May 28, 2020 07:06
@danmoseley
Copy link
Member

@layomia you need a reviewer to sign off before merging a PR. Leave it merged now but please get a reviewer.

Copy link
Member

@ericstj ericstj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm

Copy link
Member

@steveharter steveharter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (direct port)

@ghost ghost locked as resolved and limited conversation to collaborators Dec 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json Servicing-approved Approved for servicing release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants