Repository navigation
feat: add opt-in naming options for enum constants, fields and explicit names - #2683
Merged
Merged
Conversation
Contributor
[Medium impact] The PR appears safe to merge; no new blocking issue remains. SummaryAdds three opt-in naming settings for enum constants, leading underscores, and explicit Go names.
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 |
…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]>
mromaszewicz
force-pushed
the
feat/issue-2682
branch
from
November 21, 2026 18:01
56a6a0d to
7d8eb3b
Compare
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]>
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]>
This was referenced Nov 21, 2026
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.
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 configuredname-normalizerand leaves everything else alone.enum-values.keep-underscoresSOME_ENUM_NAMESOMEENUMNAMESOME_ENUM_NAMEsome_enum_nameSomeEnumNameSome_Enum_Nameproperties.leading-underscore: drop_idUnderscoreIdIdexplicit-names: verbatimx-go-name: HARD_CODED_IDHARDCODEDIDHARD_CODED_IDx-enum-varnames: [second_value]SecondValuesecond_valueCompatibility
With none of the settings present, no name changes.
make generateleaves every committed.gen.gofile byte-identical; the only generated file in this PR is the new fixture.How it works
enumValueNameToGoName, which isSchemaNameToTypeNameunlesskeep-underscoresis 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.propertyNameToGoName, which trims leading underscores first whenleading-underscore: dropis set. A property that is nothing but underscores keeps its name._idnext toidis 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.x-go-nameon a property,x-go-nameon a parameter, andx-enum-varnames/x-enumNames. (x-go-nameon schemas, responses, request bodies and servers, andx-go-type-name, were already used as written.)x-go-nameis already required to be a Go identifier by the spec validator. Verbatimx-enum-varnamesentries get the same requirement, since they would otherwise be an unsanitized path from spec text into generated code.keep-underscorestogether withcompatibility.old-enum-conflictsis rejected when the configuration is validated; that flag keeps its own code path.Differences from the issue
some_enum_namebecomesSome_Enum_Name, notSome_enum_nameas the issue's table says. Normalizing each word separately is what makes the option independent of the normalizer (ToCamelCaseWithInitialismsdiscards anything its word pattern does not match), and it givesUser_IDwith initialisms.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