Skip to content

bug - inconsistent underscore handling for property names in 2.5.0 #2032

Closed

Description

@shadowCow

We have some properties in our open api spec formatted like _UPPER_SNAKE_CASE (note the leading underscore).

Prior to 2.5.0, the generated go property names omitted all the underscores, so it would generate:

    UPPERSNAKECASE string `json:"_UPPER_SNAKE_CASE"`

In 2.5.0, it now translates the leading _ to the literal text Underscore:

    UnderscoreUPPERSNAKECASE string `json:"_UPPER_SNAKE_CASE"`

More complete example of the yaml spec:

/my-endpoint:
    post:
      summary: a summary
      description: a description
      operationId: anId
      requestBody:
        description: a description
        content:
          application/json:
            schema:
              type: object
              properties:
                _UPPER_SNAKE_CASE:
                  type: string

Activity

  1. added
    bug
    on Jul 16, 2025
  2. jamietanna commented on Jul 16, 2025

    @jamietanna
    Member

    Hmm, so this is an interesting one because of the mixed case - the Underscore prefix goes through the name-normalizer Output Option, which doesn't know that this specific field is intended to be SNAKE_CASE.

    As a short-term, using x-go-name and x-oapi-codegen-only-honour-go-name via should give you the ability to override it locally, and continue to use v2.5.0

    Will have a think about how best to handle this, given the default would be to use PascalCase (which we refer to as CamelCase, incorrectly 🫣)

  3. added and removed
    bug
    on Jul 16, 2025
  4. mromaszewicz commented on Nov 21, 2026

    @mromaszewicz
    Member

    As of #2683 there is an opt-in setting that brings back the pre-2.5.0 field names for properties that start with an underscore.

    Add this to your configuration:

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

    With a property like the one in this issue:

    components:
      schemas:
        Thing:
          type: object
          properties:
            _UPPER_SNAKE_CASE:
              type: string

    the generated code is now:

    type Thing struct {
    	UPPERSNAKECASE *string `json:"_UPPER_SNAKE_CASE,omitempty"`
    }

    The Underscore prefix was added in 2.5.0 so that _id and id in the same object would not end up as the same field. With drop, that case 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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions