Overview
pb-ext uses three source file directives to control API documentation generation:// API_SOURCE — File Marker
Purpose
Marks a Go file for AST parsing. Only files with this directive are analyzed for handler functions. Type definitions (structs) in imported packages are discovered automatically.Placement
Place at the top of the file, before thepackage declaration or imports:
When to Use
Mark these files:- Handler files (
handlers.go,routes.go,api.go) - Files containing
func(c *core.RequestEvent) errorfunctions
- Type definition files (
types.go,models.go) — auto-discovered via imports - Utility files without handlers (
helpers.go,utils.go) - Test files (
*_test.go)
Example: handlers.go
Example: handlers.go
// API_DESC — Handler Description
Purpose
Sets thedescription field in the OpenAPI operation. This appears in Swagger UI and helps developers understand what the endpoint does.
Placement
Place in the function’s doc comment (the comment block directly above the function):Fallback Behavior
If omitted, the system auto-generates a description from the handler name:// API_DESC for better documentation.Writing Good Descriptions
Start with a verb
Get, Create, Update, Delete, List, Search, Calculate, etc.Explain the purpose
Mention key features
Keep it concise
// API_TAGS — OpenAPI Tags
Purpose
Groups related endpoints in Swagger UI. Tags appear as collapsible sections in the navigation sidebar.Placement
Place in the function’s doc comment, same as// API_DESC:
Format
Comma-separated list of tag names:Fallback Behavior
If omitted, the system auto-generates a tag from the path:Best Practices
Organize by Feature
Organize by Feature
Consistency Matters
Consistency Matters
Indirect Parameter Detection
What Requires Annotations?
Nothing. Indirect parameter detection is fully automatic for helper functions that accept*core.RequestEvent as the first parameter.
How It Works
The parser automatically extracts parameters from helper functions:Requirements for Auto-Detection
Helper must accept *core.RequestEvent first
Helper must NOT return error
Generic helpers need string literal param names
When Annotations Are Needed vs Auto-Detected
// API_SOURCE is required. The other directives (// API_DESC, // API_TAGS) are optional but recommended for better documentation.Real Annotated Examples from handlers.go
createTodoHandler (Full Example)
createTodoHandler (Full Example)
- Description: “Create a new todo item” (from
// API_DESC) - Tags: [“Todos”] (from
// API_TAGS) - Auth: Required (from
if c.Auth == nilcheck) - Request:
TodoRequestschema (fromjson.Decode(&req)) - Response: Inline object schema with
messageandtodoproperties
getTodosHandler (With Query Params)
getTodosHandler (With Query Params)
- Description: “Get all todos with optional filtering”
- Tags: [“Todos”]
- Query params:
completed,priority(both optional, type: string) - Response: Inline object with
todos,count,filtersproperties
getTodoHandler (With Path Param)
getTodoHandler (With Path Param)
- Description: “Get a specific todo by ID”
- Tags: [“Todos”]
- Path param:
id(required=true, type: string) - Response: Inline object with
messageandtodoproperties
Best Practices Summary
Mark handler files with // API_SOURCE
Add // API_DESC to all handlers
Use // API_TAGS for grouping
Extract parameter parsing into helpers
Verify with the debug endpoint
/api/docs/debug/ast to confirm all metadata is detected.Debugging Missing Metadata
If a parameter, schema, or description is missing from the OpenAPI spec:1. Check the debug endpoint
1. Check the debug endpoint
handlers section. If it’s missing:- File doesn’t have
// API_SOURCE - Function signature doesn’t match
func(c *core.RequestEvent) error
2. Verify helper function signatures
2. Verify helper function signatures
- Helper must accept
*core.RequestEventas first parameter - Helper must NOT return
error(that makes it a handler) - Generic helpers need string literal param names at call site
3. Check directive placement
3. Check directive placement
// API_SOURCEmust be at the top of the file// API_DESCand// API_TAGSmust be in the function doc comment (directly above the function)
4. Review AST parser logs
4. Review AST parser logs