Emit safe C# for enum constants in CodeFormatter.Write#197
Merged
jeremydmiller merged 1 commit intomainfrom Apr 28, 2026
Merged
Emit safe C# for enum constants in CodeFormatter.Write#197jeremydmiller merged 1 commit intomainfrom
jeremydmiller merged 1 commit intomainfrom
Conversation
The previous implementation rendered any enum value as
$"{Type}.{value}", which silently calls Enum.ToString().
For values that don't correspond to a single named member —
most notably bit-OR'd combinations on non-[Flags] enums like
Npgsql.NpgsqlDbType — ToString returns the underlying integer
literal as a string, so we'd emit uncompilable source such as
`NpgsqlTypes.NpgsqlDbType.-2147483629`. Roslyn then rejects the
generated assembly with CS1001.
Replace the single-line implementation with WriteEnum, which
handles three cases:
1. Defined single member → Type.Name (unchanged)
2. [Flags] combination → Type.A | Type.B
3. Undefined value → ((Type)(rawIntegerLiteral))
Three new CodeFormatterTests cover each branch, including the
exact non-[Flags] OR'd-bit shape that NpgsqlDbType uses.
Fixes the Marten codegen bug tracked in JasperFx/marten#3702.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
CodeFormatter.Writerenders enum values with the single line:That implicitly calls
Enum.ToString(). For values that aren't a single defined member — particularly bit-OR'd combinations on non-[Flags]enums such asNpgsql.NpgsqlDbType—ToStringreturns the underlying integer literal as a string, so we emit uncompilable source like:Roslyn rejects the generated assembly with
CS1001 Identifier expected. This is the root cause of the Marten codegen bug tracked at JasperFx/marten#3702 (a[DuplicateField]onList<T>withDbType = NpgsqlDbType.Array | NpgsqlDbType.Text).Fix
Replace the single-line implementation with
WriteEnum, which handles three cases explicitly:Numbers.one)Namespace.Numbers.one(unchanged)[Flags]combo (Toppings.Cheese | Toppings.Pepperoni)Type.Cheese | Type.Pepperoni((Type)(rawIntegerLiteral))The
[Flags]branch defends against the case whereToStringstill returns a numeric literal (e.g. an Or'd combination whose bits don't all map to defined members) by checkingIsNumericLiteraland falling through to the cast form.Tests
Three new tests in
CodeFormatterTests, including aDirtyFlaglessenum that mimics the exactNpgsqlDbTypeshape (non-[Flags]enum whose callers OR member values together).Downstream verification
With this JasperFx built locally and dropped into the NuGet cache, the Marten reproduction test from #3702 (
Bug_PR_3702_list_index_compile_error.can_create_array_duplicate_column_on_a_list_field) passes with no Marten-side changes, superseding the call-site patch that PR proposed. Marten will be bumped to the next JasperFx release in a follow-up PR.🤖 Generated with Claude Code