x-oapi-codegen-extra-tags¶
Generate arbitrary struct tags to fields.
Overview¶
If you're making use of a field's struct tags to i.e. apply custom validation,
decide whether something should be logged, etc, you can use x-oapi-codegen-extra-tags to set
additional tags for your generated types.
Example¶
openapi: "3.0.0"
info:
version: 1.0.0
title: x-oapi-codegen-extra-tags
components:
schemas:
Client:
type: object
required:
- name
- id
properties:
name:
type: string
id:
type: number
ClientWithExtension:
type: object
required:
- name
- id
properties:
name:
type: string
id:
type: number
x-oapi-codegen-extra-tags:
validate: "required,min=1,max=256"
safe-to-log: "true"
gorm: primarykey
Generated Code¶
From here, we now get two different models:
type Client struct {
Name string `json:"name" validate:"required"`
ID float32 `json:"id" validate:"required"`
}
type ClientWithExtension struct {
Name string `json:"name" validate:"required"`
ID float32 `gorm:"primarykey" json:"id" safe-to-log:"true" validate:"required,min=1,max=256"`
}
Use Cases¶
Common use cases for extra tags include:
- ORM mapping: Add GORM, SQLBoiler, or other ORM tags
- Logging control: Mark fields as safe or unsafe to log
- Serialization: Add tags for other serialization formats (XML, YAML, etc.)
- Custom metadata: Any custom tags your application needs
Full Example¶
You can see this in more detail in the example code.
Related Extensions¶
x-go-json-ignore- Ignore fields when (un)marshaling JSONx-sensitive-data- Automatically mask sensitive data