Skip to main content

Overview

pb-ext uses three source file directives to control API documentation generation:
Directives are comment lines — they don’t affect runtime behavior, only 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 the package declaration or imports:

When to Use

Mark these files:
  • Handler files (handlers.go, routes.go, api.go)
  • Files containing func(c *core.RequestEvent) error functions
Don’t mark these files:
  • Type definition files (types.go, models.go) — auto-discovered via imports
  • Utility files without handlers (helpers.go, utils.go)
  • Test files (*_test.go)

// API_DESC — Handler Description

Purpose

Sets the description 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:
Auto-generated descriptions are basic. Always add // API_DESC for better documentation.

Writing Good Descriptions

1

Start with a verb

Get, Create, Update, Delete, List, Search, Calculate, etc.
2

Explain the purpose

What does this endpoint do? What data does it return?
3

Mention key features

Filtering, pagination, optional parameters, special behavior.
4

Keep it concise

1-2 sentences. Save detailed docs for external documentation.

// 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

This groups all user-related endpoints under a single “Users” tag in Swagger UI.
Endpoints appear in multiple tag groups.

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

1

Helper must accept *core.RequestEvent first

2

Helper must NOT return error

3

Generic helpers need string literal param names

When Annotations Are Needed vs Auto-Detected

In most cases, only // API_SOURCE is required. The other directives (// API_DESC, // API_TAGS) are optional but recommended for better documentation.

Real Annotated Examples from handlers.go

What’s detected:
  • Description: “Create a new todo item” (from // API_DESC)
  • Tags: [“Todos”] (from // API_TAGS)
  • Auth: Required (from if c.Auth == nil check)
  • Request: TodoRequest schema (from json.Decode(&req))
  • Response: Inline object schema with message and todo properties
What’s detected:
  • Description: “Get all todos with optional filtering”
  • Tags: [“Todos”]
  • Query params: completed, priority (both optional, type: string)
  • Response: Inline object with todos, count, filters properties
What’s detected:
  • Description: “Get a specific todo by ID”
  • Tags: [“Todos”]
  • Path param: id (required=true, type: string)
  • Response: Inline object with message and todo properties

Best Practices Summary

1

Mark handler files with // API_SOURCE

Place at the top of the file, before package declaration.
2

Add // API_DESC to all handlers

Don’t rely on auto-generated descriptions — write clear, concise explanations.
3

Use // API_TAGS for grouping

Consistent tag names improve Swagger UI navigation.
4

Extract parameter parsing into helpers

Domain helpers make code cleaner and params are still auto-detected.
5

Verify with the debug endpoint

Check /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:
Look for your handler in the handlers section. If it’s missing:
  • File doesn’t have // API_SOURCE
  • Function signature doesn’t match func(c *core.RequestEvent) error
For indirect parameter detection:
  • Helper must accept *core.RequestEvent as first parameter
  • Helper must NOT return error (that makes it a handler)
  • Generic helpers need string literal param names at call site
  • // API_SOURCE must be at the top of the file
  • // API_DESC and // API_TAGS must be in the function doc comment (directly above the function)
Look for parse errors in server logs:
Common causes: syntax errors, import issues, circular dependencies.

Next Steps

OpenAPI System

Deep dive into AST parsing internals

Route Registration

Learn manual and CRUD registration patterns