From c2415d7e96f1bbc8864a2bbb7035fbcac2eb5100 Mon Sep 17 00:00:00 2001 From: Eirik Tsarpalis Date: Thu, 8 Feb 2024 09:46:07 +0000 Subject: [PATCH] [release/8.0] Fix STJ SG regression in handling of property names that are reserved keywords. (#98082) * Fix #98050. (#98058) * bump servicing version --- .../gen/JsonSourceGenerator.Emitter.cs | 2 +- .../src/System.Text.Json.csproj | 2 +- .../JsonSourceGeneratorTests.cs | 27 +++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs index 90575721c507d..692f90741b037 100644 --- a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs +++ b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs @@ -798,7 +798,7 @@ private void GenerateFastPathFuncForObject(SourceWriter writer, ContextGeneratio if (defaultCheckType != DefaultCheckType.None) { // Use temporary variable to evaluate property value only once - string localVariableName = $"__value_{propertyGenSpec.NameSpecifiedInSourceCode}"; + string localVariableName = $"__value_{propertyGenSpec.NameSpecifiedInSourceCode.TrimStart('@')}"; writer.WriteLine($"{propertyGenSpec.PropertyType.FullyQualifiedName} {localVariableName} = {objectExpr}.{propertyGenSpec.NameSpecifiedInSourceCode};"); propValueExpr = localVariableName; } diff --git a/src/libraries/System.Text.Json/src/System.Text.Json.csproj b/src/libraries/System.Text.Json/src/System.Text.Json.csproj index 29409f7f88629..9c492f0c6e034 100644 --- a/src/libraries/System.Text.Json/src/System.Text.Json.csproj +++ b/src/libraries/System.Text.Json/src/System.Text.Json.csproj @@ -9,7 +9,7 @@ true true true - 2 + 3 Provides high-performance and low-allocating types that serialize objects to JavaScript Object Notation (JSON) text and deserialize JSON text to objects, with UTF-8 support built-in. Also provides types to read and write JSON text encoded as UTF-8, and to create an in-memory document object model (DOM), that is read-only, for random access of the JSON elements within a structured view of the data. The System.Text.Json library is built-in as part of the shared framework in .NET Runtime. The package can be installed when you need to use it in other target frameworks. diff --git a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/JsonSourceGeneratorTests.cs b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/JsonSourceGeneratorTests.cs index ce27630b6ab69..332f718ce3ff4 100644 --- a/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/JsonSourceGeneratorTests.cs +++ b/src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/JsonSourceGeneratorTests.cs @@ -737,5 +737,32 @@ public class MyClass Compilation compilation = CompilationHelper.CreateCompilation(source, parseOptions: CompilationHelper.CreateParseOptions(languageVersion)); CompilationHelper.RunJsonSourceGenerator(compilation); } + + [Fact] + public void FastPathWithReservedKeywordPropertyNames_CompilesSuccessfully() + { + // Regression test for https://github.com/dotnet/runtime/issues/98050 + + string source = """ + using System.Text.Json.Serialization; + + public class Model + { + public string type { get; set; } + public string alias { get; set; } + public string @class { get; set; } + public string @struct { get; set; } + } + + [JsonSourceGenerationOptions(DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)] + [JsonSerializable(typeof(Model))] + internal partial class ModelContext : JsonSerializerContext + { + } + """; + + Compilation compilation = CompilationHelper.CreateCompilation(source); + CompilationHelper.RunJsonSourceGenerator(compilation); + } } }