Repository navigation
STJ: Avoid duplicate initialization of required or init-only properties - #97726
Conversation
|
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis |
eiriktsarpalis
left a comment
There was a problem hiding this comment.
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?)
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. |
| 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.
There's an equivalent implementation in the reflection resolver:
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.
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.
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
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.
|
@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. |
…neration.Unit.Tests/JsonSourceGeneratorTests.cs
…neration.Unit.Tests/JsonSourceGeneratorTests.cs
Fixes #97621