Skip to main content

Overview

The APIVersionManager orchestrates versioned API documentation by maintaining isolated registries, parsers, and configurations for each API version. It enables side-by-side deployment of multiple API versions with automatic OpenAPI spec generation and Swagger UI support.

Type Definition

Each API version gets its own isolated ASTParser, SchemaGenerator, and APIRegistry. This ensures complete separation between versions with independent component schemas and route registrations.

Constructor Functions

NewAPIVersionManager

Creates a new version manager with empty state. Returns:
  • *APIVersionManager - New manager instance
Example:

NewAPIVersionManagerWithDefault

Creates a version manager with a pre-set default version.
string
required
Version identifier to use as default (e.g., “v1”, “v2”)
Example:

Version Management

RegisterVersion

Registers a new API version with its own registry and configuration.
string
required
Version identifier (e.g., “v1”, “v2”, “beta”)
*APIDocsConfig
Version-specific configuration (can be nil for defaults)
Returns:
  • error - Error if version already exists or validation fails
Behavior:
  1. Validates version string format
  2. Creates version-specific AST parser and schema generator
  3. Creates isolated API registry for the version
  4. Sets version-specific server URL in OpenAPI spec
  5. Automatically becomes default version if first registered
Example:

RemoveVersion

Removes a version and its registry. Cannot remove the default version.
string
required
Version identifier to remove

GetVersionConfig

Retrieves the configuration for a specific version. Location: core/server/api/version_manager.go:198

GetVersionRegistry

Retrieves the API registry for a specific version. Location: core/server/api/version_manager.go:209

Route Registration

SetVersionRouteRegistrar

Sets the route registration callback for a version.
string
required
Version identifier
func(*VersionedAPIRouter)
required
Function that registers routes for this version
Example:

RegisterAllVersionRoutes

Registers all version routes to a ServeEvent router and docs registries. Location: core/server/api/version_manager.go:300

RegisterAllVersionRoutesForDocs

Registers all version routes only to docs registries (for build-time spec generation). Location: core/server/api/version_manager.go:315

Version Information

GetDefaultVersion

Returns the default version identifier.

SetDefaultVersion

Sets the default API version. Version must exist.

GetAllVersions

Returns all registered versions (sorted). Returns a copy to prevent external modifications.

GetVersionInfo

Returns detailed information about a specific version. Returns:
Location: core/server/api/version_manager.go:650

HTTP Handlers

RegisterWithServer

Registers version management endpoints with the PocketBase app:
  • GET /api/docs/versions - List all versions (requires auth)
  • GET /api/docs/debug/ast - AST pipeline introspection (requires auth)
  • GET /api/docs/{version} - Version-specific OpenAPI spec (requires auth)
  • GET /api/docs/{version}/spec - Public OpenAPI spec (if PublicSwagger enabled)
  • GET /api/docs/{version}/swagger - Swagger UI (if PublicSwagger enabled)
  • GET /api/{version}/schema/config - Schema configuration (requires auth)
Location: core/server/api/version_manager.go:706

VersionsHandler

HTTP handler that returns list of all available API versions. Response:
Location: core/server/api/version_manager.go:756

GetVersionOpenAPI

Returns the complete OpenAPI schema for a specific version. Priority:
  1. Direct spec loading from disk (if available)
  2. Fallback to runtime registry generation
Location: core/server/api/version_manager.go:827

ServeSwaggerUI

Serves the Swagger UI HTML page for the given API version with dark mode support. Location: core/server/api/version_manager.go:898
Uses SwaggerDark theme by Amoenus (MIT License) for automatic dark mode support based on user’s system preferences.

Complete Example

Best Practices

  1. Version Naming: Use semantic versions (“v1”, “v2”) or descriptive names (“stable”, “beta”)
  2. Default Version: Always set a stable version as default
  3. Public Access: Only enable PublicSwagger for stable, production-ready versions
  4. Route Isolation: Use SetPrefix() to namespace each version’s routes
  5. Deprecation: Mark old versions with Status: "deprecated" before removal
  6. Testing: Use RegisterAllVersionRoutesForDocs() for build-time spec generation