Documentation
¶
Overview ¶
Package openapi loads OpenAPI documents that are compiled into the binary with go:embed.
API packages embed their openapi.yaml verbatim and wrap it in a Document. Embedding the YAML rather than a generated, base64-encoded blob keeps the specification readable and diffable in source control, at the cost of parsing YAML at runtime instead of JSON. Document pays that cost at most once per process.
That trade is deliberate. Parsing YAML is slower than decoding the blob was (roughly 125ms versus 75ms for core v1beta1), and specifications that reuse another version's schemas pay more still, because the blob used to arrive with its external references already inlined whereas we now resolve them by parsing the referenced document too. Every call after the first is a cached pointer read of a few nanoseconds, so the whole cost lands once during process start-up and never on a request path.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
Document is an OpenAPI document embedded in a Go package, parsed lazily.
The first call to Spec parses the document; every later call returns the cached result, including a cached failure. A Document is safe for concurrent use.
func NewDocument ¶
NewDocument wraps a raw OpenAPI document, normally one populated by an embed directive.
externalRefs resolves the document's external "$ref" values. Each key is a reference path exactly as it appears in the document (for example "../../core/v1beta1/openapi.yaml") and each value is the raw document it names, typically obtained from the Go package that embeds it. A document with no external references passes nil. References are resolved from these bytes alone: the filesystem is never consulted, so a reference that is missing from the map is an error rather than a disk read, and URL-based references are not supported.
Only directly reachable references need an entry. Resolving a reference whose target itself has external references requires those nested paths to be present in the map as well, keyed as the nested document spells them.
NewDocument does not parse or copy doc. Neither doc nor the values in externalRefs may be modified afterwards.
func (*Document) Raw ¶
Raw returns the embedded document as it appears in source control.
It is exported so a package whose specification refers to this one can pass the bytes to NewDocument as an external reference. Callers must not modify the returned slice.