apicontract

package
v0.22.1 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 8 Imported by: ? 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 apicontract describes a query operation once, for both the OpenAPI document a client or an assistant reads and the validation the handler applies: the required inputs, the supported filters, paging and the response schema. A request naming a filter the operation does not list is refused with a problem document before the handler runs, so the surface the model is told about is the surface it gets.

var api = apicontract.New("Inventory", "/api/v1/inventory",
    apicontract.Operation{Path: "/nodes/summary", ID: "summarizeNodes", Summary: "Count nodes per cluster",
        Parameters: append([]apicontract.Parameter{apicontract.Query("cluster_id", "One cluster", false)}, apicontract.Paging()...),
        ResponseSchema: apicontract.Schema[NodeSummary]()})
router.HandleFunc("/api/v1/inventory/openapi.json", auth.Bearer(api.ServeHTTP)).Methods("GET")
router.HandleFunc("/api/v1/inventory/nodes/summary", auth.Bearer(api.Wrap("GET", "/nodes/summary", summarizeNodes))).Methods("GET")

The document is also what instance.RouteTools overlays on the routes it walks (RouteToolsOptions.Contracts), so the generated tools carry the declared parameters instead of inferred ones.

Index

Constants

View Source
const (
	// DefaultPageLimit is the page size when a request names none.
	DefaultPageLimit = 100
	// MaxPageLimit is the largest page a request may ask for.
	MaxPageLimit = 200
)

Variables

This section is empty.

Functions

func Schema

func Schema[T any]() map[string]any

Schema describes a JSON response from its Go representation. Summary payloads use finite structs and maps, avoiding duplicate hand-maintained field lists.

Types

type Contract

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

Contract is a set of operations and their OpenAPI document.

func New

func New(title, base string, operations ...Operation) *Contract

New builds the contract and its document. base is the path every operation's Path is relative to. It panics on an operation without an ID or summary, as a missing contract would otherwise surface only to the model.

func (*Contract) Document

func (c *Contract) Document() []byte

Document is the OpenAPI 3 document.

func (*Contract) ServeHTTP

func (c *Contract) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP serves the document.

func (*Contract) Wrap

func (c *Contract) Wrap(method, path string, next http.HandlerFunc) http.HandlerFunc

Wrap validates a request against the operation registered for method and path (relative to base) and answers a problem document for unknown filters, missing required inputs and values outside a parameter's schema; valid requests reach next. It panics when the operation is not in the contract. The accountID and userID query parameters are always accepted, because the platform's ID middleware reads them.

type Operation

type Operation struct {
	Method         string         `json:"-"`
	Path           string         `json:"-"`
	ID             string         `json:"operationId"`
	Summary        string         `json:"summary"`
	Description    string         `json:"description"`
	Parameters     []Parameter    `json:"parameters"`
	Responses      map[string]any `json:"responses"`
	ResponseSchema map[string]any `json:"-"`
}

Operation is one documented operation. Method defaults to GET; ID and Summary are required. Responses is generated from ResponseSchema when empty.

type Page

type Page struct {
	Total      int  `json:"total"`
	Limit      int  `json:"limit"`
	Offset     int  `json:"offset"`
	HasMore    bool `json:"has_more"`
	NextOffset *int `json:"next_offset,omitempty"`
}

Page describes one page of a result: totals precede paging, so a reader knows the size of the whole filtered result before walking it.

func Pagination

func Pagination(r *http.Request, total int) Page

Pagination reads limit and offset from the request (see Paging) and places them against total.

func (Page) Bounds

func (p Page) Bounds() (int, int)

Bounds returns the half-open slice bounds of the page within total.

type Parameter

type Parameter struct {
	Name        string         `json:"name"`
	In          string         `json:"in"`
	Description string         `json:"description"`
	Required    bool           `json:"required,omitempty"`
	Schema      map[string]any `json:"schema"`
}

Parameter is one query parameter of an operation.

func Boolean

func Boolean(name, description string) Parameter

Boolean is an optional true/false parameter.

func Choice

func Choice(name, description string, values ...string) Parameter

Choice is an optional parameter restricted to the given values.

func Paging

func Paging() []Parameter

Paging returns the limit and offset parameters Pagination reads.

func Query

func Query(name, description string, required bool) Parameter

Query is a required or optional string parameter.

Jump to

Keyboard shortcuts

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