oas

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: Apache-2.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package oas holds the small, dependency-light helpers that the rules use to read an OpenAPI document as raw YAML nodes: safe accessors, JSONPath building, local $ref resolution and iterators over operations, parameters and schemas.

Nothing in this package reports violations; it only navigates documents. All accessors accept nil nodes and return zero values for them, so a check can chain lookups without testing every step.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dig

func Dig(n *yaml.Node, keys ...string) *yaml.Node

Dig follows keys through nested mappings and returns the node found, or nil.

func Empty

func Empty(n *yaml.Node) bool

Empty reports whether n is nil, an empty collection or an empty scalar.

func Entry

func Entry(n *yaml.Node, key string) (*yaml.Node, *yaml.Node)

Entry returns the key node and the value node of key in the mapping n.

func Get

func Get(n *yaml.Node, key string) *yaml.Node

Get returns the value of key in the mapping n, or nil when it is absent.

func Has

func Has(n *yaml.Node, key string) bool

Has reports whether the mapping n defines key.

func HasType

func HasType(schema *yaml.Node, want string) bool

HasType reports whether the schema declares the given type.

func Index

func Index(base string, i int) string

Index appends an array index to the JSONPath base.

func Int

func Int(n *yaml.Node) (int, bool)

Int returns the integer value of the scalar n and whether it parsed.

func IsClientOrServerError

func IsClientOrServerError(code string) bool

IsClientOrServerError reports whether the status code is 4xx or 5xx.

func IsMap

func IsMap(n *yaml.Node) bool

IsMap reports whether n is a mapping.

func IsMethod

func IsMethod(key string) bool

IsMethod reports whether key is an HTTP method keyword of a path item.

func IsNull

func IsNull(n *yaml.Node) bool

IsNull reports whether n is a YAML null scalar.

func IsOAS3

func IsOAS3(root *yaml.Node) bool

IsOAS3 reports whether root is any OpenAPI 3.x document.

func IsOAS31

func IsOAS31(root *yaml.Node) bool

IsOAS31 reports whether root is an OpenAPI 3.1 (or later 3.x) document.

func IsScalar

func IsScalar(n *yaml.Node) bool

IsScalar reports whether n is a scalar.

func IsSeq

func IsSeq(n *yaml.Node) bool

IsSeq reports whether n is a sequence.

func IsSuccess

func IsSuccess(code string) bool

IsSuccess reports whether the status code is a 2xx or 2XX code.

func IsTrue

func IsTrue(n *yaml.Node) bool

IsTrue reports whether n is the boolean scalar true.

func Items

func Items(n *yaml.Node) iter.Seq2[int, *yaml.Node]

Items iterates over the elements of the sequence n.

func Join

func Join(base string, key string) string

Join appends key to the JSONPath base, using dot notation for plain identifiers and bracket notation for everything else.

func JoinAll

func JoinAll(base string, keys ...string) string

JoinAll appends every key to the JSONPath base in order.

func Keys

func Keys(n *yaml.Node) []string

Keys returns the keys of the mapping n in document order.

func Methods

func Methods() []string

Methods lists the HTTP methods that can appear in a path item, in the order the OpenAPI specification declares them.

func NodeAt added in v1.1.0

func NodeAt(root *yaml.Node, path string) *yaml.Node

NodeAt returns the node that marks the location of a JSONPath produced by Join and Index: the key node for a mapping entry and the item itself for a sequence index. It returns nil when the path does not lead anywhere in root.

func Number

func Number(n *yaml.Node) (float64, bool)

Number returns the numeric value of the scalar n and whether it parsed.

func Operations

func Operations(root *yaml.Node) iter.Seq[Operation]

Operations iterates over every operation declared under paths in root.

func Pairs

func Pairs(n *yaml.Node) iter.Seq2[*yaml.Node, *yaml.Node]

Pairs iterates over the key and value nodes of the mapping n.

func PathItems

func PathItems(root *yaml.Node) iter.Seq[PathItem]

PathItems iterates over the entries of the paths object of root.

func Pointer

func Pointer(root *yaml.Node, ref string) *yaml.Node

Pointer resolves a local JSON pointer such as "#/components/schemas/Pet" against root. It returns nil when the pointer is not local or does not exist.

func Ref

func Ref(n *yaml.Node) string

Ref returns the value of the $ref keyword of n, or the empty string.

func RefName

func RefName(ref string) string

RefName returns the last segment of a $ref, which is the component name for references into #/components.

func Resolve

func Resolve(root, n *yaml.Node) *yaml.Node

Resolve follows local $ref chains starting at n and returns the final target. A reference that cannot be resolved, or one that is not local, returns n.

func Schemas

func Schemas(root *yaml.Node) iter.Seq[Schema]

Schemas iterates over every schema in root: component schemas, parameter and header schemas, request and response media type schemas, and all nested schemas below them. Each schema node is reported once.

The walk records only a cheap trail for every schema; the JSONPath strings are rendered afterwards. A node that was already reported is not entered again, which also terminates cyclic aliases.

func StringAt

func StringAt(n *yaml.Node, key string) string

StringAt returns the scalar value stored under key in the mapping n.

func Strings

func Strings(n *yaml.Node) []string

Strings returns the scalar values of the sequence n.

func Text

func Text(n *yaml.Node) string

Text returns the value of the scalar n, or the empty string for any other node.

func Types

func Types(schema *yaml.Node) []string

Types returns the declared types of a schema. In OpenAPI 3.1 the type keyword may be a list; in 3.0 it is a single string.

func UnderNegation added in v1.1.0

func UnderNegation(path string) bool

UnderNegation reports whether the JSONPath of a schema is below a not, if, then or else keyword. Such schemas are constraint fragments (for example "not: {anyOf: [{type: 'null'}]}"), not descriptions of the data.

func Unwrap

func Unwrap(n *yaml.Node) *yaml.Node

Unwrap removes the document wrapper from n and returns the content node. It returns nil for a nil node.

func Version

func Version(root *yaml.Node) string

Version returns the value of the top-level openapi (or swagger) keyword.

func Walk

func Walk(n *yaml.Node, path string) iter.Seq[Node]

Walk iterates over every mapping and sequence value below n, depth first, reporting each node with its JSONPath. The start node itself is not reported. $ref targets are not followed.

Types

type MediaType

type MediaType struct {
	// Name is the media type key, such as application/json.
	Name string
	// Node is the media type object.
	Node *yaml.Node
	// Path is the JSONPath of the media type object.
	Path string
}

MediaType is one entry of a content map.

func MediaTypes

func MediaTypes(content *yaml.Node, path string) []MediaType

MediaTypes returns the entries of a content map.

type Node

type Node struct {
	// Key is the mapping key (or "[i]" index) the node was found under.
	Key string
	// Node is the YAML node.
	Node *yaml.Node
	// Path is the JSONPath of the node.
	Path string
}

Node is a node of the document with the JSONPath it was found at.

type Operation

type Operation struct {
	// Route is the path template, such as /pets/{petId}.
	Route string
	// Method is the lower-case HTTP method.
	Method string
	// PathItem is the path item that holds the operation, with $ref resolved.
	PathItem *yaml.Node
	// Node is the operation object.
	Node *yaml.Node
	// Path is the JSONPath of the operation.
	Path string
}

Operation is one operation of the document together with its location.

type Parameter

type Parameter struct {
	// Node is the parameter object with $ref resolved.
	Node *yaml.Node
	// Path is the JSONPath of the parameter entry.
	Path string
	// Entry is the item as written in the parameters list, which is the $ref
	// object when the parameter is a reference. Report findings on it, because
	// Node is shared by every operation that references the same component.
	Entry *yaml.Node
	// Inherited is true when the parameter comes from the path item.
	Inherited bool
}

Parameter is a resolved parameter with its location.

func OwnParameters

func OwnParameters(root *yaml.Node, op Operation) []Parameter

OwnParameters returns only the parameters declared on the operation itself.

func Parameters

func Parameters(root *yaml.Node, op Operation) []Parameter

Parameters returns the parameters of an operation. Parameters declared at the path item are included unless the operation overrides them by name and location, as the specification requires.

func (Parameter) In

func (p Parameter) In() string

In returns the parameter location: query, header, path or cookie.

func (Parameter) Name

func (p Parameter) Name() string

Name returns the parameter name.

func (Parameter) Required

func (p Parameter) Required() bool

Required reports whether the parameter is marked required.

type PathItem

type PathItem struct {
	// Route is the path template.
	Route string
	// KeyNode is the key node of the route.
	KeyNode *yaml.Node
	// Node is the path item with $ref resolved.
	Node *yaml.Node
	// Path is the JSONPath of the path item.
	Path string
}

PathItem is one entry of the paths object.

type Response

type Response struct {
	// Code is the status code key, such as 200, 4XX or default.
	Code string
	// Node is the response object with $ref resolved.
	Node *yaml.Node
	// Path is the JSONPath of the response entry.
	Path string
}

Response is one response of an operation.

func Responses

func Responses(root *yaml.Node, op Operation) []Response

Responses iterates over the responses of an operation.

type Schema

type Schema struct {
	// Node is the schema object. It is not $ref-resolved: a reference is
	// reported as the node holding the $ref keyword.
	Node *yaml.Node
	// Path is the JSONPath of the schema.
	Path string
	// Component is the components.schemas key when the schema is a top-level
	// component, and empty otherwise.
	Component string
}

Schema is one schema object found while walking the document.

Jump to

Keyboard shortcuts

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