Skip to main content

Parameters & Models

Define request and response models using Go structs. Parameters are declared using struct tags that map to OpenAPI parameter locations.

Request Body

Use option.Request() to set the request body model. The json tag controls the field name in the serialized body:

type CreateUserRequest struct {
Name string `json:"name" required:"true"`
Email string `json:"email" required:"true"`
}

r.Post("/users",
option.Summary("Create user"),
option.Request(new(CreateUserRequest)),
option.Response(201, new(User)),
)

For application/x-www-form-urlencoded or multipart/form-data bodies, use the form tag:

type UploadRequest struct {
File string `form:"file" required:"true"`
Label string `form:"label"`
}

r.Post("/upload",
option.Request(new(UploadRequest), option.ContentType("multipart/form-data")),
)

Path Parameters

Path parameters are defined with the path struct tag:

type GetUserRequest struct {
ID string `path:"id" required:"true" description:"User identifier"`
}

r.Get("/users/{id}",
option.Request(new(GetUserRequest)),
option.Response(200, new(User)),
)

Query Parameters

Use the query struct tag:

type ListUsersRequest struct {
Page int `query:"page" description:"Page number" minimum:"1"`
PageSize int `query:"page_size" description:"Items per page" minimum:"1" maximum:"100"`
Search string `query:"search" description:"Search term"`
Active *bool `query:"active" description:"Filter by active status"`
}

r.Get("/users",
option.Request(new(ListUsersRequest)),
option.Response(200, new([]User)),
)

OpenAPI 3.2 Querystring Parameters

For OpenAPI 3.2.0, you can model whole-query-string parameters with the querystring tag:

type SearchRequest struct {
Raw string `querystring:"q" mediaType:"application/x-www-form-urlencoded"`
}

Header Parameters

Use the header struct tag:

type AuthenticatedRequest struct {
APIKey string `header:"X-API-Key" required:"true" description:"API authentication key"`
Locale string `header:"Accept-Language" description:"Preferred language"`
}

Responses

Use option.Response() to define responses. Multiple responses can be added per route:

r.Get("/users/{id}",
option.Request(new(GetUserRequest)),
option.Response(200, new(User),
option.ContentDescription("Successful response"),
option.ContentType("application/json"),
),
option.Response(404, new(ErrorResponse),
option.ContentDescription("User not found"),
),
option.Response(401, new(ErrorResponse)),
)

Schema Validation Tags

Rich schema constraints are defined with struct tags:

type CreateUserRequest struct {
Name string `json:"name" required:"true" minLength:"2" maxLength:"50"`
Email string `json:"email" required:"true" format:"email"`
Age int `json:"age" minimum:"18" maximum:"120"`
Tags []string `json:"tags" maxItems:"10"`
Bio string `json:"bio,omitempty" description:"User biography"`
Role string `json:"role" enum:"admin,user,guest"`
}

Naming tags

TagApplies toDescription
json:"name"body fieldsField name in JSON request/response body
form:"name"body fieldsField name in form-encoded or multipart body
query:"name"query paramsField name as a query parameter
path:"name"path paramsField name as a path parameter
header:"name"header paramsField name as a request header
querystring:"name"OAS 3.2Whole-struct as a querystring parameter

Use json:"-" to exclude a field from any schema. Any field with json:"-" is also excluded from non-JSON schemes.

Overriding tag sources

The reflector supports overriding which struct tag is used for a particular parameter location. Use option.ParameterTagMapping (via option.WithReflectorConfig) to change the tag source for path, query, header, and now also body and form parameters.

Example (use uri tags for path parameters and form tags for form bodies):

r := spec.NewRouter(
option.WithReflectorConfig(
option.ParameterTagMapping(openapi.ParameterInPath, "uri"),
option.ParameterTagMapping(openapi.ParameterInForm, "form"),
),
)

This is useful when your framework uses different tag names (e.g., uri, form) than the defaults (json, form).

Embedded struct referencing and refer:"true"

By default embedded structs are inlined into the parent schema. To preserve them as component references (using allOf: [{"$ref": "..."}]), implement the EmbedReferencer interface or annotate the embedded field with refer:"true".

This keeps component reuse and makes generated schemas easier to read and maintain.

Schema constraint tags

TagTypeDescription
required:"true"boolMark field as required
type:"string"stringOverride inferred type (supports comma-separated types in OAS 3.1+)
title:"..."stringSchema title
description:"..."stringField description
format:"email"stringOpenAPI format (email, uri, date-time, uuid, etc.)
default:"..."anyDefault value (parsed as JSON, then bool/int/float/string)
example:"..."anyExample value
examples:"[...]"JSON arrayMultiple examples — OAS 3.1+ only
enum:"a,b,c"listAllowed values (comma-separated or JSON array)
const:"v"anyConstant value — OAS 3.1+ only
pattern:"..."stringRegex pattern
minLength:"n"intMinimum string length
maxLength:"n"intMaximum string length
minimum:"n"floatMinimum numeric value
maximum:"n"floatMaximum numeric value
exclusiveMinimum:"n"float/boolExclusive lower bound (bool in OAS 3.0, number in OAS 3.1+)
exclusiveMaximum:"n"float/boolExclusive upper bound (bool in OAS 3.0, number in OAS 3.1+)
multipleOf:"n"floatValue must be a multiple of n
minItems:"n"intMinimum array items
maxItems:"n"intMaximum array items
uniqueItems:"true"boolArray items must be unique
minProperties:"n"intMinimum object properties
maxProperties:"n"intMaximum object properties
nullable:"true"boolAllow null — OAS 3.0 only (use type:"t,null" in OAS 3.1+)
readOnly:"true"boolField appears only in responses
writeOnly:"true"boolField appears only in requests
deprecated:"true"boolMark field as deprecated
contentEncoding:"..."stringBinary encoding hint (base64, etc.) — OAS 3.1+ only
contentMediaType:"..."stringMedia type of string content — OAS 3.1+ only
mediaType:"..."stringMedia type for querystring parameter content

XML tags

TagDescription
xmlName:"..."XML element name
xmlNamespace:"..."XML namespace URI
xmlPrefix:"..."XML namespace prefix
xmlAttribute:"true"Render as XML attribute instead of element
xmlWrapped:"true"Wrap array items in a parent element
xmlNodeType:"..."XML node type — OAS 3.2 only (attribute, element)

Generic Response Types

Generic types are supported for wrapping responses:

type APIResponse[T any] struct {
Success bool `json:"success"`
Data T `json:"data,omitempty"`
Error string `json:"error,omitempty"`
Timestamp string `json:"timestamp"`
}

// In routes
r.Get("/users/{id}",
option.Response(200, new(APIResponse[User])),
)

r.Get("/users",
option.Response(200, new(APIResponse[[]User])),
)

Content Options Reference

OptionDescription
ContentDescription(d)Set response description
ContentType(t)Override content type (default: application/json)
ContentDefault(true)Mark as the default response