-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Respect initialisms for schema generation #454
Description
Per the Go code review guide, initialisms like ID, DB, etc. should have consistent casing like id or ID. However, the schema generation functionality is just converting the properties to UpperCamelCase and disregarding this rule, which produces structs with fields like Id and Db.
The culprit seems to be that schema.go calls ToCamelCase, a homegrown case converter. Packages like iancoleman/strcase support custom acronyms so that users can specify these initialism overrides. We don't necessarily need to import this new package but it's a good illustration of how this might be implemented.
One option is to maintain the current behavior but add a new configuration flag -initialisms with values like ID,HTTP,DB that would override those parts of the property name.
Another option is to create a new property extension that would allow users to override the struct field name in the Open API definition, e.g. x-oapi-codegen-name. Maybe something similar for overriding the json tag as well if necessary. The risk with this approach is that we will alienate people that are using an OpenAPI doc that they do not own and therefore cannot modify to add these extended properties.
Some test cases:
dbName- Expected: DBName string `json:"dbName"`
- Actual: DbName string `json:"dbName"`
id- Expected: ID string `json:"id"`
- Actual: Id string `json:"id"`
someHttpThing- Expected: SomeHTTPThing string `json:"someHttpThing"` (json tag is not changed)
- Actual: SomeHttpThing string `json:"someHttpThing"`
some_http_thing- Expected: SomeHTTPThing string `json:"some_http_thing"`
- Actual: SomeHttpThing string `json:"some_http_thing"`