Skip to content

Commit

Permalink
Fix #98050. (#98058)
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriktsarpalis committed Feb 7, 2024
1 parent 063447d commit 24d149c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -795,5 +795,32 @@ public partial class MyContext : JsonSerializerContext
CompilationHelper.RunJsonSourceGenerator(compilation);
}
#endif

[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);
}
}
}

0 comments on commit 24d149c

Please sign in to comment.