Repository navigation
Description
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 todayOption 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-enumNamesentries (unlessexplicit-names: verbatimis 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
Underscoreprefix 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-conflictsstays on its existing code path. Combining it withkeep-underscoresis 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 |
- Scope is struct fields generated from object properties only. Other leading symbols (
-,+,$, digits, ...) are unaffected. _idandidin the same object then derive the same field name. That is handled by the existing field-collision logic from fix: resolve Go field name collisions when JSON keys differ only by separator (#2495) #2550: a generation error naming both properties, or automatic renaming withresolve-type-name-collisions.
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, andx-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-nameandx-go-type-namealready get this check from the spec validator today.x-enum-varnames/x-enumNamesentries 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-varnameslists 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-nameextension, without requiringcompatibility.allow-unexported-struct-field-names.
Compatibility
- All three options are opt-in. With none set, no code path changes and all committed
.gen.gofixtures must remain byte-identical. - As with other naming settings, specs that reference each other through
import-mappingmust be generated with the samenamingsettings.
Implementation notes
- The normalizer is currently a single package-level function, and the
Underscore/Minus/Nprefix words come fromtypeNamePrefix, which runs outside the normalizer. Options 1 and 2 need the enum pipeline andProperty.derivedGoFieldNameto call a parameterized variant; other call sites keep using the global normalizer unchanged. ToCamelCaseWithInitialismssplits 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.jsonand document them in the README next toname-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-underscoresis on, across a randomized corpus. - Config validation tests for the rejected
old-enum-conflictscombination and for invalid verbatimx-enum-varnamesentries.
Out of scope
- User-supplied normalizer functions or rewrite rules (Custom Name Normalizer? #1766).
- Collisions between enum constants and type names (Naming conflict between enum-constants and type names #1226).
- Choosing a different normalizer per category of name.
Activity
feat: add opt-in naming options for enum constants, fields and explic…
feat: add opt-in naming options for enum constants, fields and explic…