-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Fix JsonSourceGenerator verbatim identifier handling in property initializers #122740
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
base: main
Are you sure you want to change the base?
Fix JsonSourceGenerator verbatim identifier handling in property initializers #122740
Conversation
…ializers Fix #116507: C# verbatim identifiers like @else are now correctly handled when generating property initializers for init-only properties. The fix changes PropertyInitializerGenerationSpec.Name to use NameSpecifiedInSourceCode (which includes the @ prefix for reserved keywords) instead of MemberName. Also added a unit test to verify this scenario works correctly. Co-authored-by: stephentoub <[email protected]>
|
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.
Pull request overview
This PR fixes a bug in the JsonSourceGenerator where verbatim identifiers (e.g., @else) for C# reserved keywords were not properly handled when generating property initializers for init-only properties, causing compilation errors.
Key changes:
- Changed
PropertyInitializerGenerationSpec.Nameto useNameSpecifiedInSourceCodeinstead ofMemberNameto preserve the@prefix for reserved keywords - Added regression test for init-only properties with reserved keyword names
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs | Fixed property initializer generation to use NameSpecifiedInSourceCode which includes the @ prefix for verbatim identifiers |
| src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/JsonSourceGeneratorTests.cs | Added regression test InitOnlyPropertyWithReservedKeywordName_CompilesSuccessfully to verify the fix |
...tem.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/JsonSourceGeneratorTests.cs
Outdated
Show resolved
Hide resolved
...tem.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/JsonSourceGeneratorTests.cs
Outdated
Show resolved
Hide resolved
…neration.Unit.Tests/JsonSourceGeneratorTests.cs
| CompilationHelper.RunJsonSourceGenerator(compilation, logger: logger); | ||
| } | ||
|
|
||
| #if ROSLYN4_4_OR_GREATER && NET |
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.
@copilot, why are these necessary? Can the test be tweaked so that it still tests what it needs to be test but can work on all targets?
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.
Changed the test to use a regular class instead of record, which removes the need for the conditional compilation guards while still testing the same code path for verbatim identifier handling in property initializers. Fixed in af6c4c0.
Changed the test to use a regular class instead of a record, which removes the need for #if ROSLYN4_4_OR_GREATER && NET guards while still testing the same code path for verbatim identifier handling in property initializers. Co-authored-by: stephentoub <[email protected]>
main PR
Description
JsonSourceGeneratoremitted bare C# keywords instead of verbatim identifiers (@else→else) when generating property initializers for init-only properties, causing compilation errors.Root cause:
PropertyInitializerGenerationSpec.Namewas set toMemberName(raw identifier) instead ofNameSpecifiedInSourceCode(includes@prefix for reserved keywords).Fix: Single line change in
JsonSourceGenerator.Parser.cs:Repro:
Previously generated invalid code:
new MyClass(){ else = ... }instead ofnew MyClass(){ @else = ... }.Customer Impact
Any type with init-only properties using C# reserved keywords as names fails to compile with source generation.
Regression
No, this is a long-standing bug in the source generator.
Testing
InitOnlyPropertyWithReservedKeywordName_CompilesSuccessfullyusing a regular class to ensure compatibility across all targets without conditional compilationRisk
Low. Single line change affecting only the name used in generated property initializers. The fix uses the same
NameSpecifiedInSourceCodeproperty already used elsewhere in the generator for the same purpose.Package authoring no longer needed in .NET 9
IMPORTANT: Starting with .NET 9, you no longer need to edit a NuGet package's csproj to enable building and bump the version.
Keep in mind that we still need package authoring in .NET 8 and older versions.
Original prompt
@verbatim identifiers. #122732✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.