Knowledge Base updated: 2026-11-22
oapi-codegen knowledge base
Orientation
oapi-codegen turns OpenAPI specifications into Go models, clients, and server integrations. Its command-line entry point loads input and configuration, then delegates to the code-generation pipeline.
Core generation behavior lives in pkg/codegen: it resolves references, gathers schemas and operations, applies filtering and configuration, and renders Go through shared and framework-specific templates.
Generated-output behavior is controlled by configuration, OpenAPI extensions, templates, and import/type mappings. Example applications under examples/ demonstrate generated clients and servers across supported frameworks.
Architecture
flowchart TD
CLI[cmd/oapi-codegen] --> Load[Input loading]
CLI --> Config[Generator configuration]
Load --> Refs[Reference resolution]
Refs --> Pipeline[pkg/codegen pipeline]
Config --> Pipeline
Pipeline --> Validate[Filtering and validation]
Pipeline --> Schemas[Schema and model generation]
Pipeline --> Operations[Operation and client generation]
Pipeline --> Servers[Server interface generation]
Schemas --> Templates[Template rendering]
Operations --> Templates
Servers --> Frameworks[Framework server templates]
Frameworks --> Templates
Templates --> Output[Generated Go source]
Examples[examples/] --> Output
Glossary
- Strict server: generated server API form that uses typed request and response contracts.
- Import mapping: configuration that maps referenced OpenAPI documents to Go import paths.
- Additional properties: OpenAPI object fields not explicitly declared in a schema, represented through generated support code.
- Overlay: an OpenAPI Overlay document that updates or removes elements from a source specification.
- Vendor extension: an OpenAPI
x-...field recognized by oapi-codegen to alter generated output.
Invariants
- Generated code is driven by the configured generation switches and output settings; configuration behavior is defined in pkg/…/configuration.go.
- Paths and operations are transformed into generated request, response, and client forms by pkg/…/operations.go.
- Schema output must preserve the generator's schema-to-Go mapping rules, implemented and tested in pkg/…/schema.go and pkg/…/schema_test.go.
- Rendered output is composed through shared helper logic and templates, centered on pkg/…/template_helpers.go and
pkg/codegen/templates/**. - Framework examples exercise generated integration output under
examples/**, including Fiber and Fiber v3 generated APIs.
Routing table
cmd/oapi-codegen/**-> Command-line generation interfacecmd/oapi-codegen/**-> Command execution and generation invocation- pkg/…/codegen.go -> OpenAPI generation pipeline
- pkg/…/gencontext.go -> OpenAPI generation pipeline
- pkg/…/configuration.go -> Generator configuration and output options
- pkg/…/externalref.go -> OpenAPI loading and reference resolution
- pkg/…/import_mappings.go -> OpenAPI loading and reference resolution
- pkg/…/schema.go -> Schema and Go model generation
- pkg/…/union.go -> Schema and Go model generation
- pkg/…/naming.go -> Schema and Go model generation
- pkg/…/operations.go -> Operation and client generation
- pkg/…/server_urls.go -> Operation and client generation
pkg/codegen/templates/server.tmpl-> Server interface and middleware generationpkg/codegen/templates/strict/**-> Server interface and middleware generationpkg/codegen/templates/chi/**-> Framework-specific server templatespkg/codegen/templates/echo/**-> Framework-specific server templatespkg/codegen/templates/fiber/**-> Framework-specific server templatespkg/codegen/templates/gin/**-> Framework-specific server templatespkg/codegen/templates/gorilla/**-> Framework-specific server templatespkg/codegen/templates/iris/**-> Framework-specific server templates- pkg/…/filter.go -> Specification filtering and validation
- pkg/…/merge_schemas_v*.go -> Specification filtering and validation
- pkg/…/template_helpers.go -> Template rendering system
pkg/codegen/templates/*.tmpl-> Template rendering system- pkg/…/extensions.go -> Customization and OpenAPI extensions
- pkg/…/extensions.go -> OpenAPI vendor extension behavior
- pkg/…/user_templates.go -> Generated-code customization
- pkg/…/configuration.go -> Generated-code customization
- pkg/…/overlay.go -> OpenAPI overlay processing
pkg/**/input/**-> Supporting librariespkg/util/**-> Input and document utilitiespkg/securityprovider/**-> Security provider helpersexamples/**-> Generated API integration examples