Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Order of serialized properties determined by dictionary enumeration on fast-path source generators #59043

Closed
Tracked by #77019
eiriktsarpalis opened this issue Sep 13, 2021 · 8 comments
Labels
area-System.Text.Json enhancement Product code improvement that does NOT require public API changes/additions help wanted [up-for-grabs] Good issue for external contributors source-generator Indicates an issue with a source generator feature
Milestone

Comments

@eiriktsarpalis
Copy link
Member

Currently, the fast-path source generator will use the TryFilterSerializableProps method to determine the properties that are serializable. The results are returned as a dictionary, which the source generator will enumerate in order to emit the relevant logic.

Note that dictionary enumeration does not provide any guarantees on ordering, as such it is likely that this could end up not honoring the desired serialization order (e.g. as specified by the JsonPropertyOrderAttribute). That being said, the current implementation of System.Collections.Generic.Dictionary happens to preserve ordering due to an implementation detail that may well change in the future. We should update the TryFilterSerializableProps method to provide more robust guarantees on property ordering.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Sep 13, 2021
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@eiriktsarpalis eiriktsarpalis added this to the 7.0.0 milestone Sep 13, 2021
@eiriktsarpalis eiriktsarpalis added enhancement Product code improvement that does NOT require public API changes/additions area-System.Text.Json and removed untriaged New issue has not been triaged by the area owner labels Sep 13, 2021
@ghost
Copy link

ghost commented Sep 13, 2021

Tagging subscribers to this area: @eiriktsarpalis, @layomia
See info in area-owners.md if you want to be subscribed.

Issue Details

Currently, the fast-path source generator will use the TryFilterSerializableProps method to determine the properties that are serializable. The results are returned as a dictionary, which the source generator will enumerate in order to emit the relevant logic.

Note that dictionary enumeration does not provide any guarantees on ordering, as such it is likely that this could end up not honoring the desired serialization order (e.g. as specified by the JsonPropertyOrderAttribute). That being said, the current implementation of System.Collections.Generic.Dictionary happens to preserve ordering due to an implementation detail that may well change in the future. We should update the TryFilterSerializableProps method to provide more robust guarantees on property ordering.

Author: eiriktsarpalis
Assignees: -
Labels:

enhancement, area-System.Text.Json

Milestone: 7.0.0

@eiriktsarpalis
Copy link
Member Author

Given that the source generator targets netstandard2.0, for the 6.0 release we might want to check if this is honored in older implementations of Dictionary, up to and including the netfx implementation.

@danmoseley
Copy link
Member

I wonder whether when built in DEBUG we should have our dictionary randomize its enumeration. Long ago, before string hash codes were randomized by default, the libraries used to randomize string hashcodes (XOR them with the build number or similar) in DEBUG only.

@layomia layomia added the source-generator Indicates an issue with a source generator feature label Sep 22, 2021
@GSPP
Copy link

GSPP commented Sep 25, 2021

@danmoseley I have worked on a large application that relied on Dictionary usually being ordered in about 1000 places... I am a fan of intentionally breaking behavior that is not guaranteed but I think that really has to happen in v1 or else it's an upgrade pain.

@danmoseley
Copy link
Member

@GSPP in general no framework can reasonably guarantee undefined behavior, post v1 or not. It so heavily constrains maintenance, perf improvements, etc. As well we do not test such behavior - so we do not necessarily know when it changes - nor necessarily know that anyone depends on it. That may turn up much later when the product is in servicing mode. We have seen the result of this in .NET Framework where callers even depended on details of timing to the extent performance improvements caused breaks and had to be reverted later. .NET Core introduced SxS deployment to give more freedom to make improvements like this.

There can always be exceptions where something is so widely assumed in the ecosystem that it is best to document it. I do not have the data to guess whether this is the case here or not but my guess is that it is unlikely.

@eiriktsarpalis
Copy link
Member Author

eiriktsarpalis commented Apr 18, 2022

Moving to Future, as we won't have time to work on this in the .NET 7 timeframe.

@layomia layomia added the help wanted [up-for-grabs] Good issue for external contributors label Dec 2, 2022
@eiriktsarpalis
Copy link
Member Author

Fixed by #87383.

@eiriktsarpalis eiriktsarpalis modified the milestones: Future, 8.0.0 Jun 22, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Jul 22, 2023
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
area-System.Text.Json enhancement Product code improvement that does NOT require public API changes/additions help wanted [up-for-grabs] Good issue for external contributors source-generator Indicates an issue with a source generator feature
Projects
None yet
Development

No branches or pull requests

4 participants