Documentation
¶
Overview ¶
Package openapi generates OpenAPI 3.1 documents from plain Go types: the same struct tags that drive github.com/libtnb/validator (validate, json, uri, query) become schemas, parameters and constraints — the validation rules are the documentation, so the two can never drift apart.
g := openapi.New("my-api", "1.0.0")
g.Add(http.MethodPost, "/users", openapi.Op{
Summary: "Create a user",
Request: request.UserAdd{}, // validate:"required && min:3" -> minLength: 3
Response: biz.User{},
})
blob, _ := g.JSON()
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DocsHTML ¶
DocsHTML returns a self-contained documentation page rendering the spec at specURL with Scalar (https://github.com/scalar/scalar).
Types ¶
type Components ¶
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"`
}
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator accumulates operations into an OpenAPI 3.1 document. It is not safe for concurrent use; build the document from one goroutine.
type Op ¶
type Op struct {
Summary string
Tags []string
Request any // uri tags -> path params, query tags -> query params, json tags -> body
Response any // the 200-response body; nil means no content
Status int // response status, default 200
}
Op describes one operation. Request and Response are sample values (or zero structs); their types drive everything else.
type Option ¶
type Option func(*Generator)
func WithType ¶
WithType overrides the schema of a type the reflector cannot guess, e.g. WithType(carbon.DateTime{}, &Schema{Type: "string", Format: "date-time"}).
func WithValidator ¶
WithValidator uses v to introspect validation rules. Default: the package-level default validator.
type RequestBody ¶
type Schema ¶
type Schema struct {
Ref string `json:"$ref,omitempty"`
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Description string `json:"description,omitempty"`
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"`
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"`
// ContentEncoding marks base64 payloads ([]byte fields), per JSON Schema 2020-12.
ContentEncoding string `json:"contentEncoding,omitempty"`
}
Schema is the JSON Schema subset used by both parameters and bodies.