Generate the OpenAPI document from a route table
Security / Vulnerability check (push) Successful in 38s
Test / Lint and test (push) Successful in 1m53s

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:
57_WolveandClaude Opus 5.5 committed 2026-11-30 09:30:42 -06:00
1 parent 9f7c5db923
commit bdd337e2dc
31 files changed
+3153 -128

No files matched your search

+120
View File
@@ -0,0 +1,120 @@
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"reflect"
"strings"
)
// modulePath is the module's import path, which every package path starts
// with.
const modulePath = "uberbringer"
// comments maps "pkgpath.Type" and "pkgpath.Type.Field" to the first
// paragraph of their doc comments, for the packages whose types the document
// describes. Later paragraphs are for Go readers only.
// jsonschema's own extractor builds its keys from file paths, which come out
// with backslashes on Windows, so the generator reads the comments itself to
// give the same document on every platform.
type comments map[string]string
// extractComments reads the doc comments of every type in the packages of
// the given values.
func extractComments(root string, of ...any) (comments, error) {
out := make(comments)
for _, v := range of {
pkg := reflect.TypeOf(v).PkgPath()
rel, ok := strings.CutPrefix(pkg, modulePath+"/")
if !ok {
return nil, fmt.Errorf("package %s is outside the module", pkg)
}
dir := filepath.Join(root, filepath.FromSlash(rel))
entries, err := os.ReadDir(dir)
if err != nil {
return nil, fmt.Errorf("reading %s: %w", rel, err)
}
fset := token.NewFileSet()
for _, e := range entries {
name := e.Name()
if e.IsDir() || !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
continue
}
f, err := parser.ParseFile(fset, filepath.Join(dir, name), nil, parser.ParseComments)
if err != nil {
return nil, err
}
out.collect(pkg, f)
}
}
return out, nil
}
func (c comments) collect(pkg string, f *ast.File) {
for _, decl := range f.Decls {
gen, ok := decl.(*ast.GenDecl)
if !ok || gen.Tok != token.TYPE {
continue
}
for _, spec := range gen.Specs {
ts := spec.(*ast.TypeSpec)
doc := ts.Doc
if doc == nil && len(gen.Specs) == 1 {
doc = gen.Doc
}
key := pkg + "." + ts.Name.Name
if text := firstParagraph(doc.Text()); text != "" {
c[key] = text
}
st, ok := ts.Type.(*ast.StructType)
if !ok {
continue
}
for _, field := range st.Fields.List {
text := field.Doc.Text()
if text == "" {
text = field.Comment.Text()
}
for _, n := range field.Names {
if text != "" {
c[key+"."+n.Name] = firstParagraph(text)
}
}
}
}
}
}
// lookup is the jsonschema LookupComment hook: field is the Go field name,
// or empty for the type itself. A comment starts, as Go doc comments do,
// with the Go name; the document shows the schema's or the property's name
// there instead.
func (c comments) lookup(t reflect.Type, field string) string {
key := t.PkgPath() + "." + t.Name()
if field == "" {
text := c[key]
if rest, ok := strings.CutPrefix(text, t.Name()+" "); ok {
text = schemaName(t) + " " + rest
}
return text
}
text := c[key+"."+field]
if f, ok := t.FieldByName(field); ok {
name, _, _ := strings.Cut(f.Tag.Get("json"), ",")
if rest, ok := strings.CutPrefix(text, field+" "); ok && name != "" {
text = name + " " + rest
}
}
return text
}
// firstParagraph returns a doc comment's text up to its first blank line, on
// one line.
func firstParagraph(text string) string {
para, _, _ := strings.Cut(strings.TrimSpace(text), "\n\n")
return strings.Join(strings.Fields(para), " ")
}