Skip to main content

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:
How it works: The parser tracks variable declarations and finds 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 from c.JSON(status, data) calls by analyzing the second argument:
Deep Schema Resolution: When a handler returns the result of a helper function, the parser analyzes the helper’s body to extract the exact schema. This works for map[string]any and []map[string]any return types.

Query Parameters

Direct detection from these patterns:
Indirect detection via helper functions:
Generic helpers must accept *core.RequestEvent as the first parameter and the param name as a string literal in the second parameter. The parser extracts the name from the call site.

Header Parameters

Same patterns as query parameters, but for headers:

Path Parameters

Detected from PathValue() calls:
Path parameters are always marked as required in the OpenAPI spec.

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):
This becomes the description field in the OpenAPI operation.

// API_TAGS — OpenAPI Tags

Comma-separated list of tags for grouping endpoints:
Tags appear in Swagger UI as navigation groups.

Debug Endpoint Usage

The debug endpoint provides full visibility into the AST parsing pipeline:

When Annotations Are Needed

The system auto-detects most patterns, but you should add annotations when:
If a parameter or schema is missing from the generated spec, check the debug endpoint to see what the parser detected. Common issues:
  • Helper function doesn’t accept *core.RequestEvent as first param
  • Generic helper param name is not a string literal
  • File missing // API_SOURCE directive

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