Skip to content

x-go-name

Override the generated name of a field or a type.

Overview

By default, oapi-codegen will attempt to generate the name of fields and types in as best a way it can.

However, sometimes, the name doesn't quite fit what your codebase standards are, or the intent of the field, so you can override it with x-go-name.

Example

openapi: "3.0.0"
info:
  version: 1.0.0
  title: x-go-name
paths:
  /clients:
    post:
      operationId: createClient
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ClientWithExtension'
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ClientWithExtension'
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: AccountIdentifier

Generated Code

From here, we now get two different models:

type Client struct {
    Name string   `json:"name" validate:"required"`
    ID   *float32 `json:"id,omitempty"`
}
type ClientRenamedByExtension struct {
    Name              string   `json:"name" validate:"required"`
    AccountIdentifier *float32 `json:"id,omitempty"`
}

Full Example

You can see this in more detail in the example code.

  • x-go-type-name - Override the generated name of a type only (not fields)
  • x-go-type - Override the generated type definition