openapi

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2026 License: MIT Imports: 11 Imported by: 4

Documentation

Overview

Package openapi exposes the public contracts for loader and parser stages, mirroring the wrapper strategy described in go-form-gen.md:25-159 and go-form-gen.md:304-357. Implementations live under internal/openapi to keep kin-openapi dependencies hidden from consumers.

Index

Constants

View Source
const (
	SourceKindFile = schema.SourceKindFile
	SourceKindFS   = schema.SourceKindFS
	SourceKindURL  = schema.SourceKindURL
)
View Source
const DefaultAdapterName = "openapi"

Variables

This section is empty.

Functions

func FormFromOperation added in v0.15.0

func FormFromOperation(op Operation) schema.Form

FormFromOperation converts an OpenAPI operation into a canonical form.

Types

type Adapter added in v0.15.0

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

Adapter wraps the OpenAPI loader/parser flow behind the schema adapter interface.

func NewAdapter added in v0.15.0

func NewAdapter(loader Loader, parser Parser) *Adapter

NewAdapter constructs an OpenAPI adapter with the supplied loader and parser.

func (*Adapter) Detect added in v0.15.0

func (a *Adapter) Detect(_ schema.Source, raw []byte) bool

Detect reports whether the raw payload appears to be OpenAPI.

func (*Adapter) Forms added in v0.15.0

func (a *Adapter) Forms(_ context.Context, ir schema.SchemaIR) ([]schema.FormRef, error)

Forms returns the list of operation-backed form references.

func (*Adapter) Load added in v0.15.0

func (a *Adapter) Load(ctx context.Context, src schema.Source) (schema.Document, error)

Load fetches the raw OpenAPI document.

func (*Adapter) Name added in v0.15.0

func (a *Adapter) Name() string

Name returns the adapter registry identifier.

func (*Adapter) Normalize added in v0.15.0

Normalize parses operations and converts them into the canonical schema IR.

type Document

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

Document wraps the raw OpenAPI payload and its origin. By exposing this type instead of kin-openapi structs we keep the public API decoupled, as committed in go-form-gen.md:25-77.

func MustNewDocument

func MustNewDocument(src Source, raw []byte) Document

MustNewDocument panics if the document cannot be created. Useful for tests.

func NewDocument

func NewDocument(src Source, raw []byte) (Document, error)

NewDocument constructs a Document wrapper while validating the inputs.

func (Document) Location

func (d Document) Location() string

Location returns the string identifier for the origin.

func (Document) Raw

func (d Document) Raw() []byte

Raw returns a defensive copy of the OpenAPI payload.

func (Document) Source

func (d Document) Source() Source

Source returns the origin metadata for the document.

type Loader

type Loader interface {
	Load(ctx context.Context, src Source) (Document, error)
}

Loader fetches OpenAPI documents from different sources (filesystem, fs.FS, HTTP). Implementations live under internal/openapi but satisfy this contract.

type LoaderOption

type LoaderOption func(*LoaderOptions)

LoaderOption mutates LoaderOptions prior to construction.

func WithDefaultSources

func WithDefaultSources() LoaderOption

WithDefaultSources enables the built-in HTTP loader using the default client when no explicit client is provided. This mirrors the Quick Start examples in the README.

func WithFileSystem

func WithFileSystem(files fs.FS) LoaderOption

WithFileSystem injects an fs.FS implementation for relative paths.

func WithHTTPClient

func WithHTTPClient(client *http.Client) LoaderOption

WithHTTPClient injects a custom HTTP client for remote OpenAPI documents.

func WithHTTPFallback

func WithHTTPFallback(timeout time.Duration) LoaderOption

WithHTTPFallback enables HTTP loading using http.DefaultClient and assigns an optional timeout.

type LoaderOptions

type LoaderOptions struct {
	// FileSystem enables loading from an abstract filesystem; defaults to the
	// operating system if nil.
	FileSystem fs.FS

	// HTTPClient allows callers to inject custom HTTP behaviour (timeouts,
	// proxies). Nil means HTTP sources are disabled unless AllowHTTPFallback is
	// true.
	HTTPClient *http.Client

	// AllowHTTPFallback toggles the default HTTP loader using http.DefaultClient
	// when no client is supplied. Keeping this explicit preserves offline-first
	// behaviour promised in the README.
	AllowHTTPFallback bool

	// RequestTimeout caps remote fetch durations when AllowHTTPFallback is true.
	RequestTimeout time.Duration
}

LoaderOptions configures how a Loader resolves sources. It collects the knobs referenced throughout go-form-gen.md:25-77 (offline-first with optional HTTP).

func NewLoaderOptions

func NewLoaderOptions(options ...LoaderOption) LoaderOptions

NewLoaderOptions applies a set of LoaderOption values and returns the resulting configuration. Implementations can embed this helper to stay consistent.

type Operation

type Operation struct {
	ID          string
	Method      string
	Path        string
	Summary     string
	Description string
	RequestBody Schema
	Responses   map[string]Schema
	Extensions  map[string]any `json:"Extensions,omitempty"`
}

Operation models the subset of OpenAPI operation metadata needed to build form models, aligning with go-form-gen.md:111-158.

func MustNewOperation

func MustNewOperation(id, method, path string, request Schema, responses map[string]Schema) Operation

MustNewOperation panics when construction fails, assisting fixtures/tests.

func NewOperation

func NewOperation(id, method, path string, request Schema, responses map[string]Schema) (Operation, error)

NewOperation validates core fields and initialises response maps.

func (Operation) HasResponse

func (op Operation) HasResponse(code string) bool

HasResponse reports whether a response code has a schema registered.

type Parser

type Parser interface {
	Operations(ctx context.Context, doc Document) (map[string]Operation, error)
}

Parser normalises OpenAPI documents into operation wrappers that downstream packages consume. See go-form-gen.md:82-159 for the unidirectional flow.

type ParserOption

type ParserOption func(*ParserOptions)

ParserOption mutates ParserOptions during construction.

func WithPartialDocuments

func WithPartialDocuments(enabled bool) ParserOption

WithPartialDocuments toggles support for component-only documents (planned for v2 per go-form-gen.md:403-414).

func WithReferenceResolution

func WithReferenceResolution(enabled bool) ParserOption

WithReferenceResolution toggles eager reference resolution.

type ParserOptions

type ParserOptions struct {
	// ResolveReferences controls whether the parser eagerly resolves $ref
	// pointers. Defaults to true for full documents.
	ResolveReferences bool

	// AllowPartialDocuments gates loading component-only inputs. Defaults to
	// false per the README commitment to focus on full documents in v1.
	AllowPartialDocuments bool
}

ParserOptions exposes toggles for future behaviour (e.g., dereferencing $refs). For v1 most fields remain unused but documenting them up front keeps progressive complexity low.

func NewParserOptions

func NewParserOptions(options ...ParserOption) ParserOptions

NewParserOptions applies ParserOption functions and returns the resulting configuration. Implementations under internal/openapi should call this helper to remain consistent.

type Schema

type Schema struct {
	Ref              string
	Type             string
	Format           string
	Required         []string
	Properties       map[string]Schema
	Items            *Schema
	Enum             []any
	Description      string
	Default          any
	Minimum          *float64
	Maximum          *float64
	ExclusiveMinimum bool
	ExclusiveMaximum bool
	MinLength        *int
	MaxLength        *int
	Pattern          string
	Extensions       map[string]any `json:"Extensions,omitempty"`
}

Schema represents request/response bodies and nested fields within an operation, linked to the README description in go-form-gen.md:111-158.

func (Schema) Clone

func (s Schema) Clone() Schema

Clone creates a deep copy of the schema tree to avoid accidental mutation.

func (Schema) DebugString

func (s Schema) DebugString() string

DebugString renders the schema for logging/debugging without exposing implementation details. This helps maintain observability without coupling to kin-openapi structures (see go-form-gen.md:82-159).

func (Schema) Validate

func (s Schema) Validate() error

Validate performs basic sanity checks useful for callers before building form models.

type Source

type Source = schema.Source

Source identifies where an OpenAPI document originated. This is an alias to the canonical schema source abstraction so adapters can share loader logic.

func SourceFromFS

func SourceFromFS(name string) Source

SourceFromFS returns a Source identifying a resource inside an fs.FS.

func SourceFromFile

func SourceFromFile(path string) Source

SourceFromFile returns a Source pointing to a file path.

func SourceFromURL

func SourceFromURL(raw string) Source

SourceFromURL parses the supplied URL string and returns a Source. It panics if the URL is invalid to surface configuration mistakes early.

type SourceKind

type SourceKind = schema.SourceKind

SourceKind enumerates the loader modalities.

Jump to

Keyboard shortcuts

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