x-go-type and x-go-type-import¶
Override the generated type definition (and optionally, add an import from another package).
Overview¶
Using the x-go-type (and optionally, x-go-type-import when you need to import another package) allows overriding the type that oapi-codegen determined the generated type should be.
Example¶
openapi: "3.0.0"
info:
version: 1.0.0
title: x-go-type
components:
schemas:
Client:
type: object
required:
- name
properties:
name:
type: string
id:
type: number
ClientWithExtension:
type: object
required:
- name
properties:
name:
type: string
# this is a bit of a contrived example, as you could instead use
# `format: uuid` but it explains how you'd do this when there may be
# a clash, for instance if you already had a `uuid` package that was
# being imported, or ...
x-go-type: googleuuid.UUID
x-go-type-import:
path: github.com/google/uuid
name: googleuuid
id:
type: number
# ... this is also a bit of a contrived example, as you could use
# `type: integer` but in the case that you know better than what
# oapi-codegen is generating, like so:
x-go-type: int64
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 googleuuid.UUID `json:"name" validate:"required"`
ID *int64 `json:"id,omitempty"`
}
Full Example¶
You can see this in more detail in the example code.
Related Extensions¶
x-go-name- Override the generated name of a field or typex-go-type-name- Override the generated name of a type only