From 3206e15e831672f0ae0d837d822150ca19415594 Mon Sep 17 00:00:00 2001 From: VictorMaznyuk Date: Wed, 7 Oct 2026 19:26:08 +0200 Subject: [PATCH] fix(docs): make `make docs` reproduce the committed Swagger again (#3751) Running `make docs` on main no longer reproduces the committed docs/*: every definition loses its package-qualified name (types.* instead of github_com_Tencent_WeKnora_internal_types.*), enums disappear, ErrorCode becomes a plain integer, and docs/swagger_contract_test.go fails (TestModelDeleteUsageContract). Cause: internal/localsandbox is not a dependency of cmd/server (its importers are behind the `desktop` build tag), but swag's directory walk reaches it. Its `const X = core.X` aliases make swag load internal/localsandbox/core again through go/loader, and that second load re-registers every already-deduplicated type under its short name without enum values. Excluding the directory removes the second load. That load was also hiding a second problem: skill_handler.go references errors.AppError in @Failure without importing internal/errors, so with the exclude swag stops with "cannot find type definition: errors.AppError". A blank import lets swag resolve it. Co-authored-by: Victor Maznyuk Co-authored-by: Claude Opus 5.5 (1M context) --- Makefile | 11 ++++++++++- internal/handler/skill_handler.go | 3 +++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 2f454068..616c5962 100644 --- a/Makefile +++ b/Makefile @@ -246,9 +246,18 @@ migrate-goto: ./scripts/migrate.sh goto $(version) # Generate API documentation (Swagger) +# +# internal/localsandbox is excluded because cmd/server does not depend on it +# (its importers are behind the `desktop` build tag). Without the exclude, +# swag walks into the directory, follows its `const X = core.X` aliases and +# loads internal/localsandbox/core a second time through go/loader; that +# second load re-registers every model under its short name without enum +# values, so definitions lose their package-qualified names and +# docs/swagger_contract_test.go fails. docs: @echo "生成 Swagger API 文档..." - swag init -g $(MAIN_PATH)/main.go -o ./docs --parseDependency --parseInternal + swag init -g $(MAIN_PATH)/main.go -o ./docs --parseDependency --parseInternal \ + --exclude ./internal/localsandbox @echo "文档已生成到 ./docs 目录" @echo "启动服务后访问 http://localhost:8080/swagger/index.html 查看文档" diff --git a/internal/handler/skill_handler.go b/internal/handler/skill_handler.go index f4f8d04d..381fb8f9 100644 --- a/internal/handler/skill_handler.go +++ b/internal/handler/skill_handler.go @@ -6,6 +6,9 @@ import ( "github.com/Tencent/WeKnora/internal/application/access" "github.com/Tencent/WeKnora/internal/application/service" + // Referenced only by the @Failure annotations (errors.AppError); swag + // resolves annotation types through the file's imports. + _ "github.com/Tencent/WeKnora/internal/errors" "github.com/Tencent/WeKnora/internal/types" "github.com/gin-gonic/gin" )