Documentation
¶
Overview ¶
Package openapi generates OpenAPI specs from Fox routes and handler signatures.
Index ¶
- Constants
- func ApplyFilters(spec *openapi3.T, filters ...Filter) error
- func ApplySpecMetadata(spec *openapi3.T, metadata SpecMetadata)
- func CleanHandlerName(name string) string
- func HTTPBearerSecurity(description string) *openapi3.SecurityScheme
- func JSONHandler(g *Generator) fox.HandlerFunc
- func MarshalSpecJSON(spec *openapi3.T) ([]byte, error)
- func MarshalSpecYAML(spec *openapi3.T) ([]byte, error)
- func Mount(router Router, g *Generator, opts ...MountOption)
- func YAMLHandler(g *Generator) fox.HandlerFunc
- type Filter
- type Generator
- func (g *Generator) Err() error
- func (g *Generator) JSON() ([]byte, error)
- func (g *Generator) Regenerate()
- func (g *Generator) Spec() *openapi3.T
- func (g *Generator) SpecErr() (*openapi3.T, error)
- func (g *Generator) Warnings() []string
- func (g *Generator) WarningsErr() ([]string, error)
- func (g *Generator) WriteYAML(w io.Writer) error
- func (g *Generator) YAML() ([]byte, error)
- type MountOption
- type OAuthFlowConfig
- type OAuthFlowsConfig
- type OperationContext
- type OperationOption
- func Deprecated(value bool) OperationOption
- func Description(value string) OperationOption
- func OperationID(value string) OperationOption
- func Response(status int, body any, description string) OperationOption
- func Security(name string, scopes ...string) OperationOption
- func Summary(value string) OperationOption
- func Tags(values ...string) OperationOption
- type Option
- func Group(prefix string, opts ...OperationOption) Option
- func Info(title, version string) Option
- func Operation(method, path string, opts ...OperationOption) Option
- func RegisterFormatter(typ reflect.Type, schema *openapi3.Schema) Option
- func SecurityScheme(name string, scheme *openapi3.SecurityScheme) Option
- func SecuritySchemeFromConfig(name string, scheme SecuritySchemeConfig) Option
- func Server(url string) Option
- func SetErrorSchema(body any) Option
- func Source(paths []string, opts ...SourceOption) Option
- func WithFilters(filters ...Filter) Option
- type RouteManifest
- type RouteManifestField
- type RouteManifestRoute
- type RouteManifestType
- type Router
- type SecuritySchemeConfig
- type SourceOption
- type SpecExternalDocs
- type SpecMetadata
- type SpecTag
Constants ¶
const RouteManifestVersion = "fox.route-manifest/v1"
RouteManifestVersion is the supported Fox route manifest format version.
Variables ¶
This section is empty.
Functions ¶
func ApplyFilters ¶ added in v0.4.0
ApplyFilters applies filters in order.
func ApplySpecMetadata ¶
func ApplySpecMetadata(spec *openapi3.T, metadata SpecMetadata)
ApplySpecMetadata applies top-level metadata to a generated OpenAPI model.
func CleanHandlerName ¶ added in v0.4.0
CleanHandlerName strips runtime decorations from a Go handler symbol.
func HTTPBearerSecurity ¶
func HTTPBearerSecurity(description string) *openapi3.SecurityScheme
HTTPBearerSecurity creates an HTTP bearer security scheme.
func JSONHandler ¶
func JSONHandler(g *Generator) fox.HandlerFunc
JSONHandler returns a Fox handler that serves the generated JSON spec.
func MarshalSpecJSON ¶
MarshalSpecJSON serializes an OpenAPI model as formatted JSON.
func MarshalSpecYAML ¶
MarshalSpecYAML serializes an OpenAPI model as YAML.
func Mount ¶
func Mount(router Router, g *Generator, opts ...MountOption)
Mount registers YAML and JSON spec endpoints on the router.
router accepts *fox.Engine or *fox.RouterGroup. Mount forces spec generation immediately so the spec endpoints themselves are not included in the generated paths. Routes registered after Mount are not picked up unless the caller invokes Generator.Regenerate().
func YAMLHandler ¶
func YAMLHandler(g *Generator) fox.HandlerFunc
YAMLHandler returns a Fox handler that serves the generated YAML spec.
Types ¶
type Filter ¶ added in v0.4.0
Filter mutates a generated OpenAPI document.
func ExcludeOperationsWithExtensionValue ¶ added in v0.4.0
ExcludeOperationsWithExtensionValue removes operations whose extension equals the provided scalar value.
func FilterOperationExpression ¶ added in v0.4.0
FilterOperationExpression builds an operation filter from a small expression language: "field = value", "field == value", or "field != value". Field names currently address operation extensions such as x-public or x-product.
func FilterOperations ¶ added in v0.4.0
func FilterOperations(keep func(OperationContext) bool) Filter
FilterOperations removes operations for which keep returns false. Paths with no remaining operations are removed.
func PruneUnusedComponents ¶ added in v0.4.0
func PruneUnusedComponents() Filter
PruneUnusedComponents removes components that are no longer reachable from paths, operations, top-level security requirements, or other reachable components.
func StripOperationExtension ¶ added in v0.4.0
StripOperationExtension removes an extension from all retained operations.
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
Generator builds and serializes an OpenAPI specification for a Fox engine.
Generation is lazy: routes registered after New() are still picked up on the next Spec()/JSON()/YAML() call. Schemas are cached across regenerations so repeated calls only re-walk the route table.
func NewFromRouteManifest ¶ added in v0.4.0
func NewFromRouteManifest(manifest RouteManifest, opts ...Option) *Generator
NewFromRouteManifest creates a Generator from a Fox route manifest instead of a live Engine.
func (*Generator) Regenerate ¶
func (g *Generator) Regenerate()
Regenerate forces a full re-scan of the engine's routes on the next access. Useful when routes are added dynamically and the caller wants the next JSON()/YAML() call to reflect them without retaining stale state.
func (*Generator) Spec ¶
Spec returns the generated OpenAPI model. Generation errors are ignored; call SpecErr or Err when the generator was configured with filters that can fail.
func (*Generator) SpecErr ¶ added in v0.4.0
SpecErr returns the generated OpenAPI model and any generation error.
func (*Generator) Warnings ¶
Warnings returns non-fatal generation warnings, triggering generation if it has not yet happened. Call WarningsErr or Err to inspect fatal generation errors.
func (*Generator) WarningsErr ¶ added in v0.4.0
WarningsErr returns non-fatal generation warnings and any generation error.
type MountOption ¶
type MountOption func(*mountConfig)
MountOption configures OpenAPI spec endpoint mounting.
func MountJSON ¶
func MountJSON(path string) MountOption
MountJSON sets the JSON spec endpoint path. Empty string disables.
func MountYAML ¶
func MountYAML(path string) MountOption
MountYAML sets the YAML spec endpoint path. Empty string disables.
type OAuthFlowConfig ¶
type OAuthFlowConfig struct {
AuthorizationURL string
TokenURL string
RefreshURL string
Scopes map[string]string
}
OAuthFlowConfig describes a serializable OAuth2 flow.
type OAuthFlowsConfig ¶
type OAuthFlowsConfig struct {
Implicit *OAuthFlowConfig
Password *OAuthFlowConfig
ClientCredentials *OAuthFlowConfig
AuthorizationCode *OAuthFlowConfig
}
OAuthFlowsConfig describes serializable OAuth2 flows.
type OperationContext ¶ added in v0.4.0
type OperationContext struct {
OperationID string
Path string
Method string
PathItem *openapi3.PathItem
Operation *openapi3.Operation
}
OperationContext describes an operation while filtering.
func (OperationContext) Extension ¶ added in v0.4.0
func (o OperationContext) Extension(name string) (any, bool)
Extension returns an operation extension value.
func (OperationContext) ExtensionBoolDefault ¶ added in v0.4.0
func (o OperationContext) ExtensionBoolDefault(name string, fallback bool) bool
ExtensionBoolDefault returns a boolean extension value or fallback when the extension is absent or not a boolean.
type OperationOption ¶
type OperationOption func(*operationDoc)
OperationOption configures explicit metadata for one operation.
func Deprecated ¶
func Deprecated(value bool) OperationOption
Deprecated marks the operation as deprecated.
func Description ¶
func Description(value string) OperationOption
Description sets the operation description.
func Response ¶
func Response(status int, body any, description string) OperationOption
Response adds or replaces a response for the operation.
func Security ¶
func Security(name string, scopes ...string) OperationOption
Security adds a security requirement to the operation.
type Option ¶
type Option func(*Generator)
Option configures a Generator.
func Group ¶
func Group(prefix string, opts ...OperationOption) Option
Group adds metadata to operations whose Fox route path starts with prefix.
func Operation ¶
func Operation(method, path string, opts ...OperationOption) Option
Operation adds explicit metadata for a registered route.
func RegisterFormatter ¶
RegisterFormatter overrides schema generation for a Go type.
func SecurityScheme ¶
func SecurityScheme(name string, scheme *openapi3.SecurityScheme) Option
SecurityScheme registers an OpenAPI security scheme.
func SecuritySchemeFromConfig ¶
func SecuritySchemeFromConfig(name string, scheme SecuritySchemeConfig) Option
SecuritySchemeFromConfig registers a serializable OpenAPI security scheme.
func SetErrorSchema ¶
SetErrorSchema overrides the default error response schema.
func Source ¶
func Source(paths []string, opts ...SourceOption) Option
Source enables Go comment extraction from the provided paths. A path ending in "/..." recursively walks the tree skipping vendor and dot-prefixed directories. Test files (_test.go) are skipped unless IncludeTestFiles is passed.
func WithFilters ¶ added in v0.4.0
WithFilters applies post-generation filters before the spec is serialized.
type RouteManifest ¶ added in v0.4.0
type RouteManifest struct {
Version string `json:"version"`
Routes []RouteManifestRoute `json:"routes"`
}
RouteManifest is the route registry exchange format written by Fox.
type RouteManifestField ¶ added in v0.4.0
type RouteManifestField struct {
Name string `json:"name"`
PkgPath string `json:"pkgPath,omitempty"`
Tag string `json:"tag,omitempty"`
Anonymous bool `json:"anonymous,omitempty"`
Type RouteManifestType `json:"type"`
}
RouteManifestField is a serializable subset of Go reflect.StructField.
type RouteManifestRoute ¶ added in v0.4.0
type RouteManifestRoute struct {
Method string `json:"method"`
Path string `json:"path"`
Handler string `json:"handler,omitempty"`
HandlerName string `json:"handlerName,omitempty"`
HandlerType string `json:"handlerType,omitempty"`
Inputs []string `json:"inputs,omitempty"`
Results []string `json:"results,omitempty"`
InputTypes []RouteManifestType `json:"inputTypes,omitempty"`
ResultTypes []RouteManifestType `json:"resultTypes,omitempty"`
}
RouteManifestRoute describes one registered Fox route.
func (RouteManifestRoute) HandlerSymbol ¶ added in v0.4.0
func (route RouteManifestRoute) HandlerSymbol() string
type RouteManifestType ¶ added in v0.4.0
type RouteManifestType struct {
Kind string `json:"kind"`
String string `json:"string,omitempty"`
Name string `json:"name,omitempty"`
PkgPath string `json:"pkgPath,omitempty"`
TypeArgs []RouteManifestType `json:"typeArgs,omitempty"`
Key *RouteManifestType `json:"key,omitempty"`
Elem *RouteManifestType `json:"elem,omitempty"`
Fields []RouteManifestField `json:"fields,omitempty"`
}
RouteManifestType is a serializable subset of Go reflect.Type.
type Router ¶
type Router interface {
GET(relativePath string, handlers ...fox.HandlerFunc) gin.IRoutes
}
Router is the minimal surface Mount needs — both *fox.Engine and *fox.RouterGroup satisfy it.
type SecuritySchemeConfig ¶
type SecuritySchemeConfig struct {
Type string
Description string
Name string
In string
Scheme string
BearerFormat string
OpenIDConnectURL string
Flows *OAuthFlowsConfig
}
SecuritySchemeConfig describes a serializable OpenAPI security scheme.
type SourceOption ¶
type SourceOption func(*sourceConfig)
SourceOption configures Source.
func IncludeTestFiles ¶
func IncludeTestFiles() SourceOption
IncludeTestFiles makes Source also scan _test.go files. By default test files are skipped so comments authored in tests do not bleed into production specs; this option exists for the rare case where handler documentation lives in test fixtures.
type SpecExternalDocs ¶
SpecExternalDocs describes top-level tag external documentation.
type SpecMetadata ¶
SpecMetadata contains top-level OpenAPI metadata used by generated drivers.
type SpecTag ¶
type SpecTag struct {
Name string
Description string
ExternalDocs *SpecExternalDocs
}
SpecTag describes a top-level OpenAPI tag.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
fox-openapi
command
|
|
|
internal
|
|
|
collisionfixture/v1/users
Package users in v1 path collides on short name with v2/users.
|
Package users in v1 path collides on short name with v2/users. |
|
collisionfixture/v2/users
Package users in v2 path collides on short name with v1/users.
|
Package users in v2 path collides on short name with v1/users. |