Skip to content

Commit b59631d

Browse files
committed
Add OpenAPI spec validation with vacuum
1 parent 1f9ea94 commit b59631d

File tree

6 files changed

+34
-4
lines changed

6 files changed

+34
-4
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ jobs:
1717
run: just build-templates
1818
- name: Run Linter
1919
run: just lint
20+
- name: Validate OpenAPI Spec
21+
run: just check-spec
2022

2123
test:
2224
runs-on: ubuntu-latest

internal/apidoc/handler.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package apidoc
22

33
import (
44
"encoding/json"
5+
"io"
56
"net/http"
67
)
78

@@ -26,3 +27,8 @@ func (h *Handler) HandleDocJSON(w http.ResponseWriter, _ *http.Request) {
2627
func (h *Handler) RegisterRoutes(mux *http.ServeMux) {
2728
mux.HandleFunc("GET /api/doc.json", h.HandleDocJSON)
2829
}
30+
31+
func PrintSpec(w io.Writer, includeInternal bool) {
32+
data, _ := json.MarshalIndent(BuildSpec(includeInternal), "", " ")
33+
_, _ = w.Write(data)
34+
}

internal/apidoc/spec.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ type OpenAPISpec struct {
1414
}
1515

1616
type SpecInfo struct {
17-
Title string `json:"title"`
18-
Version string `json:"version"`
17+
Title string `json:"title"`
18+
Version string `json:"version"`
19+
Description string `json:"description,omitempty"`
1920
}
2021

2122
type SpecTag struct {
@@ -233,8 +234,9 @@ func BuildSpec(includeInternal bool) *OpenAPISpec {
233234
spec := &OpenAPISpec{
234235
OpenAPI: "3.0.0",
235236
Info: SpecInfo{
236-
Title: "pkgstats API documentation",
237-
Version: "3.0.0",
237+
Title: "pkgstats API documentation",
238+
Version: "3.0.0",
239+
Description: "Read-only API for Arch Linux package popularity statistics.",
238240
},
239241
Paths: make(map[string]PathItem),
240242
Components: SpecComponents{Schemas: make(map[string]*Schema)},

justfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ lint:
6161
golangci-lint run
6262
just --fmt --unstable --check
6363

64+
# validate the OpenAPI spec
65+
check-spec:
66+
#!/usr/bin/env bash
67+
set -euo pipefail
68+
spec=$(mktemp --suffix=.json)
69+
trap 'rm -f "$spec"' EXIT
70+
go run -tags development . spec > "$spec"
71+
go run github.com/daveshanley/vacuum@latest lint -bx -r vacuum.conf.yaml "$spec"
72+
6473
# auto-format all code
6574
fmt:
6675
pnpm run format

main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ import (
2828
const defaultCacheMaxAge = 5 * time.Minute
2929

3030
func main() {
31+
if isDevelopment && len(os.Args) > 1 && os.Args[1] == "spec" {
32+
apidoc.PrintSpec(os.Stdout, true)
33+
return
34+
}
35+
3136
cfg, err := config.Load()
3237
if err != nil {
3338
slog.Error("failed to load config", "error", err)

vacuum.conf.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extends: [[vacuum:oas, recommended]]
2+
rules:
3+
description-duplication: false
4+
oas3-missing-example: false
5+
oas3-api-servers: false
6+
component-description: false

0 commit comments

Comments
 (0)