openapi

package
v1.11.1 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2026 License: MIT, Apache-2.0, MIT Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const JsonEXT = ".json"
View Source
const OpenapiEXT = ".openapi.json"
View Source
const SwaggerEXT = ".swagger.json"
View Source
const TypeOpenapi = "openapi"

Variables

View Source
var DocDir = "./apidoc/"
View Source
var UriPrefix = "/openapi"

目录结构 ./api/mod/mod.openapi.json 请求路由 /openapi /openapi/mod.openapi.json

Functions

func DocList

func DocList(w http.ResponseWriter, r *http.Request)

func Get

func Get(packageName string) (m map[string]string, err error)

func GetEnums

func GetEnums(ty reflect.Type) ([]any, error)

func OpenApi

func OpenApi(w http.ResponseWriter, r *http.Request)

func Openapi

func Openapi(mux *http.ServeMux, uriPrefix, dir string)

func Redoc

func Redoc(opts RedocOpts, next http.Handler) http.Handler

Redoc creates a middleware to serve a documentation site for a swagger spec.

This allows for altering the spec before starting the http listener.

func Type

func Type(fieldType reflect.Type) string

func WriteToFile

func WriteToFile(docDir, modName string, doc *openapi3.T) error

Types

type API

type API struct {
	// Name of the API.
	Name string
	// Routes of the API.
	// From patterns, to methods, to route.
	Routes map[Pattern]MethodToRoute
	// StripPkgPaths to strip from the type names in the OpenAPI output to avoid
	// leaking internal implementation details such as internal repo names.
	//
	// This increases the risk of type clashes in the OpenAPI output, i.e. two types
	// in different namespaces that are set to be stripped, and have the same type Name
	// could clash.
	//
	// Example values could be "github.com/a-h/rest".
	StripPkgPaths []string

	// KnownTypes are added to the OpenAPI specification output.
	// The default implementation:
	//   Maps time.Time to a string.
	KnownTypes map[reflect.Type]openapi3.Schema

	// ApplyCustomSchemaToType callback to customise the OpenAPI specification for a given type.
	// Apply customisation to a specific type by checking the t parameter.
	// Apply customisations to all types by ignoring the t parameter.
	ApplyCustomSchemaToType func(t reflect.Type, s *openapi3.Schema)
	// contains filtered or unexported fields
}

API is a model of a REST API's routes, along with their request and response types.

func NewAPI

func NewAPI(name string, opts ...APIOpts) *API

NewAPI creates a new API from the router.

func (*API) Connect

func (api *API) Connect(pattern string) (r *Route)

Connect defines a CONNECT request route for the given pattern.

func (*API) Delete

func (api *API) Delete(pattern string) (r *Route)

Delete defines a DELETE request route for the given pattern.

func (*API) Get

func (api *API) Get(pattern string) (r *Route)

Get defines a GET request route for the given pattern.

func (*API) Head

func (api *API) Head(pattern string) (r *Route)

Head defines a HEAD request route for the given pattern.

func (*API) Merge

func (api *API) Merge(r Route)

Merge route data into the existing configuration. This is typically used by adapters, such as the chiadapter to take information that the router already knows and add it to the specification.

func (*API) Options

func (api *API) Options(pattern string) (r *Route)

Options defines an OPTIONS request route for the given pattern.

func (*API) Patch

func (api *API) Patch(pattern string) (r *Route)

Patch defines a PATCH request route for the given pattern.

func (*API) Post

func (api *API) Post(pattern string) (r *Route)

Post defines a POST request route for the given pattern.

func (*API) Put

func (api *API) Put(pattern string) (r *Route)

Put defines a PUT request route for the given pattern.

func (*API) RegisterModel

func (api *API) RegisterModel(model Model, opts ...ModelOpts) (name string, schema *openapi3.Schema, err error)

RegisterModel allows a model to be registered manually so that additional configuration can be applied. The schema returned can be modified as required.

func (*API) Route

func (api *API) Route(method, pattern string) (r *Route)

Route upserts a route to the API definition.

func (*API) Spec

func (api *API) Spec() (spec *openapi3.T, err error)

Spec creates an OpenAPI 3.0 specification document for the API.

func (*API) Trace

func (api *API) Trace(pattern string) (r *Route)

Trace defines an TRACE request route for the given pattern.

type APIOpts

type APIOpts func(*API)

func WithApplyCustomSchemaToType

func WithApplyCustomSchemaToType(f func(t reflect.Type, s *openapi3.Schema)) APIOpts

WithApplyCustomSchemaToType enables customisation of types in the OpenAPI specification. Apply customisation to a specific type by checking the t parameter. Apply customisations to all types by ignoring the t parameter.

type CustomSchemaApplier

type CustomSchemaApplier interface {
	ApplyCustomSchema(s *openapi3.Schema)
}

CustomSchemaApplier is a type that customises its OpenAPI schema.

type Method

type Method string

Method is the HTTP method of the route, e.g. http.MethodGet

type MethodToRoute

type MethodToRoute map[Method]*Route

MethodToRoute maps from a HTTP method to a Route.

type Model

type Model struct {
	Type reflect.Type
	// contains filtered or unexported fields
}

Model is a model used in one or more routes.

func ModelOf

func ModelOf[T any]() Model

ModelOf creates a model of type T.

func (Model) ApplyCustomSchema

func (m Model) ApplyCustomSchema(s *openapi3.Schema)

type ModelOpts

type ModelOpts func(s *openapi3.Schema)

ModelOpts defines options that can be set when registering a model.

func WithDescription

func WithDescription(desc string) ModelOpts

WithDescription sets the description field on the schema.

func WithEnumConstants

func WithEnumConstants[T ~string | constraints.Integer]() ModelOpts

WithEnumConstants sets the property to be an enum containing the values of the type found in the package.

func WithEnumValues

func WithEnumValues[T ~string | constraints.Integer](values ...T) ModelOpts

WithEnumValues sets the property to be an enum value with the specific values.

func WithNullable

func WithNullable() ModelOpts

WithNullable sets the nullable field to true.

type Models

type Models struct {
	Request   Model
	Responses map[int]Model
}

Models defines the models used by a route.

type Parameter

type Parameter struct {
	// Description of the param.
	Description string
	// Regexp is a regular expression used to validate the param.
	// An empty string means that no validation is applied.
	Regexp string
	// Required sets whether the querystring parameter must be present in the URL.
	Required bool
	// AllowEmpty sets whether the querystring parameter can be empty.
	AllowEmpty bool
	// Type of the param (string, number, integer, boolean).
	Type string
	// ApplyCustomSchema customises the OpenAPI schema for the query parameter.
	ApplyCustomSchema func(s *openapi3.Parameter)
}

type Params

type Params struct {
	// Path parameters are used in the path of the URL, e.g. /users/{id} would
	// have a name of "id".
	Path map[string]Parameter
	// Query parameters are used in the querystring of the URL, e.g. /users/?sort={sortOrder} would
	// have a name of "sort".
	Query  map[string]Parameter
	Header map[string]Parameter
}

Params is a route parameter.

type Pattern

type Pattern string

Pattern of the route, e.g. /posts/list, or /users/{id}

type RedocOpts

type RedocOpts struct {
	// BasePath for the UI, defaults to: /
	BasePath string

	// Path combines with BasePath to construct the path to the UI, defaults to: "docs".
	Path string

	// SpecURL is the URL of the spec document.
	//
	// Defaults to: /openapi.json
	SpecURL string

	// Title for the documentation site, default to: API documentation
	Title string

	// Template specifies a custom template to serve the UI
	Template string

	// RedocURL points to the js that generates the redoc site.
	//
	// Defaults to: https://cdn.jsdelivr.net/npm/redoc/bundles/redoc.standalone.js
	RedocURL string
}

RedocOpts configures the Redoc middlewares

func (*RedocOpts) EnsureDefaults

func (r *RedocOpts) EnsureDefaults()

EnsureDefaults in case some options are missing

type Route

type Route struct {
	// Method is the HTTP method of the route, e.g. http.MethodGet
	Method Method
	// Pattern of the route, e.g. /posts/list, or /users/{id}
	Pattern Pattern
	// Params of the route.
	Params Params
	// Models used in the route.
	Models Models
	// Tags used in the route.
	Tags []string
	// OperationID for the route.
	OperationID string
	// Description for the route.
	Description string
}

Route models a single API route.

func (*Route) HasDescription

func (rm *Route) HasDescription(description string) *Route

HasDescription sets the description for the route.

func (*Route) HasOperationID

func (rm *Route) HasOperationID(operationID string) *Route

HasOperationID sets the OperationID for the route.

func (*Route) HasPathParameter

func (rm *Route) HasPathParameter(name string, p Parameter) *Route

HasPathParameter configures a path parameter for the route.

func (*Route) HasQueryParameter

func (rm *Route) HasQueryParameter(name string, q Parameter) *Route

HasQueryParameter configures a query parameter for the route.

func (*Route) HasRequest

func (rm *Route) HasRequest(request Model) *Route

func (*Route) HasRequestModel

func (rm *Route) HasRequestModel(request Model) *Route

HasResponseModel configures the request model of the route. Example:

api.Post("/user").HasRequestModel(http.StatusOK, rest.ModelOf[User]())

func (*Route) HasResponseModel

func (rm *Route) HasResponseModel(status int, response Model) *Route

HasResponseModel configures a response for the route. Example:

api.Get("/user").HasResponseModel(http.StatusOK, rest.ModelOf[User]())

func (*Route) HasTags

func (rm *Route) HasTags(tags []string) *Route

HasTags sets the tags for the route.

Jump to

Keyboard shortcuts

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