Files
uberbringer/tools/genopenapi/main.go
T
57_WolveandClaude Opus 5.5 bdd337e2dc
Security / Vulnerability check (push) Successful in 38s
Test / Lint and test (push) Successful in 1m53s
Generate the OpenAPI document from a route table
The server registers its routes from one table in server/routes.go, and
tools/genopenapi (run by go generate) writes docs/swagger.json, an
OpenAPI 3.1 document, from the same table and the Go types it names.
Schemas are reflected with invopop/jsonschema: descriptions from the
first paragraph of the doc comments, enums and limits from jsonschema
tags. Request schemas are strict, as the decoder is; response schemas
allow fields added later. The daemon serves the embedded document at
GET /openapi.json without a token, internal/app passes it to the server
in its config, and release archives and the Docker build include it.

Tests keep it honest: a golden test and a CI step fail when the
document is stale, every registered route must be in the table, and the
tags are held to validation, to the Go constants and to the values the
handlers send. receipt.Element and printqueue.Status gain field doc
comments for the descriptions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 09:30:42 -06:00

73 lines
1.8 KiB
Go

// Command genopenapi writes docs/swagger.json, the OpenAPI 3.1 document of
// Überbringer's HTTP API, from the route table in
// internal/app/uberbringer/server and the Go types it names: paths from the
// routes, schemas by reflecting the request and response types (their json
// and jsonschema tags and their doc comments). Nothing in the document is
// written by hand, and a golden test fails when the committed file differs
// from what this produces.
//
// go run ./tools/genopenapi (or: go generate ./...)
package main
import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
)
// swaggerPath is the generated document, relative to the repository root.
const swaggerPath = "docs/swagger.json"
func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, "genopenapi:", err)
os.Exit(1)
}
}
func run() error {
cwd, err := os.Getwd()
if err != nil {
return err
}
root, err := findRepoRoot(cwd)
if err != nil {
return err
}
out, err := Generate(root)
if err != nil {
return err
}
path := filepath.Join(root, filepath.FromSlash(swaggerPath))
current, err := os.ReadFile(path)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
if bytes.Equal(current, out) {
fmt.Printf("genopenapi: %s is up to date\n", swaggerPath)
return nil
}
if err := os.WriteFile(path, out, 0o644); err != nil {
return err
}
fmt.Printf("genopenapi: wrote %s\n", swaggerPath)
return nil
}
// findRepoRoot walks up from start to the directory holding go.mod.
func findRepoRoot(start string) (string, error) {
dir := start
for {
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
return dir, nil
}
parent := filepath.Dir(dir)
if parent == dir {
return "", fmt.Errorf("go.mod not found above %s", start)
}
dir = parent
}
}