Skip to content

Commit 24d149c

Browse files
Fix #98050. (#98058)
1 parent 063447d commit 24d149c

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ private void GenerateFastPathFuncForObject(SourceWriter writer, ContextGeneratio
796796
if (defaultCheckType != DefaultCheckType.None)
797797
{
798798
// Use temporary variable to evaluate property value only once
799-
string localVariableName = $"__value_{propertyGenSpec.NameSpecifiedInSourceCode}";
799+
string localVariableName = $"__value_{propertyGenSpec.NameSpecifiedInSourceCode.TrimStart('@')}";
800800
writer.WriteLine($"{propertyGenSpec.PropertyType.FullyQualifiedName} {localVariableName} = {objectExpr}.{propertyGenSpec.NameSpecifiedInSourceCode};");
801801
propValueExpr = localVariableName;
802802
}

src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Unit.Tests/JsonSourceGeneratorTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,5 +795,32 @@ public partial class MyContext : JsonSerializerContext
795795
CompilationHelper.RunJsonSourceGenerator(compilation);
796796
}
797797
#endif
798+
799+
[Fact]
800+
public void FastPathWithReservedKeywordPropertyNames_CompilesSuccessfully()
801+
{
802+
// Regression test for https://github.com/dotnet/runtime/issues/98050
803+
804+
string source = """
805+
using System.Text.Json.Serialization;
806+
807+
public class Model
808+
{
809+
public string type { get; set; }
810+
public string alias { get; set; }
811+
public string @class { get; set; }
812+
public string @struct { get; set; }
813+
}
814+
815+
[JsonSourceGenerationOptions(DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
816+
[JsonSerializable(typeof(Model))]
817+
internal partial class ModelContext : JsonSerializerContext
818+
{
819+
}
820+
""";
821+
822+
Compilation compilation = CompilationHelper.CreateCompilation(source);
823+
CompilationHelper.RunJsonSourceGenerator(compilation);
824+
}
798825
}
799826
}

0 commit comments

Comments
 (0)