-
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
STJ: Avoid duplicate initialization of required or init-only properties #97726
STJ: Avoid duplicate initialization of required or init-only properties #97726
Conversation
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis |
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.
There's a test suite in the repo call PropertyVisibilityTests
. As part of this change, would it be possible to evaluate the test coverage for shadowed properties (i.e. properties with the new keyword?)
src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs
Outdated
Show resolved
Hide resolved
Some tests around shadowed properties already exist but do not cover combination with required and init-only properties, what should be addressed by this PR. |
@@ -891,7 +891,9 @@ private TypeGenerationSpec ParseTypeGenerationSpec(in TypeToGenerate typeToGener | |||
// property is static or an indexer | |||
propertyInfo.IsStatic || propertyInfo.Parameters.Length > 0 || | |||
// It is overridden by a derived property | |||
PropertyIsOverriddenAndIgnored(propertyInfo, state.IgnoredMembers)) | |||
PropertyIsOverriddenAndIgnored(propertyInfo, state.IgnoredMembers) || |
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.
There's an equivalent implementation in the reflection resolver:
Line 141 in 3a382f2
PropertyIsOverriddenAndIgnored(propertyInfo, state.IgnoredProperties)) |
Should this be updated as well? Do we know the tests are not failing in that case as well?
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.
Yes, we should keep both implementations aligned. For reflection flow, the deduplication is done at one place for both get and set operations, whereas for the source generation flow the fast-path and initializers deduplication logic were split. With this PR it is handled at the same place for all flows.
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.
Taking a closer look at the implementation -- this method ultimately calls into the AddPropertyWithConflictResolution
method which handles conflict resolution (including shadowed properties):
runtime/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs
Lines 1037 to 1039 in b94a82d
// Is the current property hidden by the previously cached property | |
// (with `new` keyword, or by overriding)? | |
memberInfo.IsOverriddenOrShadowedBy(otherSymbol) || |
My impression is that the bug is caused by a missed case in that logic, so it seems to me that it's this particular logic that should be debugged.
If you don't know how to debug source generator code, the easiest way to do that is by adding a unit test in System.Text.Json.SourceGeneration.Roslyn*.Unit.Tests
and debugging them.
src/libraries/System.Text.Json/tests/Common/RequiredKeywordTests.cs
Outdated
Show resolved
Hide resolved
@manandre I just pushed changes to the branch that moves the deduplication logic back to the member initializer parser method. On closer examination of the parser logic, I was reminded that no such deduplication should be happening in the property parsing logic (code for shadowed members must be generated for use by the metadata-based serializer). At the same time, property-based deduplication uses the JSON name of the property (configurable via |
@eiriktsarpalis What do we do for the metadata flow then? I had changed it to keep alignment with source generation flow... |
Good catch, I've reverted the changes. |
...tem.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/JsonSourceGeneratorTests.cs
Outdated
Show resolved
Hide resolved
…neration.Unit.Tests/JsonSourceGeneratorTests.cs
...tem.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/JsonSourceGeneratorTests.cs
Outdated
Show resolved
Hide resolved
…neration.Unit.Tests/JsonSourceGeneratorTests.cs
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.
Thank you @manandre !
Fixes #97621