Skip to content

oapi-codegen v2 produces duplicate Go field names when JSON keys differ only by separator (hyphen vs underscore) #2495

Description

@ChalamaiahReddy

Issue

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 -.

Version:

oapi-codegen Version: v2.7.2

Spec source:
https://developer.tenable.com/openapi/vulnerability-management.json
Section:
GET /scans/{scan_uuid}/hosts/{host_id}/plugins/{plugin_id} response — info.host-fqdn and info.host_fqdn coexist in the same object.

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions