oas

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package oas provides OpenAPI-schema utilities that operate directly on a parsed kin-openapi document: extracting an operation's response schemas, validating a response body against a schema, and synthesizing an example value from a schema.

It is deliberately provider-free (it imports only kin-openapi and the standard library) so that both the openapi compiler and the rest provider can depend on it without an import cycle, mirroring the provider-free oauth package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Synthesize

func Synthesize(schema *openapi3.SchemaRef) any

Synthesize builds an example value for a schema, suitable for a mock response body. It prefers an explicit example, default, or first enum value; otherwise it constructs a value from the schema's type, recursing into object properties and array items. It never panics on an absent or malformed schema, returning nil instead.

func ValidateBody

func ValidateBody(schema *openapi3.SchemaRef, body []byte) []string

ValidateBody checks a response body against a schema, returning a list of human-readable violation messages. It returns nil when the body conforms (or when there is no schema to validate against). A body that is not valid JSON is itself reported as a single violation.

Validation runs in response mode with format validation enabled and collects every violation rather than stopping at the first.

Types

type MediaSchema

type MediaSchema struct {
	Schema  *openapi3.SchemaRef
	Example any
}

MediaSchema is the application/json schema (and any example) declared for a single response status.

func PickResponse

func PickResponse(rs ResponseSchemas, prefer int) (status string, ms MediaSchema, ok bool)

PickResponse selects the response schema to use for a given status. It prefers an exact match, then the lowest 2xx response, then the "default" response. The returned status is the matched key.

type ResponseSchemas

type ResponseSchemas map[string]MediaSchema

ResponseSchemas maps an operation's response status keys ("200", "404", "default") to their application/json schema. It is empty for operations that declare no JSON responses.

func Extract

func Extract(op *openapi3.Operation) ResponseSchemas

Extract pulls the application/json response schemas (and examples) from an operation, keyed by status code ("200", "default", …). It returns nil when the operation declares no JSON responses.

Jump to

Keyboard shortcuts

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