How AST Parsing Works
The OpenAPI documentation system uses Go’s AST (Abstract Syntax Tree) parser to analyze your source code at compile time. This extracts metadata about your handlers, request/response types, and parameters without requiring runtime reflection or manual annotations.Pipeline Overview
The parser operates in two phases: first parsing files marked with
// API_SOURCE, then following local imports to resolve type definitions. This is fully automatic — no configuration needed.What’s Auto-Detected
Request Body Detection
The parser detects request body types from these patterns:BindBody(&varName) or Decode(&varName) calls. It resolves varName to its declared type and generates the JSON schema from the struct definition.
Response Schema Detection
Response schemas are extracted fromc.JSON(status, data) calls by analyzing the second argument:
map[string]any and []map[string]any return types.
Query Parameters
Direct detection from these patterns:Header Parameters
Same patterns as query parameters, but for headers:Path Parameters
Detected fromPathValue() calls:
Authentication Requirements
Detected from PocketBase auth patterns:Source File Directives
Three directives control how the parser processes your files:// API_SOURCE — File Marker
Place at the top of your Go file (before package declaration or imports) to mark it for AST parsing:
Only files with
// API_SOURCE are parsed for handlers. Type definitions (structs) in imported packages are automatically discovered — no directive needed.// API_DESC — Handler Description
Place in the function’s doc comment (the comment block directly above the function):
description field in the OpenAPI operation.
// API_TAGS — OpenAPI Tags
Comma-separated list of tags for grouping endpoints:
Debug Endpoint Usage
The debug endpoint provides full visibility into the AST parsing pipeline:Example Debug Output
Example Debug Output
When Annotations Are Needed
The system auto-detects most patterns, but you should add annotations when:Best Practices
1
Mark handler files with // API_SOURCE
Place the directive at the top of files containing handler functions.
2
Use explicit request/response types
Define structs for complex request/response bodies instead of inline maps.
3
Extract parameter parsing into helpers
Domain helpers make code cleaner and params are still auto-detected.
4
Add descriptions and tags
Use
// API_DESC and // API_TAGS for better Swagger UI navigation.5
Test with the debug endpoint
Verify that all parameters and schemas are detected correctly.
Next Steps
Versioned Routing
Manage multiple API versions with isolated registries
Route Registration
Learn the two registration patterns: manual and CRUD