You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
underscores are incorrectly removed in enums #1996
// Package oapi provides primitives to interact with the openapi HTTP API.
//
// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.4.1 DO NOT EDIT.
package oapi
// Defines values for SomeSchema.
const (
SOMEENUMNAME SomeSchema = "SOME_ENUM_NAME"
)
// SomeSchema defines model for SomeSchema.
type SomeSchema string
POSSIBLE BUGS?
a. maybe this is the wrong conversion, for an enum item name
b. maybe ToCamelCase should not remove underscores
c. not really a bug, but the name ToCamelCase does not describe this code. In the code (and its comment), ABC_DEF does not become abcDef; it becomes all-capitals ABCDEF
ToCamelCase is used in the functions like renameSChema
// ToCamelCase will convert query-arg style strings to CamelCase. We will
// use `., -, +, :, ;, _, ~, ' ', (, ), {, }, [, ]` as valid delimiters for words.
// So, "word.word-word+word:word;word_word~word word(word)word{word}[word]"
// would be converted to WordWordWordWordWordWordWordWordWordWordWordWordWord
BUG SYMPTOM
An enum named "ABC_DEF" should not be renamed to "ABCDEF", it should remain "ABC_DEF".
IMO, there could be three ore more functions:
a. ToCamelCase, which would turn "ABC_DEF #$@" into "abcDef"
b. ToEnumCharset, which would turn "ABC_DEF #$@" into "ABC_DEF"
c. ToStrictCharset, which would be the new name for ToCamelCase. it would still turn "ABC_DEF #$@" into "ABCDEF"
I don't know what the rules for Schema, Request, Response, and RequestBody are. Each of them would use the matching function above, or require a different "To...()" function.
Also, for Enum maybe there should be a new renameEnum() function.
the body of the function implements the rule described in the comment:
func ToCamelCase(str string) string { s := strings.Trim(str, " ") n := ""
capNext := true
for _, v := range s {
if unicode.IsUpper(v) {
n += string(v)
}
if unicode.IsDigit(v) {
n += string(v)
}
if unicode.IsLower(v) {
if capNext {
n += strings.ToUpper(string(v))
} else {
n += string(v)
}
}
_, capNext = separatorSet[v]
}
return n
}
The default can't change, because existing users already have constants like SOMEENUMNAME in their code. As of #2683 there is an opt-in setting that keeps the underscores in enum constant names.
The words between the underscores still go through the name normalizer, so a lower-case value such as some_enum_name becomes Some_Enum_Name. Other separators, like -, are still removed.
The option is opt-in, so nothing changes unless you set it. It will be released in v2.9.0. Until then you need to use the main branch, for example:
go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@main --config=config.yaml spec.yaml
Activity
david-chenevert-lr commentedon Jun 1, 2025
is this the bug?
nameNormalizerto chosen to beToCamelCase, which removes underscores.https://github.com/oapi-codegen/oapi-codegen/blob/main/pkg/codegen/utils.go#L35
POSSIBLE BUGS?
a. maybe this is the wrong conversion, for an enum item name
b. maybe
ToCamelCaseshould not remove underscoresc. not really a bug, but the name ToCamelCase does not describe this code. In the code (and its comment),
ABC_DEFdoes not becomeabcDef; it becomes all-capitalsABCDEFToCamelCaseis used in the functions likerenameSChemahttps://github.com/oapi-codegen/oapi-codegen/blob/main/pkg/codegen/utils.go#L929-L1002
which leads to:
ToCamelCase()function is:https://github.com/oapi-codegen/oapi-codegen/blob/main/pkg/codegen/utils.go#L220-L2233
BUG SYMPTOM
An enum named "ABC_DEF" should not be renamed to "ABCDEF", it should remain "ABC_DEF".
IMO, there could be three ore more functions:
a. ToCamelCase, which would turn "ABC_DEF #$@" into "abcDef"
b. ToEnumCharset, which would turn "ABC_DEF #$@" into "ABC_DEF"
c. ToStrictCharset, which would be the new name for ToCamelCase. it would still turn "ABC_DEF #$@" into "ABCDEF"
I don't know what the rules for Schema, Request, Response, and RequestBody are. Each of them would use the matching function above, or require a different "To...()" function.
Also, for Enum maybe there should be a new
renameEnum()function.https://github.com/oapi-codegen/oapi-codegen/blob/main/pkg/codegen/utils.go#L224-L246
mromaszewicz commentedon Nov 21, 2026
The default can't change, because existing users already have constants like
SOMEENUMNAMEin their code. As of #2683 there is an opt-in setting that keeps the underscores in enum constant names.Add this to your configuration:
With the spec from this issue:
the generated code is now:
The words between the underscores still go through the name normalizer, so a lower-case value such as
some_enum_namebecomesSome_Enum_Name. Other separators, like-, are still removed.The option is opt-in, so nothing changes unless you set it. It will be released in v2.9.0. Until then you need to use the
mainbranch, for example:The other naming options are described in the README under Adjusting how names are normalized.