Skip to content

Opt-in naming options: keep underscores in enum constants, drop leading underscores in fields, verbatim explicit names #2682

Closed

Description

@mromaszewicz

Summary

Add three opt-in naming options under output-options.naming. Each one switches a single, narrowly scoped behavior of the existing name normalizers. With none of them set, generated output is byte-identical to today.

output-options:
  name-normalizer: ToCamelCase      # unchanged, still global
  naming:
    enum-values:
      keep-underscores: true        # default false
    properties:
      leading-underscore: drop      # default: spell as "Underscore"
    explicit-names: verbatim        # default: normalize as today

Option names are a proposal and open to adjustment; the behaviors are the spec.

Motivation

Several open issues ask for the same few things, and none of them can be changed in the default output without breaking existing users:

Issue Complaint Option that addresses it
#1996 enum value SOME_ENUM_NAME generates constant SOMEENUMNAME enum-values.keep-underscores
#1906 x-enum-varnames entries lose their underscores explicit-names: verbatim
#1851 x-go-name: HARD_CODED_ID generates field HARDCODEDID explicit-names: verbatim
#2032, #2163 property _id generates field UnderscoreId since v2.5.0 properties.leading-underscore: drop

Selecting a whole different normalizer per category of name was considered and rejected: nobody is asking for a different casing algorithm, only for these specific behaviors, and scoped options touch two call sites instead of roughly seventy.

Behavior

1. naming.enum-values.keep-underscores

When true, _ is not treated as a word separator when deriving enum constant names, and it is emitted in the name. Everything else about the configured name-normalizer still applies.

Enum value Today With keep-underscores
SOME_ENUM_NAME SOMEENUMNAME SOME_ENUM_NAME
some_enum_name SomeEnumName Some_enum_name
ABC-DEF ABCDEF ABCDEF
_FOO UnderscoreFOO Underscore_FOO
  • Applies to names derived from enum values and to x-enum-varnames / x-enumNames entries (unless explicit-names: verbatim is also set, which takes precedence for those entries).
  • Must work with all three normalizers (ToCamelCase, ToCamelCaseWithDigits, ToCamelCaseWithInitialisms).
  • The enum naming procedure itself does not change: normalize and sanitize each name, suffix collisions, normalize again, suffix collisions again. It simply calls the parameterized normalizer. The second pass is load-bearing for existing output and must stay.
  • A leading underscore keeps its Underscore prefix word, so constants stay exported.
  • When an enum's constants are prefixed with the type name (cross-enum conflict, or compatibility.always-prefix-enum-values), the prefix is applied as today: SomeSchemaSOME_ENUM_NAME.
  • compatibility.old-enum-conflicts stays on its existing code path. Combining it with keep-underscores is rejected at config validation.

2. naming.properties.leading-underscore

Controls how leading underscores in an object property name contribute to the Go struct field name.

  • unset (default): spelled out as Underscore, as today.
  • drop: leading underscores contribute nothing.
Property Today With drop
_id UnderscoreId Id
_UPPER_SNAKE_CASE UnderscoreUPPERSNAKECASE UPPERSNAKECASE
__meta UnderscoreUnderscoreMeta Meta

3. naming.explicit-names: verbatim

When set, a Go name that the spec author wrote explicitly is emitted exactly as written. It skips the name normalizer and the leading-symbol prefix step.

  • Applies to x-go-name, x-go-type-name, and x-enum-varnames / x-enumNames.
  • The author is responsible for the name being usable. The generator does not sanitize, does not fix case, does not check that the name is exported, and does not rename it to resolve collisions. Problems surface as compiler errors.
  • The only check is that the value is a valid Go identifier. x-go-name and x-go-type-name already get this check from the spec validator today. x-enum-varnames / x-enumNames entries must get the same check when this option is on, because they would otherwise be an unsanitized path from spec text into generated source.
  • Verbatim enum names never get the enum's type-name prefix, not on a cross-enum conflict and not with compatibility.always-prefix-enum-values. Within one enum, the prefix still applies to the constants whose names were derived rather than written explicitly.
  • If x-enum-varnames lists fewer names than there are enum values, the remaining values derive their names as usual.
  • This is the global equivalent of the existing per-property x-oapi-codegen-only-honour-go-name extension, without requiring compatibility.allow-unexported-struct-field-names.

Compatibility

  • All three options are opt-in. With none set, no code path changes and all committed .gen.go fixtures must remain byte-identical.
  • As with other naming settings, specs that reference each other through import-mapping must be generated with the same naming settings.

Implementation notes

  • The normalizer is currently a single package-level function, and the Underscore / Minus / N prefix words come from typeNamePrefix, which runs outside the normalizer. Options 1 and 2 need the enum pipeline and Property.derivedGoFieldName to call a parameterized variant; other call sites keep using the global normalizer unchanged.
  • ToCamelCaseWithInitialisms splits its input with a regular expression that discards non-matching characters, so keeping underscores there needs explicit handling.
  • Add the new keys to configuration-schema.json and document them in the README next to name-normalizer.

Tests

  • A fixture under internal/test/ covering each option on its own and all three together, with each of the three normalizers.
  • A test that the second enum normalization pass leaves names from the first pass unchanged when keep-underscores is on, across a randomized corpus.
  • Config validation tests for the rejected old-enum-conflicts combination and for invalid verbatim x-enum-varnames entries.

Out of scope

Activity

  1. added
    enhancement
    on Nov 21, 2026
  2. added a commit that references this issue on Nov 21, 2026
    7d8eb3b
  3. added a commit that references this issue on Nov 22, 2026
    6d8ff0f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions