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
- func Annotations(handlerDir string) (map[Op]string, error)
- func Baseline(path string) (map[Op]bool, error)
- func NormalizePath(p string) string
- func RouteToSpec(routePath string) (string, bool)
- func Routes(routesDir string) (map[Op]string, error)
- func SpecOps(specPath string) (map[Op]bool, error)
- func SpecToRoute(specPath string) string
- type Op
Constants ¶
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 ¶
Annotations returns every // @Router operation declared under handlerDir.
func Baseline ¶
Baseline reads the frozen list of registered-but-undocumented routes. Blank lines and # comments are ignored.
func NormalizePath ¶
NormalizePath reduces path parameters to a positional {} so that a rename of the parameter is not reported as a contract change.
func RouteToSpec ¶
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 ¶
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 SpecToRoute ¶
SpecToRoute maps a spec/annotation path onto the route path it should serve.