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
- func FormFromOperation(op Operation) schema.Form
- type Adapter
- func (a *Adapter) Detect(_ schema.Source, raw []byte) bool
- func (a *Adapter) Forms(_ context.Context, ir schema.SchemaIR) ([]schema.FormRef, error)
- func (a *Adapter) Load(ctx context.Context, src schema.Source) (schema.Document, error)
- func (a *Adapter) Name() string
- func (a *Adapter) Normalize(ctx context.Context, doc schema.Document, _ schema.NormalizeOptions) (schema.SchemaIR, error)
- type Document
- type Loader
- type LoaderOption
- type LoaderOptions
- type Operation
- type Parser
- type ParserOption
- type ParserOptions
- type Schema
- type Source
- type SourceKind
Constants ¶
const ( SourceKindFile = schema.SourceKindFile SourceKindFS = schema.SourceKindFS SourceKindURL = schema.SourceKindURL )
const DefaultAdapterName = "openapi"
Variables ¶
This section is empty.
Functions ¶
func FormFromOperation ¶ added in v0.15.0
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
NewAdapter constructs an OpenAPI adapter with the supplied loader and parser.
func (*Adapter) Detect ¶ added in v0.15.0
Detect reports whether the raw payload appears to be OpenAPI.
func (*Adapter) Forms ¶ added in v0.15.0
Forms returns the list of operation-backed form references.
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 ¶
MustNewDocument panics if the document cannot be created. Useful for tests.
func NewDocument ¶
NewDocument constructs a Document wrapper while validating the inputs.
type Loader ¶
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 ¶
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) DebugString ¶
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).
type 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 ¶
SourceFromFS returns a Source identifying a resource inside an fs.FS.
func SourceFromFile ¶
SourceFromFile returns a Source pointing to a file path.
func SourceFromURL ¶
SourceFromURL parses the supplied URL string and returns a Source. It panics if the URL is invalid to surface configuration mistakes early.