openapi

package module
v0.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package openapi provides a Go library for generating OpenAPI 3.1 specifications from Go source code using swagger directives.

Quick Start

Generate an OpenAPI spec from your Go code:

spec, err := openapi.Generate(
	openapi.WithDir("."),
	openapi.WithPattern("./..."),
	openapi.WithOutput("openapi.yaml", "yaml"),
)

Directives

The library scans Go source code for the following directives:

  • swagger:meta - API metadata (title, version, description)
  • swagger:model - Schema definitions
  • swagger:route - Operation definitions
  • swagger:parameters - Parameter definitions
  • swagger:enum - Enum definitions

Caching

The library supports incremental builds through caching. By default, parsed schemas and routes are cached in a .openapi directory. Only files that have changed since the last build are re-parsed.

To disable caching:

spec, err := openapi.Generate(
	openapi.WithCache(false),
)

Index

Constants

This section is empty.

Variables

View Source
var WithCache = generator.WithCache

WithCache enables or disables incremental caching.

View Source
var WithCleanUnused = generator.WithCleanUnused

WithCleanUnused enables or disables removal of unreferenced schemas.

View Source
var WithDir = generator.WithDir

WithDir sets the root directory to scan from.

View Source
var WithEnumRefs = generator.WithEnumRefs

WithEnumRefs enables generating enums as $ref references instead of inline.

View Source
var WithFlatten = generator.WithFlatten

WithFlatten enables or disables schema flattening (inlining $refs).

View Source
var WithIgnorePaths = generator.WithIgnorePaths

WithIgnorePaths sets path patterns to exclude during scanning.

View Source
var WithOutput = generator.WithOutput

WithOutput sets the output file path and format ("yaml" or "json").

View Source
var WithPattern = generator.WithPattern

WithPattern sets the package pattern to scan (e.g., "./...", "./api/...").

View Source
var WithValidation = generator.WithValidation

WithValidation enables or disables spec validation after generation.

Functions

func Generate

func Generate(opts ...Option) (*spec.OpenAPI, error)

Generate creates an OpenAPI specification from Go source code. It scans the specified packages for swagger directives and generates a complete OpenAPI 3.1 specification.

Types

type Option

type Option = generator.Option

Option is a function type for configuring the generator.

Directories

Path Synopsis
Package cache provides incremental build caching for OpenAPI spec generation.
Package cache provides incremental build caching for OpenAPI spec generation.
Package generator provides OpenAPI specification generation from Go source code.
Package generator provides OpenAPI specification generation from Go source code.
Package parser provides an extensible parsing system for swagger directives.
Package parser provides an extensible parsing system for swagger directives.
tags
Package tags provides built-in parsers for common swagger directives.
Package tags provides built-in parsers for common swagger directives.
Package sdkgen generates Go SDK packages from OpenAPI specs and SDK config files.
Package sdkgen generates Go SDK packages from OpenAPI specs and SDK config files.
Package swagger provides an HTTP handler to serve Swagger UI with embedded OpenAPI specs.
Package swagger provides an HTTP handler to serve Swagger UI with embedded OpenAPI specs.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL