Skip to main content

Overview

The SpecGenerator generates OpenAPI 3.0.3 specifications at build time by parsing Go source code and extracting endpoint metadata. It produces JSON spec files that can be embedded or served at runtime.

Type Definition

The spec generator operates in docs-only mode during builds, meaning it doesn’t start an HTTP server. It only parses code and generates documentation.

Constructor Functions

NewSpecGenerator

Creates a spec generator with version configs and route registrar.
VersionConfigProvider
required
Function that returns version configurations
RouteRegistrar
required
Function that registers routes for all versions
Location: core/server/api/spec_generator.go:25 Example:

NewSpecGeneratorWithInitializer

Creates a spec generator with a custom version manager initializer.
VersionManagerInitializer
required
Function that creates and configures an APIVersionManager
Location: core/server/api/spec_generator.go:32 Example:

Core Methods

Generate

Generates OpenAPI specs and writes them to disk.
string
required
Directory where spec files will be written
string
Optional: Generate spec for a single version only (empty string = all versions)
Returns:
  • error - Error if generation or validation fails
Location: core/server/api/spec_generator.go:38 Process:
  1. Creates output directory if it doesn’t exist
  2. Temporarily disables embedded spec loading (sets PB_EXT_DISABLE_OPENAPI_SPECS=1)
  3. Initializes version manager and registers routes
  4. Parses source code via AST
  5. Generates OpenAPI specs for each version
  6. Writes specs as {version}.json files
  7. Validates all generated specs
  8. Restores environment variables
Example:

Validate

Validates that all required spec files exist and are properly formatted.
string
required
Directory containing spec files to validate
Location: core/server/api/spec_generator.go:131 Checks:
  • All configured versions have corresponding spec files
  • Spec files are valid JSON
  • Required OpenAPI fields are present (openapi, info, paths)
  • Version identifiers match file names
Example:

Validation Functions

ValidateSpecs

Validates specs for a list of versions.
string
required
Directory containing spec files
[]string
required
List of version identifiers to validate
Location: core/server/api/spec_generator.go:156

ValidateSpecFile

Validates a single spec file.
string
required
Path to spec file
string
Expected version identifier (validates filename matches)
Location: core/server/api/spec_generator.go:171 Validation Checks:
  • File exists and is readable
  • Valid JSON format
  • Contains required OpenAPI fields:
    • openapi (version string)
    • info.title
    • info.version
    • paths (object)
  • Filename matches expected version pattern

Build Integration Examples

CLI Tool

Makefile Integration

Go Generate

CI/CD Pipeline

Complete Example

Environment Variables

The spec generator temporarily modifies environment variables during generation:
string
Set to "1" during generation to force runtime spec generation (disables disk loading)
string
Unset during generation to prevent reading from disk
Environment variables are automatically restored after generation completes, even if an error occurs.

Best Practices

  1. Build-Time Generation: Always generate specs during builds, not at runtime in production
  2. Version Control: Commit generated specs to version control for reproducibility
  3. Validation: Always run Validate() after Generate() in CI/CD
  4. Single Responsibility: Keep spec generation separate from server startup
  5. Go Generate: Use //go:generate directives for automatic regeneration
  6. Output Directory: Use ./specs or ./openapi as standard output directory
  7. CI Integration: Generate and validate specs in CI pipeline before builds

Common Patterns

Generate on File Change

Version-Specific Generation

Custom Output Format

Output Structure

Each spec file contains:
  • OpenAPI version ("3.0.3")
  • API info (title, version, description)
  • Server URLs
  • All endpoint paths and operations
  • Component schemas
  • Security schemes