Skip to content

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

Merged
mttrbrts merged 1 commit intoaccordproject:mainfrom
yashhzd:yashhzd/i31/fix-csharp-reserved-keyword-enum-values
Feb 25, 2026
Merged

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

Conversation

@yashhzd
Copy link
Contributor

@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>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a critical compilation bug where C# reserved keywords used as enum values generate invalid C# code. The fix applies the existing toCSharpIdentifier method to escape reserved keywords with an underscore prefix and adds EnumMember attributes to preserve original names during JSON serialization.

Changes:

  • Modified visitEnumValueDeclaration to escape reserved keywords in enum value names using toCSharpIdentifier
  • Added EnumMember attribute when enum value names are transformed to preserve original serialization behavior
  • Added comprehensive integration and unit tests covering reserved keywords in enum values

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
lib/codegen/fromcto/csharp/csharpvisitor.js Modified visitEnumValueDeclaration to apply toCSharpIdentifier transformation and conditionally add EnumMember attribute for escaped enum values
test/codegen/fromcto/csharp/csharpvisitor.js Added integration test with reserved keywords (event, fixed, virtual) and two unit tests verifying both reserved and non-reserved enum value handling

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@mttrbrts mttrbrts enabled auto-merge (squash) February 25, 2026 20:29
@mttrbrts mttrbrts self-requested a review February 25, 2026 20:29
@mttrbrts mttrbrts merged commit 9dd63f1 into accordproject:main Feb 25, 2026
16 checks passed
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

3 participants