Skip to content

feat: add opt-in naming options for enum constants, fields and explicit names - #2683

Merged
mromaszewicz merged 3 commits into
mainfrom
feat/issue-2682
Nov 21, 2026
Merged

mromaszewicz merged 3 commits into
mainfrom
feat/issue-2682

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

Closes #2682. Also addresses #1996, #1906, #1851, #2032 and #2163, each of which asks for one of these behaviors and none of which can change in the default output.

What this adds

output-options.naming, three opt-in settings. Each changes one narrowly scoped behavior of the configured name-normalizer and leaves everything else alone.

output-options:
  naming:
    enum-values:
      keep-underscores: true
    properties:
      leading-underscore: drop
    explicit-names: verbatim
Setting Input Default With the setting
enum-values.keep-underscores enum value SOME_ENUM_NAME SOMEENUMNAME SOME_ENUM_NAME
enum value some_enum_name SomeEnumName Some_Enum_Name
properties.leading-underscore: drop property _id UnderscoreId Id
explicit-names: verbatim x-go-name: HARD_CODED_ID HARDCODEDID HARD_CODED_ID
x-enum-varnames: [second_value] SecondValue second_value

Compatibility

With none of the settings present, no name changes. make generate leaves every committed .gen.go file byte-identical; the only generated file in this PR is the new fixture.

How it works

  • Enum constants go through the same procedure as before (convert, suffix collisions, convert again, suffix again); it now calls enumValueNameToGoName, which is SchemaNameToTypeName unless keep-underscores is on. With it on, the name is split on underscores, each word goes through the configured normalizer, and the words are joined back with the underscores. That works the same for all three normalizers without touching any of them.
  • Struct fields call propertyNameToGoName, which trims leading underscores first when leading-underscore: drop is set. A property that is nothing but underscores keeps its name. _id next to id is then a field-name collision, reported by the existing check from fix: resolve Go field name collisions when JSON keys differ only by separator (#2495) #2550.
  • Verbatim names skip the normalizer. Only three places normalized an explicit name to begin with: x-go-name on a property, x-go-name on a parameter, and x-enum-varnames / x-enumNames. (x-go-name on schemas, responses, request bodies and servers, and x-go-type-name, were already used as written.)
    • A verbatim enum name is never suffixed and never gets the enum's type-name prefix; derived names in the same enum give way to it.
    • x-go-name is already required to be a Go identifier by the spec validator. Verbatim x-enum-varnames entries get the same requirement, since they would otherwise be an unsanitized path from spec text into generated code.
  • keep-underscores together with compatibility.old-enum-conflicts is rejected when the configuration is validated; that flag keeps its own code path.

Differences from the issue

  • some_enum_name becomes Some_Enum_Name, not Some_enum_name as the issue's table says. Normalizing each word separately is what makes the option independent of the normalizer (ToCamelCaseWithInitialisms discards anything its word pattern does not match), and it gives User_ID with initialisms.
  • The same verbatim name given to two values of one enum is a generation error. Enum constants are held in a map keyed by name, so without the error one of the values would be dropped silently instead of reaching the compiler.

Tests

  • pkg/codegen/naming_test.go: each setting on its own with each of the three normalizers, all three together, the collision cases, the rejected configurations and invalid verbatim names, and a randomized check that the second conversion pass leaves the first pass's result alone when underscores are kept.
  • internal/test/naming/options: a compiled fixture with all three settings on, whose test uses the generated names and round-trips the struct through JSON.

🤖 Generated with Claude Code

@mromaszewicz
mromaszewicz requested a review from a team as a code owner November 21, 2026 05:32
@mromaszewicz mromaszewicz added the enhancement New feature or request label Nov 21, 2026
@greptile-apps

greptile-apps Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new blocking issue remains.

Summary

Adds three opt-in naming settings for enum constants, leading underscores, and explicit Go names.

  • Enum constants can keep underscores between normalized words.
  • Object and parameter fields can drop leading underscores.
  • Selected names from the spec can be emitted exactly as written.

The earlier parameter-name mismatch and enum-prefix value loss are also fixed in the current code. No new actionable issues were found.

Reviews (4) · Last reviewed commit: "refactor: derive a struct field's name i..." · Reviewed by Greptile

Comment thread pkg/codegen/schema.go Resolved
Comment thread pkg/codegen/schema.go Resolved
@mromaszewicz mromaszewicz added the notable changes Used for release notes to highlight these more highly label Nov 21, 2026
…it names

Add `output-options.naming`, three opt-in settings that each change one
narrowly scoped behavior of the configured name normalizer:

- `enum-values.keep-underscores` keeps the underscores of an enum value
  in the name of its constant, so `SOME_ENUM_NAME` is no longer turned
  into `SOMEENUMNAME`.
- `properties.leading-underscore: drop` makes the leading underscores of
  a property contribute nothing to its field name, so `_id` becomes `Id`
  rather than `UnderscoreId`.
- `explicit-names: verbatim` emits a name the spec states itself, with
  `x-go-name` on a property or parameter or with `x-enum-varnames` /
  `x-enumNames`, exactly as written.

With none of them set no name changes: every committed generated file is
byte-identical.

Closes #2682

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Address review findings on the naming options, plus one more of the same
kind:

- With `properties.leading-underscore: drop`, a query, header or cookie
  parameter `_id` became the field `Id` of the parameters struct while the
  client and server still read `params.UnderscoreId`. A parameter is now
  named the way its field is, so the option applies to parameters too.
- With `explicit-names: verbatim`, the type name put in front of an
  enum's derived constants could produce a name the spec states for
  another value, and one of the two values was dropped. The derived name
  now gets a suffix.
- The strict server declared a path parameter's request object field with
  its first letter capitalized but assigned to it without, which no longer
  matched for a verbatim name that starts in lower case. The field is now
  declared as it is used. Names that are not verbatim already start with
  a capital, so no existing output changes.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Comment thread pkg/codegen/operations.go Resolved
A query, header or cookie parameter was named twice: once as a property
of the operation's parameters struct, and once by the parameter itself
for the code that reads the field. The two derivations had to be kept in
step by hand, and were not.

Derive field names in a single function, goFieldName, and have the
parameters struct take each field's name from its parameter rather than
deriving it again.

- A path parameter is an argument, not a field, so it is no longer
  affected by `naming.properties.leading-underscore`. `_ctx` stays
  `underscoreCtx` instead of becoming a second `ctx` argument.
- Two existing mismatches, both of which generated code that did not
  compile, are fixed along the way: `x-go-name` on a parameter's schema
  renamed the field but not its uses, and
  `x-oapi-codegen-only-honour-go-name` on a parameter did the same.

No committed generated file changes.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request notable changes Used for release notes to highlight these more highly

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant