Help us shape the future of Go! Take the 2026 Go Developer Survey

openapi

package
v0.17.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 18 Imported by: 1

Documentation

Overview

Package openapi generates an OpenAPI 3.1 document from the router: typed routes (api.Register) are described from their input and output types and `validate` tags; plain routes get a generic entry.

doc := openapi.Generate(r.Routes(), openapi.Info{Title: "Shop", Version: "1.0.0"})

Index

Constants

This section is empty.

Variables

View Source
var ErrNoOperation = errors.New("openapi: no operation matches the request")

ErrNoOperation is returned when the document has no operation for the request.

Functions

func TypeScript

func TypeScript(d *Document, zod bool) (string, error)

TypeScript returns TypeScript declarations for the document: one type per component schema, plus <Operation>Body, <Operation>Query and <Operation>Response for every operation with an operationId. With zod set, Zod schemas (<Name>Schema) for the components follow the types.

Types

type Components

type Components struct {
	Schemas         map[string]Schema   `json:"schemas,omitempty"`
	Responses       map[string]Response `json:"responses,omitempty"`
	SecuritySchemes map[string]Schema   `json:"securitySchemes,omitempty"`
}

Components holds reusable schemas, responses and security schemes.

type Document

type Document struct {
	OpenAPI    string                           `json:"openapi"`
	Info       docInfo                          `json:"info"`
	Servers    []server                         `json:"servers,omitempty"`
	Paths      map[string]map[string]*Operation `json:"paths"`
	Components Components                       `json:"components"`
}

Document is an OpenAPI 3.1 document.

func Generate

func Generate(routes []router.RouteInfo, info Info) *Document

Generate builds the document for routes (router.Routes()).

func (*Document) ValidateResponse

func (d *Document) ValidateResponse(method, path string, status int, body []byte) error

ValidateResponse checks a response body against the document: the operation for method and path (a concrete URL path), then the schema declared for status, or the "default" response. It supports the JSON Schema subset Generate emits.

type Field

type Field struct {
	Key      string
	Type     reflect.Type
	Rules    []string
	Required bool
	Doc      string
}

Field is one JSON property of a struct.

func Fields

func Fields(t reflect.Type) []Field

Fields lists the JSON properties of struct t (embedded structs are flattened, `json:"-"` is skipped), with their validate rules.

type Info

type Info struct {
	Title       string
	Version     string
	Description string
	Servers     []string
	// PathPrefix keeps only routes under it (e.g. "/api"); empty keeps all.
	PathPrefix string
	// SecuritySchemes adds or replaces schemes; "bearer" (HTTP bearer
	// token) is predefined.
	SecuritySchemes map[string]Schema
}

Info configures the document.

type MediaType

type MediaType struct {
	Schema Schema `json:"schema"`
}

MediaType wraps a schema for one content type.

type Operation

type Operation struct {
	OperationID string                `json:"operationId,omitempty"`
	Summary     string                `json:"summary,omitempty"`
	Description string                `json:"description,omitempty"`
	Tags        []string              `json:"tags,omitempty"`
	Deprecated  bool                  `json:"deprecated,omitempty"`
	Parameters  []Parameter           `json:"parameters,omitempty"`
	RequestBody *RequestBody          `json:"requestBody,omitempty"`
	Responses   map[string]Response   `json:"responses"`
	Security    []map[string][]string `json:"security,omitempty"`
}

Operation is one method on a path.

type Parameter

type Parameter struct {
	Name        string `json:"name"`
	In          string `json:"in"`
	Required    bool   `json:"required,omitempty"`
	Description string `json:"description,omitempty"`
	Schema      Schema `json:"schema"`
}

Parameter is a path or query parameter.

type RequestBody

type RequestBody struct {
	Required bool                 `json:"required"`
	Content  map[string]MediaType `json:"content"`
}

RequestBody is an operation's body.

type Response

type Response struct {
	Ref         string               `json:"$ref,omitempty"`
	Description string               `json:"description,omitempty"`
	Content     map[string]MediaType `json:"content,omitempty"`
}

Response is a response or a $ref to a shared one.

type Schema

type Schema = map[string]any

Schema is a JSON Schema (2020-12, as used by OpenAPI 3.1) object.

type Schemas

type Schemas struct {
	// contains filtered or unexported fields
}

Schemas turns Go types into JSON Schemas, collecting named structs as reusable components. Constraints come from the same `validate` tags the z package enforces, so the document matches what the server accepts.

func NewSchemas

func NewSchemas() *Schemas

NewSchemas returns an empty component registry.

func (*Schemas) Components

func (s *Schemas) Components() map[string]Schema

Components returns the named schemas collected so far.

func (*Schemas) For

func (s *Schemas) For(t reflect.Type) Schema

For returns the schema of t: a $ref for named structs, inline otherwise.

func (*Schemas) Object

func (s *Schemas) Object(t reflect.Type, skip map[string]bool) Schema

Object returns the inline object schema of struct t without the properties named in skip (e.g. path parameters).

type Skip

type Skip struct{}

Skip hides a route from the document: r.Get(...).Doc(openapi.Skip{}).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL