Skip to content

JsonSerializerContext source generation fails with keyword identifiers #64159

@jzebedee

Description

@jzebedee

Description

When a type containing a keyword identifier (e.g., @base) is used with JsonSerializerContext's source generation, compilation fails with several errors:

1>System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\CK3JsonContext.Weight.g.cs(55,78,55,82): error CS1001: Identifier expected
1>System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\CK3JsonContext.Weight.g.cs(55,78,55,82): error CS1003: Syntax error, ',' expected
1>System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\CK3JsonContext.Weight.g.cs(56,85,56,89): error CS1001: Identifier expected
1>System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\CK3JsonContext.Weight.g.cs(56,85,56,89): error CS1003: Syntax error, ',' expected
1>System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\CK3JsonContext.Weight.g.cs(119,53,119,57): error CS1001: Identifier expected
1>System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\CK3JsonContext.Weight.g.cs(119,53,119,57): error CS1003: Syntax error, ',' expected
1>System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\CK3JsonContext.Weight.g.cs(55,78,55,82): error CS1511: Keyword 'base' is not available in a static method
1>System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\CK3JsonContext.Weight.g.cs(55,78,55,82): error CS0747: Invalid initializer member declarator
1>System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\CK3JsonContext.Weight.g.cs(56,49,56,85): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
1>System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\CK3JsonContext.Weight.g.cs(56,85,56,89): error CS1511: Keyword 'base' is not available in a static method
1>System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\CK3JsonContext.Weight.g.cs(56,92,56,97): error CS0103: The name 'value' does not exist in the current context
1>System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\CK3JsonContext.Weight.g.cs(56,85,56,98): error CS0747: Invalid initializer member declarator
1>System.Text.Json.SourceGeneration\System.Text.Json.SourceGeneration.JsonSourceGenerator\CK3JsonContext.Weight.g.cs(119,53,119,57): error CS1511: Keyword 'base' is not available in a static method

Reproduction Steps

Define a type that includes a keyword identifier:

public class Weight
{
    public int @base { get; set; }
    public int current { get; set; }
    public int target { get; set; }
}

and add it to a JsonSerializerContext:

[JsonSerializable(typeof(Weight))]
internal partial class CK3JsonContext : JsonSerializerContext
{
}

Expected behavior

Either source generation succeeds, or an error that keyword identifiers are not supported is emitted

Actual behavior

Generated source gets mangled and breaks compilation in an opaque way

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions