openapi

package
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2026 License: MPL-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package openapi provides utilities for generating, serving, and filtering OpenAPI specifications, as well as rendering interactive API documentation using providers such as Scalar, Stoplight Elements, and Swagger UI.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefineErrors

func DefineErrors(op *core.Operation, registry core.Registry, factory core.ErrorHandler, discovered ...[]core.DiscoveredError)

DefineErrors populates the error responses on an operation based on its declared error status codes, the error schema from the factory, and any discovered errors.

func Downgrade

func Downgrade(oapi *core.OpenAPI) ([]byte, error)

Downgrade converts an OpenAPI 3.1+ spec to OpenAPI 3.0 compatible JSON by transforming features like type arrays, exclusive min/max, and content encoding.

func DowngradeYAML

func DowngradeYAML(oapi *core.OpenAPI) ([]byte, error)

DowngradeYAML converts an OpenAPI 3.1+ spec to OpenAPI 3.0 compatible YAML.

func FilterByTag

func FilterByTag(oapi *core.OpenAPI, tag string) *core.OpenAPI

FilterByTag returns a copy of the OpenAPI spec containing only operations that have the specified tag.

func FilterExcludeTag

func FilterExcludeTag(oapi *core.OpenAPI, tag string) *core.OpenAPI

FilterExcludeTag returns a copy of the OpenAPI spec with all operations that have the specified tag removed.

func GenerateInternalSpec

func GenerateInternalSpec(oapi *core.OpenAPI, hiddenOps []*core.Operation) (*core.OpenAPI, error)

GenerateInternalSpec returns a copy of the OpenAPI spec with hidden operations merged in, intended for internal-only consumption.

func GenerateInternalSpecJSON

func GenerateInternalSpecJSON(oapi *core.OpenAPI, hiddenOps []*core.Operation) ([]byte, error)

GenerateInternalSpecJSON returns the internal OpenAPI spec as JSON, including hidden operations, hidden parameters, and hidden schema properties.

func RegisterDocsRoute

func RegisterDocsRoute(adapter core.Adapter, oapi *core.OpenAPI, config core.Config)

RegisterDocsRoute registers an HTTP route that serves an interactive API documentation page using the configured documentation provider.

func RegisterErrorDocRoutes

func RegisterErrorDocRoutes(adapter core.Adapter, factory core.ErrorHandler, config core.Config)

RegisterErrorDocRoutes registers HTTP routes that serve human-readable error documentation pages (HTML and JSON) for each HTTP status code used by the API.

func RegisterInternalSpecRoutes

func RegisterInternalSpecRoutes(adapter core.Adapter, api core.API, config core.Config)

RegisterInternalSpecRoutes registers HTTP routes that serve the internal OpenAPI spec (JSON and YAML) and an internal documentation page.

func RegisterSpecRoutes

func RegisterSpecRoutes(adapter core.Adapter, oapi *core.OpenAPI, config core.Config)

RegisterSpecRoutes registers HTTP routes that serve the public OpenAPI spec in JSON and YAML formats, and optionally individual schema routes.

func YAML

func YAML(oapi *core.OpenAPI) ([]byte, error)

YAML serializes the OpenAPI spec to YAML without downgrading the version.

Types

type ScalarProvider

type ScalarProvider struct {
	LocalJSPath string
}

ScalarProvider renders API documentation using the Scalar UI. Set LocalJSPath to serve the Scalar JavaScript bundle from a local path instead of the CDN.

func (ScalarProvider) Render

func (p ScalarProvider) Render(specURL string, title string) string

Render returns an HTML page that loads the Scalar API reference UI for the given spec URL.

type SchemaLinkTransformer added in v1.1.0

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

SchemaLinkTransformer adds a Link header with rel="describedBy" and a $schema field to JSON response bodies, pointing to the JSON Schema that describes the response structure (RFC 8288).

func NewSchemaLinkTransformer added in v1.1.0

func NewSchemaLinkTransformer(schemasPath string) *SchemaLinkTransformer

NewSchemaLinkTransformer creates a transformer that adds a Link header with rel="describedBy" and a $schema field to response bodies.

func (*SchemaLinkTransformer) OnAddOperation added in v1.1.0

func (t *SchemaLinkTransformer) OnAddOperation(oapi *core.OpenAPI, op *core.Operation)

OnAddOperation is triggered whenever a new operation is added to the API, enabling this transformer to precompute and cache information about the response and schema.

func (*SchemaLinkTransformer) Transform added in v1.1.0

func (t *SchemaLinkTransformer) Transform(ctx core.Context, status string, v any) (any, error)

Transform adds the Link header and $schema field to the response.

type StoplightProvider

type StoplightProvider struct{}

StoplightProvider renders API documentation using Stoplight Elements.

func (StoplightProvider) Render

func (p StoplightProvider) Render(specURL string, title string) string

Render returns an HTML page that loads the Stoplight Elements UI for the given spec URL.

type SwaggerUIProvider

type SwaggerUIProvider struct {
	OAuthClientID string
	OAuthScopes   []string
}

SwaggerUIProvider renders API documentation using Swagger UI. Set OAuthClientID and OAuthScopes to enable OAuth2 authorization in the UI.

func (SwaggerUIProvider) Render

func (p SwaggerUIProvider) Render(specURL string, title string) string

Render returns an HTML page that loads the Swagger UI for the given spec URL.

Jump to

Keyboard shortcuts

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