Skip to content

x-go-name Excludes underscores #1851

Closed
Closed

Description

@MattiasMartens

An x-go-name that is specified with underscores, such as API_URL, gets truncated to "APIURL" in the generated code.

A field name of API_URL is valid Go, so i think it is justified to expect that to be the name in the final output.

Input:

openapi: "3.0.0"
info:
  version: 1.0.0
  title: x-go-name
components:
  schemas:
    Client:
      type: object
      required:
        - name
      properties:
        name:
          type: string
        id:
          type: number
    ClientWithExtension:
      type: object
      x-go-name: ClientRenamedByExtension
      required:
        - name
      properties:
        name:
          type: string
        id:
          type: number
          # maybe we want to be intentionally verbose here?
          x-go-name: HARD_CODED_ID

Expected output:

// Package xgoname provides primitives to interact with the openapi HTTP API.
//
// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.4.1 DO NOT EDIT.
package xgoname

// Client defines model for Client.
type Client struct {
	Id   *float32 `json:"id,omitempty"`
	Name string   `json:"name"`
}

// ClientRenamedByExtension defines model for ClientWithExtension.
type ClientRenamedByExtension struct {
	HARD_CODED_ID *float32 `json:"id,omitempty"`
	Name        string   `json:"name"`
}

Actual output:

// Package xgoname provides primitives to interact with the openapi HTTP API.
//
// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.4.1 DO NOT EDIT.
package xgoname

// Client defines model for Client.
type Client struct {
	Id   *float32 `json:"id,omitempty"`
	Name string   `json:"name"`
}

// ClientRenamedByExtension defines model for ClientWithExtension.
type ClientRenamedByExtension struct {
	HARDCODEDID *float32 `json:"id,omitempty"`
	Name        string   `json:"name"`
}

Activity

  1. paulmach commented on Jan 6, 2025

    @paulmach
    Contributor

    This is due to the name being "CamelCased" which removes the _. The code that does that is around here

    return SchemaNameToTypeName(goFieldName)

    Without code changes I think you can make it work by adding x-oapi-codegen-only-honour-go-name: true and using the allow-unexported-struct-field-names global option. There is info about these options in the readme.

    Honestly all of this seems a little weird. I would suggest that x-go-name act as the exact/litteral name used, no CamelCase or Capitalization. This would make it clear, allow support for unexported or names with _, and remove the need for "x-oapi-codegen-only-honour-go-name" and "allow-unexported-struct-field-names" options.

  2. mromaszewicz commented on Nov 21, 2026

    @mromaszewicz
    Member

    As of #2683 there is an opt-in setting that emits an x-go-name on a property exactly as you wrote it, instead of running it through the name normalizer.

    Add this to your configuration:

    output-options:
      naming:
        explicit-names: verbatim

    With the schema from this issue:

    components:
      schemas:
        ClientWithExtension:
          type: object
          x-go-name: ClientRenamedByExtension
          required:
            - name
          properties:
            name:
              type: string
            id:
              type: number
              x-go-name: HARD_CODED_ID

    the generated code is now:

    type ClientRenamedByExtension struct {
    	HARD_CODED_ID *float32 `json:"id,omitempty"`
    	Name          string   `json:"name"`
    }

    With verbatim, the name has to be a valid Go identifier, and it is used as is: it is not capitalized and not renamed if it collides with another field. The setting also applies to x-go-name on parameters and to x-enum-varnames.

    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