Fix JsonSourceGenerator verbatim identifier handling in property initializers#122740
Merged
stephentoub merged 4 commits intomainfrom Jan 5, 2026
Merged
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]>
Copilot
AI
changed the title
[WIP] Fix JsonSourceGenerator handling of verbatim identifiers
Fix JsonSourceGenerator verbatim identifier handling in property initializers
Dec 26, 2025
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis |
Contributor
There was a problem hiding this comment.
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
stephentoub
reviewed
Jan 3, 2026
...tem.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/JsonSourceGeneratorTests.cs
Outdated
Show resolved
Hide resolved
…neration.Unit.Tests/JsonSourceGeneratorTests.cs
stephentoub
reviewed
Jan 3, 2026
...tem.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/JsonSourceGeneratorTests.cs
Outdated
Show resolved
Hide resolved
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]>
PranavSenthilnathan
approved these changes
Jan 5, 2026
Member
|
/ba-g dead letter |
This was referenced Jan 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.