Skip to content

x-go-type-name

Override the generated name of a type.

Overview

Note

Notice that this is subtly different to the x-go-name, which also applies to fields within structs.

By default, oapi-codegen will attempt to generate the name of 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-type-name.

Example

openapi: "3.0.0"
info:
  version: 1.0.0
  title: x-go-type-name
components:
  schemas:
    Client:
      type: object
      required:
        - name
      properties:
        name:
          type: string
        id:
          type: number
    ClientWithExtension:
      type: object
      x-go-type-name: ClientRenamedByExtension
      required:
        - name
      properties:
        name:
          type: string
        id:
          type: number
          # NOTE attempting a `x-go-type-name` here is a no-op, as we're not producing a _type_ only a _field_
          x-go-type-name: ThisWillNotBeUsed

Generated Code

From here, we now get two different models and a type alias:

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

Full Example

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

  • x-go-name - Override the generated name of a field or type
  • x-go-type - Override the generated type definition