openapicontract

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2026 License: GPL-3.0 Imports: 11 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 openapicontract checks that three descriptions of this server's HTTP surface agree: the // @Router annotations on the handlers, the committed OpenAPI spec, and the routes actually registered with the router.

WHY NOT A BYTE COMPARISON OF THE SPEC

The obvious gate is "regenerate the spec and diff it". That was tried and does not hold: swag is not hermetic across environments. On a clean CI runner it emits `format: int64` for some map[string]int64 fields and not others, while the same swag version, the same Go toolchain (1.26.5) and a freshly downloaded module cache produce the format locally. The difference describes no disagreement about the API — both documents say the same thing about the same Go types — but a byte gate fails on it, cannot be satisfied by the developer who trips it, and would be weakened or deleted within a week.

So gate the property the work was actually about. Every cross-repo bug that motivated this was structural:

  • 30 documented paths had no handler and no route anywhere in the repo (/admin/platform-agents, /plans, /tenants/{id}/subscription, ...), which is how the UI came to call GET /api/v1/me/event-types against a server that never had it, and to render OAuth buttons that 404'd.
  • 40 real endpoints were undocumented, the whole /notifications API among them, so no generated client could see them.

Set comparison catches both exactly, and is stable across swag's formatting.

THE THREE CHECKS

A. annotations == spec
   Every // @Router in internal/infra/http/handler must appear as a
   path+method in the committed spec, and vice versa. This is what makes
   the spec "generated": you cannot hand-add a path, and you cannot change
   an annotation without running `make swagger`.

B. spec ⊆ routes
   Every documented path+method must be registered on the router. This is
   the phantom-endpoint check.

C. routes ⊆ spec ∪ baseline
   Every registered route must be documented, or listed in
   api/openapi/undocumented-routes.txt. 439 routes are undocumented today;
   annotating them is a large separate effort, so the baseline freezes that
   debt instead of ignoring it. A NEW route must be documented or must be
   added to the baseline deliberately, in the same commit, where a reviewer
   sees it.

Index

Constants

View Source
const BasePath = "/api/v1"

BasePath is the swagger `@BasePath`. Annotations and spec paths are relative to it; registered routes carry it literally.

Variables

This section is empty.

Functions

func Annotations

func Annotations(handlerDir string) (map[Op]string, error)

Annotations returns every // @Router operation declared under handlerDir.

func Baseline

func Baseline(path string) (map[Op]bool, error)

Baseline reads the frozen list of registered-but-undocumented routes. Blank lines and # comments are ignored.

func NormalizePath

func NormalizePath(p string) string

NormalizePath reduces path parameters to a positional {} so that a rename of the parameter is not reported as a contract change.

func RouteToSpec

func RouteToSpec(routePath string) (string, bool)

RouteToSpec maps a registered route path back onto its spec path, reporting false when the route lives outside BasePath and is not a known root-only one.

func Routes

func Routes(routesDir string) (map[Op]string, error)

Routes returns every route registered under routesDir, with its full path including the BasePath prefix the Group calls supply.

Registration nests: router.Group("/api/v1/x", func(r Router) { r.GET("/y", h) }) so the walk carries the accumulated prefix down into each Group's function literal. Parsing the AST rather than grepping matters here — a regex cannot tell which Group a method call belongs to.

func SpecOps

func SpecOps(specPath string) (map[Op]bool, error)

SpecOps returns every path+method declared in the committed OpenAPI document.

func SpecToRoute

func SpecToRoute(specPath string) string

SpecToRoute maps a spec/annotation path onto the route path it should serve.

Types

type Op

type Op struct {
	Method string
	Path   string
}

Op is one HTTP operation, normalised for comparison: method upper-cased and path parameters reduced to {} so that {id} and {groupId} compare equal.

func SortedOps

func SortedOps[V any](m map[Op]V) []Op

SortedOps returns ops in a stable order for reporting.

func (Op) String

func (o Op) String() string

Jump to

Keyboard shortcuts

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