Skip to content

x-go-type-skip-optional-pointer

Do not add a pointer type for optional fields in structs.

Overview

By default, oapi-codegen will generate a pointer for optional fields.

Using the x-go-type-skip-optional-pointer extension allows omitting that pointer.

Example

openapi: "3.0.0"
info:
  version: 1.0.0
  title: x-go-type-skip-optional-pointer
components:
  schemas:
    Client:
      type: object
      required:
        - name
      properties:
        name:
          type: string
        id:
          type: number
    ClientWithExtension:
      type: object
      required:
        - name
      properties:
        name:
          type: string
        id:
          type: number
          x-go-type-skip-optional-pointer: true

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 ClientWithExtension struct {
    Name string  `json:"name" validate:"required"`
    ID   float32 `json:"id"`
}

Full Example

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

  • x-omitempty - Force the presence of the JSON tag omitempty on a field