template-monorepo/generate/api
王性驊 4590f1c951 docs(api): group OpenAPI by tags + add Chinese field descriptions and enums
Make the generated docs/openapi/gateway.yaml usable by adding three things
go-doc parses out of the .api source:

- @server tags + summary on every block → Swagger UI groups endpoints
  (Auth / Member / Permission / Normal) instead of dumping everything
  under "default".
- backtick end-of-line // 中文 on every Request field → property
  descriptions in the schema. go-doc only reads the trailing comment,
  not the line above, so all comments are placed on the same line as
  the tag.
- options=A|B|C in json/form tags wherever validate:"oneof=..." exists
  → enum dropdowns. The validate tag is kept for runtime validation;
  go-zero also enforces options= at bind time.

Codify the rules in generate/api/README.md (tags / 行末註解 / options=)
and add AGENTS.md at repo root so any AI agent (Claude / Cursor / Codex)
picks them up automatically when working on the project.

types.go regenerated via make gen-api to keep json tags in sync.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-21 17:15:25 +08:00
..
README.md docs(api): group OpenAPI by tags + add Chinese field descriptions and enums 2026-05-21 17:15:25 +08:00
auth.api docs(api): group OpenAPI by tags + add Chinese field descriptions and enums 2026-05-21 17:15:25 +08:00
common.api feat(auth): add unified registration/login module with Zitadel + lint cleanup 2026-05-21 14:45:35 +08:00
gateway.api feat(permission): add RBAC module with Casbin enforcement and policy reload 2026-05-21 16:47:35 +08:00
member.api docs(api): group OpenAPI by tags + add Chinese field descriptions and enums 2026-05-21 17:15:25 +08:00
normal.api docs(api): group OpenAPI by tags + add Chinese field descriptions and enums 2026-05-21 17:15:25 +08:00
permission.api docs(api): group OpenAPI by tags + add Chinese field descriptions and enums 2026-05-21 17:15:25 +08:00

README.md

API 定義goctl + go-doc 共用)

檔案

檔案 用途
gateway.api 入口:info() + import
common.api 共用文件型別(APIErrorStatusErrorDetail
auth.api Auth 路由scope 28
member.api Member 路由scope 29
permission.api Permission / RBAC 路由scope 31
normal.api 路由與業務 data 型別

指令

make gen-api   # 生成 handler / logic / types
make gen-doc   # 生成 docs/openapi/gateway.yamlOpenAPI 3.0

註解約定

  • Logic returns:只寫業務 dataPingData
  • 文件 @respdoc:寫實際 HTTP JSONPingOKStatusAPIErrorStatus
  • @doc:單一 API 的 summary / description
  • 多狀態碼用 /* @respdoc-200 ... */ 區塊,放在 @handler
  • Request 驗證:欄位可加 validate:"required,email" 等 tagmake gen-api 後 handler 會自動 ValidateAll(見 generate/goctl/api/handler.tpl

文件分組與欄位說明go-doc 規則)

OpenAPI 由 generate/doc-generatego-doc.api 直接讀取,下列三種寫法必須遵守AI agent 在新增 / 修改 API 時請一併照做:

1. Swagger UI 分組tags

每個 .api 檔的 @server (...) 區塊必須tags:summary:,否則 endpoint 會散落在 "default" 群組。

@server (
    group:   permission        // goctl 用:決定 handler 子目錄
    prefix:  /api/v1/permissions
    tags:    "Permission - 權限"      // ← go-doc 用Swagger UI 分組標題
    summary: "Catalog / 角色 / 使用者角色 / 外部映射 / Policy"
)

一個 .api 內可以開多個 @server 區塊(相同 group / prefix 但不同 tags:),把同一模組再拆成子組。

2. 欄位中文 description

go-doc 把欄位的 description 只認 backtick 結尾的同一行 // 註解,寫在前一行不會被解析。

// ❌ 不會被當 description
RegisterReq {
    // 租戶 slug
    TenantSlug string `json:"tenant_slug" validate:"required"`
}

// ✅ 正確寫法backtick 行末 //
RegisterReq {
    TenantSlug string `json:"tenant_slug" validate:"required"` // 租戶 slug小寫英數
    Email      string `json:"email" validate:"required,email"` // 電子郵件
    Password   string `json:"password" validate:"required,min=8,max=128"` // 密碼8-128 字元)
}

所有 Request 型別(命名 *Req / *Query / *Path 的 struct每個欄位都要加中文 description。Response / Data 型別可選。

3. Enum 列舉值Swagger UI 下拉選單)

validate:"oneof=A B C" 是 runtime 驗證用的,go-doc 不會解析。要讓 Swagger UI 顯示下拉選單,必須在 tag 內加 options=A|B|C(管道分隔):

// ❌ 只有 validate 不會出 enum
Provider string `json:"provider" validate:"required,oneof=google"`

// ✅ 同時保留兩者options= 給 go-docvalidate= 給 runtime
Provider string `json:"provider,options=google" validate:"required,oneof=google"`
Source   string `json:"source,optional,options=manual|zitadel|ldap|scim" validate:"omitempty,oneof=manual zitadel ldap scim"`
Status   string `form:"status,optional,options=open|close" validate:"omitempty,oneof=open close"`

規則:只要有 oneof=...,就一定要在同欄位 tag 加對應 options=...,並把可選值也寫進行末註解。

4. 修改流程

每次改 .api必跑

make gen-api   # 同步 internal/types/types.gojson tag 帶 options= 給 go-zero runtime
make gen-doc   # 同步 docs/openapi/gateway.yamlgitignore本地驗證用
go build ./... # 確保編譯通過

驗證:把 docs/openapi/gateway.yaml 丟進 https://editor.swagger.io 或 Swagger UI確認分組、description、enum 都正確顯示。

與 runtime 對齊

Handler 使用 response.Write 輸出:

{ "code": 102000, "message": "SUCCESS", "data": { ... } }

失敗時含 error.biz_code / error.scope 等欄位。Handler parse 錯誤為 Facade scope10101000);各模組 logic/usecase 使用對應 scopeAuth=28、Member=29