Skip to content

Breaking change introduced by “Handle leading underscores in property names” #2163

Closed

Description

@biancabunaciu

I believe the recent change to handle leading underscores in property names has introduced a breaking change in how the oapi-codegen generates structs. #1822

This renaming alters the generated code and breaks existing code that relies on the previous field names. While I understand the goal of avoiding duplicate names, this change modifies the generated code and therefore should be considered a breaking change.

Additionally, it exposes implementation details of the OpenAPI spec (like the underscore prefix) into the generated Go code, which can be undesirable when consumers expect normalized field names.

What I expected

The generated field names to stay consistent with previous behavior, or at least a clear note that this change breaks compatibility (e.g. via a major version bump or release note).

What happened

The generated code now prefixes fields with Underscore, breaking compatibility with previously generated code.

Suggestions

Maybe this behavior could be:

  • Behind a flag, or
  • Only enforced when duplicate fields are present, or
  • Called out clearly in the changelog or release notes if it’s meant to be permanent

Activity

  1. jamietanna commented on Nov 22, 2025

    @jamietanna
    Member

    Thanks for the feedback.

    Via , we didn't consider this a breaking change.

    I'll get this explicitly documented in #2168 for the future as to what does/doesn't count as a breaking change

    Additionally, it exposes implementation details of the OpenAPI spec (like the underscore prefix) into the generated Go code, which can be undesirable when consumers expect normalized field names.

    Yes this is true - but if the spec is returning i.e. _version, then it may be expected by consumers that it may be named like so?

    I've not seen how other generators (i.e. in Typescript, Java, etc) handle this sort of case, but it'd be interesting to hear what they would do.

    Given this is a change inside typeNamePrefix, which is not currently wired in to be able to use CompatibilityOptions, I don't think we'll make it possible to opt-out of this change.

    Only enforced when duplicate fields are present

    This sounds reasonable - we'd welcome a PR to do so, which would also need a Compatibility Option to allow keeping the existing behavior.

    It's also possible for you to work around this - in the meantime - by using an Overlay to add an x-go-name to override the generated type names

  2. mromaszewicz commented on Nov 21, 2026

    @mromaszewicz
    Member

    As of #2683 there is an opt-in setting that restores the field names from before the leading-underscore change.

    Add this to your configuration:

    output-options:
      naming:
        properties:
          leading-underscore: drop

    For example, with this schema:

    components:
      schemas:
        Document:
          type: object
          properties:
            _id:
              type: string
            _type:
              type: string

    the generated code is now:

    type Document struct {
    	Id   *string `json:"_id,omitempty"`
    	Type *string `json:"_type,omitempty"`
    }

    instead of UnderscoreId and UnderscoreType. The same applies to query, header and cookie parameters, which are fields of an operation's Params struct.

    If an object has both _id and id, the two would get the same field name, which is the case the Underscore prefix was added for. With drop that is reported as an error naming both properties; you can rename one of them with x-go-name, or set output-options.resolve-type-name-collisions: true to have them renamed automatically.

    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 main branch, for example:

    go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@main --config=config.yaml spec.yaml

    The other naming options are described in the README under Adjusting how names are normalized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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