openapi

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package openapi generates OpenAPI 3.1 documents from Go types and validator tags.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidOption    = errors.New("openapi: invalid option")
	ErrInvalidOperation = errors.New("openapi: invalid operation")
	ErrOperationExists  = errors.New("openapi: operation already exists")
)

Errors returned for invalid generator configuration and operations.

Functions

func DocsHTML

func DocsHTML(title, specURL string) []byte

DocsHTML returns a self-contained documentation page rendering the spec at specURL with Scalar (https://github.com/scalar/scalar).

Types

type Components

type Components struct {
	Schemas map[string]*Schema `json:"schemas,omitempty"`
}

Components contains reusable schemas.

type Document

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

Document is the generated OpenAPI 3.1 document.

type Generator

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

Generator accumulates operations. It is not safe for concurrent use.

func MustNew added in v0.2.1

func MustNew(title, version string, opts ...Option) *Generator

MustNew is New with panic-on-error semantics.

func New

func New(title, version string, opts ...Option) (*Generator, error)

New constructs a Generator.

func (*Generator) Add

func (g *Generator) Add[Request any](method, path string, opts ...OperationOption) error

Add registers an operation. At least one WithResponse or WithDefaultResponse option is required. Generation is transactional: an error leaves paths, components, and schema-name allocation unchanged.

func (*Generator) Document

func (g *Generator) Document() Document

Document returns a deep copy.

func (*Generator) JSON

func (g *Generator) JSON() ([]byte, error)

JSON returns deterministic, indented JSON.

type Info

type Info struct {
	Title   string `json:"title"`
	Version string `json:"version"`
}

Info identifies the API.

type MediaType

type MediaType struct {
	Schema *Schema `json:"schema,omitempty"`
}

MediaType associates content with a schema.

type NoBody added in v0.2.1

type NoBody struct{}

NoBody marks a request or response without JSON content.

type Operation

type Operation struct {
	Summary     string               `json:"summary,omitempty,omitzero"`
	Tags        []string             `json:"tags,omitempty"`
	Parameters  []*Parameter         `json:"parameters,omitempty"`
	RequestBody *RequestBody         `json:"requestBody,omitempty"`
	Responses   map[string]*Response `json:"responses"`
}

Operation describes one HTTP operation.

type OperationOption added in v0.2.1

type OperationOption func(*operationConfig) error

OperationOption configures one operation.

func WithDefaultResponse added in v0.2.1

func WithDefaultResponse[Body any](opts ...ResponseOption) OperationOption

WithDefaultResponse adds the OpenAPI "default" response.

func WithResponse added in v0.2.1

func WithResponse[Body any](status int, opts ...ResponseOption) OperationOption

WithResponse adds a status response whose JSON body is Body. Use NoBody for responses without content.

func WithSummary added in v0.2.1

func WithSummary(summary string) OperationOption

WithSummary sets the operation summary.

func WithTags added in v0.2.1

func WithTags(tags ...string) OperationOption

WithTags appends non-empty operation tags.

type Option

type Option func(*Generator) error

Option configures a Generator.

func WithSchema added in v0.2.1

func WithSchema[T any](schema *Schema) Option

WithSchema replaces the inferred schema for T. The input is copied.

func WithValidator

func WithValidator(v *validator.Validator) Option

WithValidator selects the Validator used to inspect validate tags.

type Parameter

type Parameter struct {
	Name     string  `json:"name"`
	In       string  `json:"in"` // "path" or "query"
	Required bool    `json:"required,omitempty,omitzero"`
	Schema   *Schema `json:"schema,omitempty"`
}

Parameter describes a path or query parameter.

type PathItem

type PathItem map[string]*Operation

PathItem maps a lowercase HTTP method to its operation.

type RequestBody

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

RequestBody describes a JSON request body.

type Response

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

Response describes one HTTP response.

type ResponseOption added in v0.2.1

type ResponseOption func(*responseOptions) error

ResponseOption configures one response.

func ResponseDescription added in v0.2.1

func ResponseDescription(description string) ResponseOption

ResponseDescription overrides the default HTTP status description.

type Schema

type Schema struct {
	Ref                  string             `json:"$ref,omitempty,omitzero"`
	Type                 string             `json:"type,omitempty,omitzero"`
	Format               string             `json:"format,omitempty,omitzero"`
	Description          string             `json:"description,omitempty,omitzero"`
	Properties           map[string]*Schema `json:"properties,omitempty"`
	Items                *Schema            `json:"items,omitempty"`
	AdditionalProperties *Schema            `json:"additionalProperties,omitempty"`
	Required             []string           `json:"required,omitempty"`
	Enum                 []any              `json:"enum,omitempty"`
	Pattern              string             `json:"pattern,omitempty,omitzero"`
	Minimum              *float64           `json:"minimum,omitempty"`
	Maximum              *float64           `json:"maximum,omitempty"`
	ExclusiveMinimum     *float64           `json:"exclusiveMinimum,omitempty"`
	ExclusiveMaximum     *float64           `json:"exclusiveMaximum,omitempty"`
	MinLength            *uint64            `json:"minLength,omitempty"`
	MaxLength            *uint64            `json:"maxLength,omitempty"`
	MinItems             *uint64            `json:"minItems,omitempty"`
	MaxItems             *uint64            `json:"maxItems,omitempty"`
	UniqueItems          bool               `json:"uniqueItems,omitempty,omitzero"`
	// ContentEncoding marks base64 payloads ([]byte fields), per JSON Schema 2020-12.
	ContentEncoding string `json:"contentEncoding,omitempty,omitzero"`
}

Schema is the JSON Schema subset used by both parameters and bodies.

Jump to

Keyboard shortcuts

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