mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: enable strict mode for swagger generation & upgrade swag (#21975)
Adds a Go wrapper (`scripts/apidocgen/swaginit/main.go`) that calls swag's Go API with `Strict: true`. The `--strict` flag isn't available in swag's CLI in any version, so the wrapper is the only way to enable it. Also upgrades swag from v1.16.2 to v1.16.6 (better generics support, precise numeric formats, `x-enum-descriptions`, CVE-2024-45338 fix).
This commit is contained in:
@@ -18,12 +18,10 @@ trap cleanup EXIT
|
||||
log "Use temporary file: ${API_MD_TMP_FILE}"
|
||||
|
||||
pushd "${PROJECT_ROOT}"
|
||||
go tool github.com/swaggo/swag/cmd/swag init \
|
||||
--generalInfo="coderd.go" \
|
||||
--dir="./coderd,./codersdk,./enterprise/coderd,./enterprise/wsproxy/wsproxysdk" \
|
||||
--output="./coderd/apidoc" \
|
||||
--outputTypes="go,json" \
|
||||
--parseDependency=true
|
||||
# Use our custom wrapper instead of "go tool swag init" to enable
|
||||
# Strict mode, which turns duplicate-route warnings into hard errors.
|
||||
# The upstream swag CLI does not expose a --strict flag.
|
||||
go run "${APIDOCGEN_DIR}/swaginit/main.go"
|
||||
popd
|
||||
|
||||
pushd "${APIDOCGEN_DIR}"
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
// Package main wraps swag init with Strict mode enabled.
|
||||
//
|
||||
// The upstream swag CLI (v1.16.2) does not expose a --strict
|
||||
// flag, so warnings about duplicate routes are silently
|
||||
// ignored. This wrapper calls the Go API directly with
|
||||
// Strict: true, turning those warnings into hard errors.
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/swaggo/swag/gen"
|
||||
)
|
||||
|
||||
func main() {
|
||||
logger := log.New(os.Stdout, "", log.LstdFlags)
|
||||
|
||||
err := gen.New().Build(&gen.Config{
|
||||
SearchDir: "./coderd,./codersdk,./enterprise/coderd,./enterprise/wsproxy/wsproxysdk",
|
||||
MainAPIFile: "coderd.go",
|
||||
OutputDir: "./coderd/apidoc",
|
||||
OutputTypes: []string{"go", "json"},
|
||||
ParseDependency: 1,
|
||||
Strict: true,
|
||||
OverridesFile: gen.DefaultOverridesFile,
|
||||
ParseGoList: true,
|
||||
ParseDepth: 100,
|
||||
CollectionFormat: "csv",
|
||||
Debugger: logger,
|
||||
LeftTemplateDelim: "{{",
|
||||
RightTemplateDelim: "}}",
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("swag init failed: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user