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>
This commit is contained in:
1 parent
9f7c5db923
commit
bdd337e2dc
31 files changed
+3153
-128
No files matched your search
@@ -0,0 +1,72 @@
|
||||
// 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
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user