Overview
TheSpecGenerator 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
VersionConfigProvider
required
Function that returns version configurations
RouteRegistrar
required
Function that registers routes for all versions
core/server/api/spec_generator.go:25
Example:
NewSpecGeneratorWithInitializer
VersionManagerInitializer
required
Function that creates and configures an APIVersionManager
core/server/api/spec_generator.go:32
Example:
Core Methods
Generate
string
required
Directory where spec files will be written
string
Optional: Generate spec for a single version only (empty string = all versions)
error- Error if generation or validation fails
core/server/api/spec_generator.go:38
Process:
- Creates output directory if it doesn’t exist
- Temporarily disables embedded spec loading (sets
PB_EXT_DISABLE_OPENAPI_SPECS=1) - Initializes version manager and registers routes
- Parses source code via AST
- Generates OpenAPI specs for each version
- Writes specs as
{version}.jsonfiles - Validates all generated specs
- Restores environment variables
Validate
string
required
Directory containing spec files to validate
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
Validation Functions
ValidateSpecs
string
required
Directory containing spec files
[]string
required
List of version identifiers to validate
core/server/api/spec_generator.go:156
ValidateSpecFile
string
required
Path to spec file
string
Expected version identifier (validates filename matches)
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.titleinfo.versionpaths(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
- Build-Time Generation: Always generate specs during builds, not at runtime in production
- Version Control: Commit generated specs to version control for reproducibility
- Validation: Always run
Validate()afterGenerate()in CI/CD - Single Responsibility: Keep spec generation separate from server startup
- Go Generate: Use
//go:generatedirectives for automatic regeneration - Output Directory: Use
./specsor./openapias standard output directory - 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
- OpenAPI version (
"3.0.3") - API info (title, version, description)
- Server URLs
- All endpoint paths and operations
- Component schemas
- Security schemes
Related
- APIVersionManager - Multi-version API management
- OpenAPI Embedded Loader - Runtime spec loading
- Build System Guide - Complete build configuration