-
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 <2642209+stephentoub@users.noreply.github.com>
|
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 |
| [Fact] | ||
| public void InitOnlyPropertyWithReservedKeywordName_CompilesSuccessfully() | ||
| { | ||
| // Regression test for https://github.com/dotnet/runtime/issues/116507 |
Copilot
AI
Dec 26, 2025
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.
The issue number in this comment (116507) doesn't match the issue number mentioned in the PR description (122732). Please verify which issue number is correct and update the comment accordingly.
| // Regression test for https://github.com/dotnet/runtime/issues/116507 | |
| // Regression test for https://github.com/dotnet/runtime/issues/122732 |
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 A(){ else = ... }instead ofnew A(){ @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_CompilesSuccessfullyRisk
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.