openapi

package
v1.0.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package openapi generates an OpenAPI 3.1 document from the route table. Because handlers are typed, there is nothing to annotate: parameters come from the In struct tags, the request body from the Body field, and the response from Out. Route options add the rest:

r = r.Prefix("/users").Use(openapi.Tags("users"))
r.Get("/{id}", dygo.Handle(c.Get), openapi.Summary("Get a user"), openapi.Response(404, "No such user"))

Serve it by importing the module:

Imports: []*dygo.Module{openapi.Module(openapi.Info{Title: "Users API", Version: "1.0"})}

which adds GET /openapi.json and a Swagger UI at GET /docs.

Struct tags understood on fields: json, the binding tags (path, query, header, cookie, form, file, body, default, required), validate (required, min, max, gte, lte, gt, lt, len, email, url, uuid, oneof), description and example.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Deprecated

func Deprecated() dygo.RouteOption

Deprecated marks the operation deprecated.

func Description

func Description(s string) dygo.RouteOption

Description sets the operation description.

func Exclude

func Exclude() dygo.RouteOption

Exclude leaves the route out of the document (@ApiExcludeEndpoint).

func Module

func Module(o Options) *dygo.Module

Module serves the document and the UI. The routes are version neutral and excluded from the document; exclude their paths from a global prefix if you want them at the root.

func OperationID

func OperationID(id string) dygo.RouteOption

OperationID overrides the generated operation ID (Controller_Handler).

func Public

func Public() dygo.RouteOption

Public marks a route that needs no authentication when BearerAuth is on.

func Response

func Response(status int, description string) dygo.RouteOption

Response documents an error response with the standard error body (@ApiResponse). Controller and route responses are merged.

func ResponseOf

func ResponseOf[T any](status int, description string) dygo.RouteOption

ResponseOf documents a response whose body is T.

func Summary

func Summary(s string) dygo.RouteOption

Summary sets the operation summary (@ApiOperation({summary})).

func Tags

func Tags(t ...string) dygo.RouteOption

Tags groups operations (@ApiTags). Controller and route tags are merged.

Types

type Components

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

Components holds shared schemas.

type Document

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

Document is an OpenAPI 3.1 document.

func Generate

func Generate(routes []dygo.RouteInfo, o Options) *Document

Generate builds the document for routes, usually app.Routes().

type Info

type Info struct {
	Title       string `json:"title"`
	Version     string `json:"version"`
	Description string `json:"description,omitempty"`
}

Info describes the API.

type MediaType

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

MediaType holds a body schema.

type Operation

type Operation struct {
	OperationID string                  `json:"operationId"`
	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]*ResponseObj `json:"responses"`
	Security    []map[string][]string   `json:"security,omitempty"`
}

Operation is one route.

func (*Operation) MarshalJSON

func (o *Operation) MarshalJSON() ([]byte, error)

MarshalJSON keeps an empty Security list, which means "no auth".

type Options

type Options struct {
	// Info defaults to title "API", version "1.0".
	Info Info
	// Servers lists base URLs, such as "https://api.example.com".
	Servers []string
	// BearerAuth adds an HTTP bearer security scheme required by every
	// operation not marked Public().
	BearerAuth bool
	// JSONPath defaults to "/openapi.json"; DocsPath to "/docs". Set DocsPath
	// to "-" to serve no UI.
	JSONPath, DocsPath string
	// SwaggerUIURL is the base of the swagger-ui-dist files. Defaults to a
	// pinned jsDelivr URL.
	SwaggerUIURL string
	// Use adds route options to the document and UI routes, for example your
	// auth guard's "public" marker.
	Use []dygo.RouteOption
}

Options configure Module and Generate.

type Parameter

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

Parameter is a path, query, header or cookie input.

type PathItem

type PathItem struct {
	Get     *Operation `json:"get,omitempty"`
	Put     *Operation `json:"put,omitempty"`
	Post    *Operation `json:"post,omitempty"`
	Delete  *Operation `json:"delete,omitempty"`
	Options *Operation `json:"options,omitempty"`
	Head    *Operation `json:"head,omitempty"`
	Patch   *Operation `json:"patch,omitempty"`
}

PathItem holds the operations of one path.

type RequestBody

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

RequestBody is the body input.

type ResponseDoc

type ResponseDoc struct {
	Status      int
	Description string
	// Type is the body type; nil means the standard error body.
	Type reflect.Type
}

ResponseDoc documents an extra response.

type ResponseObj

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

ResponseObj is one response.

type Schema

type Schema struct {
	Ref                  string             `json:"$ref,omitempty"`
	Type                 any                `json:"type,omitempty"` // string, or []string with "null"
	Format               string             `json:"format,omitempty"`
	Description          string             `json:"description,omitempty"`
	Properties           map[string]*Schema `json:"properties,omitempty"`
	Required             []string           `json:"required,omitempty"`
	Items                *Schema            `json:"items,omitempty"`
	AdditionalProperties *Schema            `json:"additionalProperties,omitempty"`
	Enum                 []any              `json:"enum,omitempty"`
	Default              any                `json:"default,omitempty"`
	Examples             []any              `json:"examples,omitempty"`
	Minimum              *float64           `json:"minimum,omitempty"`
	Maximum              *float64           `json:"maximum,omitempty"`
	ExclusiveMinimum     *float64           `json:"exclusiveMinimum,omitempty"`
	ExclusiveMaximum     *float64           `json:"exclusiveMaximum,omitempty"`
	MinLength            *int               `json:"minLength,omitempty"`
	MaxLength            *int               `json:"maxLength,omitempty"`
	MinItems             *int               `json:"minItems,omitempty"`
	MaxItems             *int               `json:"maxItems,omitempty"`
}

Schema is a JSON Schema (the subset Dygo generates).

type SecurityScheme

type SecurityScheme struct {
	Type   string `json:"type"`
	Scheme string `json:"scheme,omitempty"`
}

SecurityScheme is an authentication method.

type Server

type Server struct {
	URL string `json:"url"`
}

Server is a base URL.

Jump to

Keyboard shortcuts

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