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 ¶
- func Deprecated() dygo.RouteOption
- func Description(s string) dygo.RouteOption
- func Exclude() dygo.RouteOption
- func Module(o Options) *dygo.Module
- func OperationID(id string) dygo.RouteOption
- func Public() dygo.RouteOption
- func Response(status int, description string) dygo.RouteOption
- func ResponseOf[T any](status int, description string) dygo.RouteOption
- func Summary(s string) dygo.RouteOption
- func Tags(t ...string) dygo.RouteOption
- type Components
- type Document
- type Info
- type MediaType
- type Operation
- type Options
- type Parameter
- type PathItem
- type RequestBody
- type ResponseDoc
- type ResponseObj
- type Schema
- type SecurityScheme
- type Server
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 ¶
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.
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 ¶
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 ¶
SecurityScheme is an authentication method.