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
oapi-codegen v2 produces duplicate Go field names when JSON keys differ only by separator (hyphen vs underscore) #2495
When two JSON properties in the same object differ only by their separator — e.g. "host-fqdn" and "host_fqdn" — both are converted to the same PascalCase Go field name HostFqdn, causing a redeclared in this block compile error. Since - is an invalid Go identifier character, stripping it is unavoidable. But _ is a valid Go identifier character and should be treated differently (e.g. preserved, or collision-detected and suffixed), rather than being normalized identically to -.
Spec snippet — Tenable Vulnerability Management API
"info": {
"type": "object",
"properties": {
"host-fqdn": {
"type": "string",
"description": "The host's fully qualified domain name; optional."
},
"host_fqdn": {
"type": "string",
"description": "The FQDN of the host. Normally, this is populated with the value used to scan the host in the target list of the last scan that was ran where it was seen; always present."
},
"host-ip": {
"type": "string",
"description": "The host's IPv4 address; optional."
}
}
}
Generated output:
// Both JSON properties produce the identical Go field name:
HostFqdn *string `json:"host-fqdn,omitempty"` // from "host-fqdn" ← line 39495
...
HostFqdn *string `json:"host_fqdn,omitempty"` // from "host_fqdn" ← line 39507 DUPLICATE
Expected behaviour
The generator should detect that two JSON property names produce the same Go identifier after normalization and either:
Preserve the underscore to produce a distinct name (e.g. Host_fqdn), or
Emit a descriptive error/warning directing the user to resolve the conflict via x-go-name
Producing uncompilable output silently is not acceptable.
We have all kinds of heuristics to try to avoid name collisions in OpenAPI specs, but they don't always work, and a little bit of manual configuration is needed to work around cases like this.
Activity
mromaszewicz commentedon Jul 26, 2026
We have all kinds of heuristics to try to avoid name collisions in OpenAPI specs, but they don't always work, and a little bit of manual configuration is needed to work around cases like this.
You can use the x-go-name extension to override our guess for one of these names. Or use an overlay as a workaround (https://github.com/oapi-codegen/oapi-codegen/blob/main/README.md#modifying-the-input-openapi-specification-with-openapi-overlay) for now.
This should be automatic, but it's also difficult to do this deterministically. I guess we could suffix numbers on colliding fields in the future.
I'll leave this open until I can fix it, but you have two good workarounds in place.
fix: resolve Go field name collisions when JSON keys differ only by s…