Skip to content

Comments

fix(csharp): escape reserved keywords in enum value declarations#201

Open
yashhzd wants to merge 1 commit intoaccordproject:mainfrom
yashhzd:yashhzd/i31/fix-csharp-reserved-keyword-enum-values
Open

fix(csharp): escape reserved keywords in enum value declarations#201
yashhzd wants to merge 1 commit intoaccordproject:mainfrom
yashhzd:yashhzd/i31/fix-csharp-reserved-keyword-enum-values

Conversation

@yashhzd
Copy link

@yashhzd yashhzd commented Feb 20, 2026

Summary

Fixes #31

Enum values using C# reserved keywords (e.g. event, fixed, virtual) produce invalid C# code that fails to compile. This PR applies the existing toCSharpIdentifier method to enum value names, which prefixes reserved keywords with _ — consistent with how field names are already handled.

When an enum value name is escaped, a [System.Runtime.Serialization.EnumMember(Value = "originalName")] attribute is added to preserve the original name during JSON serialization, matching the existing pattern used for the @AcceptedValue decorator.

Changes

  • lib/codegen/fromcto/csharp/csharpvisitor.js: Modified visitEnumValueDeclaration to pass enum value names through toCSharpIdentifier and add EnumMember attribute when the name is escaped.
  • test/codegen/fromcto/csharp/csharpvisitor.js: Added integration test with reserved keyword enum values (event, fixed, virtual) and unit tests for both reserved and non-reserved enum value names.

Example

Before (invalid C#):

enum Status {
    Active,
    event,   // compilation error — reserved keyword
    fixed,   // compilation error — reserved keyword
}

After (valid C#):

enum Status {
    Active,
    [System.Runtime.Serialization.EnumMember(Value = "event")]
    _event,
    [System.Runtime.Serialization.EnumMember(Value = "fixed")]
    _fixed,
}

Testing

All 569 tests pass (3 new tests added). No existing tests broken.

Enum values using C# reserved keywords (e.g. event, fixed, virtual)
caused compilation errors in generated code. Apply toCSharpIdentifier
to enum value names and add EnumMember attribute to preserve the
original name for serialization when escaping occurs.

Closes accordproject#31

Signed-off-by: Yash Goel <yashhzd@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to compile CSharp code with models containing reserved keywords

1 participant