Repository navigation
fix: resolve Go field name collisions when JSON keys differ only by separator (#2495) - #2550
Conversation
[Medium impact] The PR appears safe to merge; the repeated-blank-field regression is fixed. SummaryAdds optional fixes for colliding Go field names while preserving JSON names and explicit
Reviews (4) · Last reviewed commit: "Don't treat blank fields as a name colli..." · Reviewed by Greptile |
…eparator (oapi-codegen#2495) Move collision resolution from GenFieldsFromProperties to a single-source resolver (ResolvePropertyGoFieldNameCollisions) that populates an unexported resolvedGoFieldName on each Property. GoFieldName() now returns the resolved name, so the struct declaration AND the generated marshal/unmarshal boilerplate (union, additional-properties templates) all share the same disambiguated field name. Also reserves the synthetic AdditionalProperties field name so a real property named 'additionalProperties' does not collide with it. Fixes oapi-codegen#2495
ba4b9de to
c1eb68a
Compare
|
thanks for the careful review, both points were valid. i reworked the fix so it is a single source of truth instead of only touching the struct declaration:
added a test for the additional-properties reservation case as well. full |
…ision-hyphen-underscore Conflicts in pkg/codegen/schema.go and schema_test.go were adjacent additions; both sides are kept. Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
Thanks for the rework — the marshal/unmarshal boilerplate and the synthetic Two problems with the naming scheme itself mean this can't merge as it stands. 1. Names are not stable as a spec evolvesNames are handed out in sorted property order, and the first property keeps the bare name. # before
properties:
host_fqdn: {type: string} # -> HostFqdn
# after the API adds a property
properties:
host-fqdn: {type: string} # -> HostFqdn
host_fqdn: {type: string} # -> HostFqdn2
A suffixed name can also take another property's natural name: properties:
host-fqdn: {type: string} # -> HostFqdn
host_fqdn: {type: string} # -> HostFqdn2
host_fqdn2: {type: integer} # -> HostFqdn22
2. An explicit
|
Numbering the fields of colliding properties made a field's name depend on its siblings. Properties are named in sorted order and the first kept the bare name, so adding "host-fqdn" to a schema which had "host_fqdn" handed HostFqdn to the new property, and code using it went on compiling against a different JSON property. A numbered name could also take the name another property derives on its own, and a name given with x-go-name was renumbered like any other. A collision is now handled the way colliding type names are. Without output-options.resolve-type-name-collisions it is an error which names the properties and points at x-go-name and at the option. With the option, every colliding property is renamed to its derived name and a short hash of its JSON name, such as HostFqdn_cea8c90, the scheme shared path parameters already use. No property keeps the bare name, so a name depends on nothing but its own property. A name given with x-go-name is never changed, nor is the AdditionalProperties field, and two of those colliding is an error. The option keeps its name, so that existing configurations keep working, and its documentation now says that it covers field names too. Only output which did not compile changes: regenerating every fixture changes nothing but the cases added to internal/test/naming/conflicts. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Explain, next to x-go-name, why two properties can get the same Go field name, the error that reports it, and the two ways to resolve it: naming a field with x-go-name, or resolve-type-name-collisions. Co-Authored-By: Claude Fable 5.2 <[email protected]>
|
Following up on my earlier comment: I've pushed two commits on top of your branch that replace the numeric suffixes. Your approach of storing the resolved name on the Behaviour nowWe already have an opt-in resolver for colliding type names ( Option off (the default): a collision fails generation with an error that names the properties and both fixes: Option on: every colliding property is renamed to its derived name plus a short hash of its JSON name: type FieldCollisions struct {
HostFqdn_cea8c90 *string `json:"host-fqdn,omitempty"`
HostFqdn_bcac5e8 *string `json:"host_fqdn,omitempty"`
HostFqdn2 *int `json:"host_fqdn2,omitempty"`
Zed_ce5c161 *string `json:"zed,omitempty"`
Zed *int `json:"zzz,omitempty"` // x-go-name: Zed
}Why
Also in these commits
Regenerating all fixtures changes nothing except the cases added here, so only output that failed to compile is affected. Colliding query parameters are out of scope for this PR. The PR title and description still describe numeric suffixes; I'll write the squash commit message to match what is merged. |
A struct may declare the blank identifier any number of times, so two properties named "_" with x-go-name, under allow-unexported-struct-field-names and x-oapi-codegen-only-honour-go-name, generated code which compiled. The collision check reported them as one field declared twice, and resolve-type-name-collisions could not help, since both names are explicit. Leave "_" out of the collision check. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Problem
When two JSON properties in the same object differ only by their separator — e.g.
host-fqdnandhost_fqdn— both normalize to the same Go field nameHostFqdn, producing aredeclared in this blockcompile error in the generated code.-is not a valid Go identifier character so stripping it is unavoidable, and_folds to the same PascalCase, so the two collide.This is reproducible against real specs (the issue references the Tenable Vulnerability Management API, whose
infoobject contains bothhost-fqdnandhost_fqdn).Fixes #2495
Fix
Resolve Go field-name collisions within a struct in
GenFieldsFromProperties. The first property to claim a name keeps it; each later property that would collide gets an incrementing numeric suffix (HostFqdn,HostFqdn2,HostFqdn3, ...), re-checked so a suffixed name cannot itself collide. This matches the direction suggested in the issue ("suffix numbers on colliding fields") and is consistent with the collision-resolution the codebase already does elsewhere (enum constants, type names).Only the Go identifier changes — the JSON struct tag still uses the original
JsonFieldName, so the wire format is unchanged and round-trips exactly as before. Explicitx-go-nameoverrides continue to work and take precedence.Tests
Added
TestGenFieldsFromProperties_ResolvesGoFieldNameCollisions: two properties (host-fqdn,host_fqdn) now generateHostFqdnandHostFqdn2with their original JSON tags preserved. Verified the test FAILS without the fix (both fields render asHostFqdn, matching the reported compile error) and PASSES with it. Fullpkg/codegensuite passes;gofmtandgo vetare clean.