build

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package build provides OpenAPI specification building from route metadata.

The Builder type accumulates API metadata (servers, tags, security schemes) and generates version-agnostic specifications from enriched route information. The specifications can then be projected to any OpenAPI version using the export package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

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

Builder builds OpenAPI specifications from enriched route information.

A Builder accumulates API metadata (servers, tags, security schemes) and generates a version-agnostic specification from route definitions. The specification can then be projected to any OpenAPI version using the export package.

func NewBuilder

func NewBuilder(info model.Info) *Builder

NewBuilder creates a new builder with the given API info.

func (*Builder) AddSecurityScheme

func (b *Builder) AddSecurityScheme(name string, s *model.SecurityScheme) *Builder

AddSecurityScheme adds a security scheme to the specification.

func (*Builder) AddServer

func (b *Builder) AddServer(url, desc string) *Builder

AddServer adds a server URL to the specification.

func (*Builder) AddServerVariable

func (b *Builder) AddServerVariable(name string, variable *model.ServerVariable) *Builder

AddServerVariable adds a variable to the last added server for URL template substitution.

IMPORTANT: AddServerVariable must be called AFTER AddServer. It applies to the most recently added server. Validation occurs when Build() is called.

func (*Builder) AddServerWithExtensions

func (b *Builder) AddServerWithExtensions(url, desc string, extensions map[string]any) *Builder

AddServerWithExtensions adds a server URL with extensions to the specification.

func (*Builder) AddTag

func (b *Builder) AddTag(name, desc string) *Builder

AddTag adds a tag definition to the specification.

func (*Builder) AddTagWithExtensions

func (b *Builder) AddTagWithExtensions(name, desc string, extensions map[string]any) *Builder

AddTagWithExtensions adds a tag definition with extensions to the specification.

func (*Builder) AddTagWithExternalDocs

func (b *Builder) AddTagWithExternalDocs(name, desc string, extDocs *model.ExternalDocs, extensions map[string]any) *Builder

AddTagWithExternalDocs adds a tag definition with external docs and extensions.

func (*Builder) Build

func (b *Builder) Build(routes []EnrichedRoute) (*model.Spec, error)

Build builds the complete specification from enriched routes.

func (*Builder) SetExternalDocs

func (b *Builder) SetExternalDocs(docs *model.ExternalDocs) *Builder

SetExternalDocs sets external documentation links.

func (*Builder) SetGlobalSecurity

func (b *Builder) SetGlobalSecurity(reqs []model.SecurityRequirement) *Builder

SetGlobalSecurity sets global security requirements applied to all operations.

type ConstraintKind added in v0.2.0

type ConstraintKind uint8

ConstraintKind represents the type of constraint on a path parameter.

const (
	ConstraintNone     ConstraintKind = iota
	ConstraintInt                     // OpenAPI: type integer, format int64
	ConstraintFloat                   // OpenAPI: type number, format double
	ConstraintUUID                    // OpenAPI: type string, format uuid
	ConstraintRegex                   // OpenAPI: type string, pattern
	ConstraintEnum                    // OpenAPI: type string, enum
	ConstraintDate                    // OpenAPI: type string, format date
	ConstraintDateTime                // OpenAPI: type string, format date-time
)

type EnrichedRoute

type EnrichedRoute struct {
	RouteInfo RouteInfo
	Doc       *RouteDoc
}

EnrichedRoute combines route information with OpenAPI documentation.

This type is used to pass route data to Builder.Build() for spec generation. The Doc field may be nil if the route has no OpenAPI documentation.

type ExampleData added in v0.2.0

type ExampleData struct {
	Name          string
	Summary       string
	Description   string
	Value         any
	ExternalValue string
}

ExampleData holds example data to avoid import cycles with openapi package.

type PathConstraint added in v0.2.0

type PathConstraint struct {
	Kind    ConstraintKind
	Pattern string   // for ConstraintRegex
	Enum    []string // for ConstraintEnum
}

PathConstraint describes a typed constraint for a path parameter. These constraints map directly to OpenAPI schema types.

type RouteDoc

type RouteDoc struct {
	Summary               string
	Description           string
	OperationID           string
	Tags                  []string
	Deprecated            bool
	Consumes              []string
	Produces              []string
	RequestType           reflect.Type
	RequestMetadata       *schema.RequestMetadata
	RequestExample        any           // Single unnamed example
	RequestNamedExamples  []ExampleData // Named examples
	ResponseTypes         map[int]reflect.Type
	ResponseExample       map[int]any           // Single unnamed example per status
	ResponseNamedExamples map[int][]ExampleData // Named examples per status
	Security              []SecurityReq
	Extensions            map[string]any // Operation-level extensions (x-*)
}

RouteDoc holds all OpenAPI metadata for a route. This is a copy of the openapi.RouteDoc structure to avoid import cycles.

type RouteInfo

type RouteInfo struct {
	Method          string                    // HTTP method (GET, POST, etc.)
	Path            string                    // URL path with parameters (e.g. "/users/:id")
	PathConstraints map[string]PathConstraint // Typed constraints for path parameters
}

RouteInfo contains basic route information needed for OpenAPI generation. This avoids importing the openapi package to prevent import cycles.

type SecurityReq

type SecurityReq struct {
	Scheme string
	Scopes []string
}

SecurityReq represents a security requirement for an operation.

Jump to

Keyboard shortcuts

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