Repository navigation
Closed
Description
An x-go-name that is specified with underscores, such as API_URL, gets truncated to "APIURL" in the generated code.
A field name of API_URL is valid Go, so i think it is justified to expect that to be the name in the final output.
Input:
openapi: "3.0.0"
info:
version: 1.0.0
title: x-go-name
components:
schemas:
Client:
type: object
required:
- name
properties:
name:
type: string
id:
type: number
ClientWithExtension:
type: object
x-go-name: ClientRenamedByExtension
required:
- name
properties:
name:
type: string
id:
type: number
# maybe we want to be intentionally verbose here?
x-go-name: HARD_CODED_ID
Expected output:
// Package xgoname 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 xgoname
// Client defines model for Client.
type Client struct {
Id *float32 `json:"id,omitempty"`
Name string `json:"name"`
}
// ClientRenamedByExtension defines model for ClientWithExtension.
type ClientRenamedByExtension struct {
HARD_CODED_ID *float32 `json:"id,omitempty"`
Name string `json:"name"`
}
Actual output:
// Package xgoname 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 xgoname
// Client defines model for Client.
type Client struct {
Id *float32 `json:"id,omitempty"`
Name string `json:"name"`
}
// ClientRenamedByExtension defines model for ClientWithExtension.
type ClientRenamedByExtension struct {
HARDCODEDID *float32 `json:"id,omitempty"`
Name string `json:"name"`
}
Metadata
Metadata
Assignees
Labels
No labels
Activity
paulmach commentedon Jan 6, 2025
This is due to the name being "CamelCased" which removes the
_. The code that does that is around hereoapi-codegen/pkg/codegen/schema.go
Line 113 in 8bbe226
Without code changes I think you can make it work by adding
x-oapi-codegen-only-honour-go-name: trueand using theallow-unexported-struct-field-namesglobal option. There is info about these options in the readme.Honestly all of this seems a little weird. I would suggest that
x-go-nameact as the exact/litteral name used, no CamelCase or Capitalization. This would make it clear, allow support for unexported or names with_, and remove the need for "x-oapi-codegen-only-honour-go-name" and "allow-unexported-struct-field-names" options.mromaszewicz commentedon Nov 21, 2026
As of #2683 there is an opt-in setting that emits an
x-go-nameon a property exactly as you wrote it, instead of running it through the name normalizer.Add this to your configuration:
With the schema from this issue:
the generated code is now:
With
verbatim, the name has to be a valid Go identifier, and it is used as is: it is not capitalized and not renamed if it collides with another field. The setting also applies tox-go-nameon parameters and tox-enum-varnames.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.