Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix #98050. (#98058)
  • Loading branch information
eiriktsarpalis committed Feb 7, 2024
commit 7e7dbd47c10c7dcd310373668422a01605f7eb4e
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}