oastools

package module
v1.32.0 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: MIT Imports: 2 Imported by: 0

README

oastools - for validating, parsing, fixing, converting, diffing, joining, and building specs

A complete, self-contained OpenAPI toolkit for Go with minimal dependencies.

Go Report Card codecov Go Reference

Parse, validate, fix, convert, diff, join, generate, and build OpenAPI specs (2.0-3.2.0) with zero runtime dependencies beyond YAML parsing.

Highlights

  • Minimal Dependencies - Only go.yaml.in/yaml and golang.org/x/tools at runtime
  • Battle-Tested - 3000+ tests against 10 production APIs (Discord, Stripe, GitHub, MS Graph 34MB)
  • Complete Toolset - 11 packages covering the full OpenAPI lifecycle
  • Performance Optimized - 90+ benchmarks; pre-parsed workflows 9-150x faster
  • Type-Safe Cloning - Generated DeepCopy() methods preserve types across OAS versions (no JSON marshal hacks)
  • Enterprise Ready - Structured errors with errors.Is()/errors.As(), pluggable logging, configurable resource limits
  • Well Documented - Every package has godoc, runnable examples, and deep dive guides for advanced usage
  • Semantic Deduplication - Automatically consolidate structurally identical schemas, reducing document size

Package Ecosystem

Package Description
parser Parse & analyze OAS files from files, URLs, or readers
validator Validate specs with structural & semantic checks
fixer Auto-fix common validation errors
httpvalidator Validate HTTP requests/responses against OAS at runtime
converter Convert between OAS 2.0 and 3.x
joiner Merge multiple OAS documents with schema deduplication
overlay Apply OpenAPI Overlay v1.0.0 with JSONPath targeting
differ Detect breaking changes between versions
generator Generate Go client/server code with security support
builder Programmatically construct OAS documents with deduplication
oaserrors Structured error types for programmatic handling

All packages include comprehensive documentation with runnable examples. See individual package pages on pkg.go.dev for API details.

Deep Dive Guides

For comprehensive examples and advanced usage patterns:

Package Deep Dive
builder Programmatic API Construction
converter Version Conversion
differ Breaking Change Detection
fixer Automatic Fixes
generator Code Generation (Client/Server/Types)
httpvalidator HTTP Request/Response Validation
joiner Multi-Document Merging
overlay Overlay Transformations
parser Parsing & Reference Resolution
validator Specification Validation

Installation

CLI Tool

# Homebrew (macOS and Linux)
brew tap erraggy/oastools && brew install oastools

# Go install
go install github.com/erraggy/oastools/cmd/oastools@latest

Pre-built binaries for macOS, Linux, and Windows are available on the Releases page.

Go Library

go get github.com/erraggy/oastools@latest

Requires Go 1.24 or higher.

Quick Start

CLI

# Validate a spec (from file or URL)
oastools validate openapi.yaml
oastools validate https://example.com/api/openapi.yaml

# Convert between versions
oastools convert -t 3.0.3 swagger.yaml -o openapi.yaml

# Compare specs and detect breaking changes
oastools diff --breaking api-v1.yaml api-v2.yaml

# Fix common validation errors
oastools fix api.yaml -o fixed.yaml

# Generate Go client/server code
oastools generate --client --server -o ./generated -p api openapi.yaml

# Generate client with full OAuth2 support
oastools generate --client --oauth2-flows --readme -o ./client -p api openapi.yaml

# Generate server with router, validation middleware, and request binding
oastools generate --server --server-all -o ./server -p api openapi.yaml

# Merge multiple specs
oastools join -o merged.yaml base.yaml extensions.yaml

# Apply overlays for environment-specific customizations
oastools overlay apply -s openapi.yaml production.yaml -o production-api.yaml

# Validate an overlay document
oastools overlay validate changes.yaml

# Pipeline support
cat swagger.yaml | oastools convert -q -t 3.0.3 - > openapi.yaml
oastools validate --format json openapi.yaml | jq '.valid'

Library

import (
    "github.com/erraggy/oastools/parser"
    "github.com/erraggy/oastools/validator"
    "github.com/erraggy/oastools/httpvalidator"
    "github.com/erraggy/oastools/differ"
    "github.com/erraggy/oastools/overlay"
    "github.com/erraggy/oastools/generator"
    "github.com/erraggy/oastools/builder"
)

// Parse
result, _ := parser.ParseWithOptions(parser.WithFilePath("api.yaml"))

// Validate specification
vResult, _ := validator.ValidateWithOptions(validator.WithFilePath("api.yaml"))

// Validate HTTP request/response at runtime
hvResult, _ := httpvalidator.ValidateRequestWithOptions(
    req,
    httpvalidator.WithFilePath("api.yaml"),
    httpvalidator.WithStrictMode(true),
)

// Diff
dResult, _ := differ.DiffWithOptions(
    differ.WithSourceFilePath("v1.yaml"),
    differ.WithTargetFilePath("v2.yaml"),
)

// Overlay with dry-run preview
oResult, _ := overlay.DryRunWithOptions(
    overlay.WithSpecFilePath("api.yaml"),
    overlay.WithOverlayFilePath("changes.yaml"),
)

// Generate Go client code
gResult, _ := generator.GenerateWithOptions(
    generator.WithFilePath("api.yaml"),
    generator.WithPackageName("api"),
    generator.WithClient(true),
)

// Build a spec programmatically
spec := builder.New(parser.OASVersion300).
    SetTitle("My API").
    SetVersion("1.0.0").
    AddServer("https://api.example.com", "Production")
doc, _ := spec.BuildOAS3()

For complete API documentation and examples, see pkg.go.dev or the Developer Guide.

Why oastools?

Minimal Dependencies

github.com/erraggy/oastools
├── go.yaml.in/yaml/v4  (YAML parsing)
└── golang.org/x/tools  (Code generation - imports analysis)

Unlike many OpenAPI tools that pull in dozens of transitive dependencies, oastools is designed to be self-contained. The stretchr/testify dependency is test-only and not included in your production builds.

Battle-Tested Quality

The entire toolchain is validated against a corpus of 10 real-world production APIs:

Domain APIs
FinTech Stripe, Plaid
Developer Tools GitHub, DigitalOcean
Communications Discord (OAS 3.1)
Enterprise Microsoft Graph (34MB, 18k+ operations)
Location Google Maps
Public US National Weather Service
Reference Petstore (OAS 2.0)
Productivity Asana

This corpus spans OAS 2.0 through 3.1, JSON and YAML formats, and document sizes from 20KB to 34MB.

Performance

Pre-parsed workflows eliminate redundant parsing when processing multiple operations:

Method Speedup
ValidateParsed() 31x faster
ConvertParsed() 9x faster
JoinParsed() 150x faster
DiffParsed() 81x faster
FixParsed() ~10x faster
ApplyParsed() ~15x faster

JSON marshaling is optimized for 25-32% better performance with 29-37% fewer allocations. See benchmarks.md for detailed analysis.

Type-Safe Document Cloning

All parser types include generated DeepCopy() methods for safe document mutation. Unlike JSON marshal/unmarshal approaches used by other tools, oastools provides:

  • Type preservation - Polymorphic fields maintain their actual types (e.g., Schema.Type as string vs []string for OAS 3.1)
  • Version-aware copying - Handles OAS version differences correctly (ExclusiveMinimum as bool in 3.0 vs number in 3.1)
  • Extension preservation - All x-* extension fields are deep copied
  • Performance - Direct struct copying without serialization overhead
// Safe mutation without affecting the original
copy := result.OAS3Document.DeepCopy()
copy.Info.Title = "Modified API"

Enterprise-Grade Error Handling

The oaserrors package provides structured error types that work with Go's standard errors.Is() and errors.As():

import (
    "errors"
    "github.com/erraggy/oastools/oaserrors"
    "github.com/erraggy/oastools/parser"
)

result, err := parser.ParseWithOptions(parser.WithFilePath("api.yaml"))
if err != nil {
    // Check error category with errors.Is()
    if errors.Is(err, oaserrors.ErrPathTraversal) {
        log.Fatal("Security: path traversal attempt blocked")
    }

    // Extract details with errors.As()
    var refErr *oaserrors.ReferenceError
    if errors.As(err, &refErr) {
        log.Printf("Failed to resolve: %s (type: %s)", refErr.Ref, refErr.RefType)
    }
}

Error types include ParseError, ReferenceError, ValidationError, ResourceLimitError, ConversionError, and ConfigError.

Configurable Resource Limits

Protect against resource exhaustion with configurable limits:

result, err := parser.ParseWithOptions(
    parser.WithFilePath("api.yaml"),
    parser.WithMaxRefDepth(50),           // Max $ref nesting (default: 100)
    parser.WithMaxCachedDocuments(200),   // Max cached external docs (default: 100)
    parser.WithMaxFileSize(20*1024*1024), // Max file size in bytes (default: 10MB)
)

Supported OpenAPI Versions

Version Specification
2.0 (Swagger) spec
3.0.0 - 3.0.4 spec
3.1.0 - 3.1.2 spec
3.2.0 spec

Features:

  • Automatic format detection and preservation (JSON/YAML)
  • External reference resolution (local files; HTTP with opt-in flag)
  • JSON Pointer array index support (#/paths/~1users/get/parameters/0)
  • Full JSON Schema Draft 2020-12 compliance for OAS 3.1+ (including unevaluatedProperties, unevaluatedItems, content keywords)
  • Path traversal protection for security

Documentation

📚 Developer Guide - Complete library usage with examples for all 10 packages

📖 CLI Reference - Full command documentation with all flags, options, and output formats

Resource Description
pkg.go.dev API reference with runnable examples
Breaking Changes Guide Understanding breaking change detection
Benchmarks Detailed performance analysis

Development

make check  # Run all quality checks (tidy, fmt, lint, test)
make test   # Run tests with coverage
make build  # Build CLI binary

Documentation

make docs-serve  # Preview docs locally at http://127.0.0.1:8000/oastools/
make docs-build  # Build static site to site/

The documentation site is automatically deployed to GitHub Pages on every push to main.

See WORKFLOW.md for the complete development process.

Contributing

  1. Fork and create a feature branch
  2. Run make check before committing
  3. Follow conventional commits (e.g., feat(parser): add feature)
  4. Submit a PR with testing checklist

See WORKFLOW.md for guidelines and AGENTS.md for AI agent setup.

License

MIT

All code generated by Claude Code using claude-4-5-sonnet/opus with minor edits and full control by @erraggy

Documentation

Overview

Package oastools provides tools for parsing, validating, fixing, converting, joining, comparing, generating code from, and building OpenAPI Specification (OAS) documents from OAS 2.0 through OAS 3.2.0.

The library consists of ten packages:

For installation, CLI usage, and examples, see: https://github.com/erraggy/oastools

For detailed API documentation and examples, see the individual package pages.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildInfo added in v1.17.2

func BuildInfo() string

BuildInfo returns a formatted string with all build metadata

func BuildTime added in v1.17.2

func BuildTime() string

BuildTime returns the build timestamp or 'unknown' if run from source

func Commit added in v1.17.2

func Commit() string

Commit returns the git commit hash or 'unknown' if run from source

func GoVersion added in v1.17.2

func GoVersion() string

GoVersion returns the Go version used to build the binary

func UserAgent

func UserAgent() string

UserAgent returns the User-Agent string to use

func Version

func Version() string

Version returns the compiled version or 'dev' if run from source

Types

This section is empty.

Directories

Path Synopsis
Package builder provides programmatic construction of OpenAPI Specification documents.
Package builder provides programmatic construction of OpenAPI Specification documents.
cmd
oastools command
oastools/commands
Package commands provides CLI command handlers for oastools.
Package commands provides CLI command handlers for oastools.
Package converter provides version conversion for OpenAPI Specification documents.
Package converter provides version conversion for OpenAPI Specification documents.
Package differ provides OpenAPI specification comparison and breaking change detection.
Package differ provides OpenAPI specification comparison and breaking change detection.
Package fixer provides automatic fixes for common OpenAPI Specification validation errors.
Package fixer provides automatic fixes for common OpenAPI Specification validation errors.
Package generator provides Go code generation from OpenAPI Specification documents.
Package generator provides Go code generation from OpenAPI Specification documents.
Package httpvalidator validates HTTP requests and responses against OpenAPI specifications.
Package httpvalidator validates HTTP requests and responses against OpenAPI specifications.
internal
cliutil
Package cliutil provides utilities for CLI operations.
Package cliutil provides utilities for CLI operations.
codegen/deepcopy command
Package main generates DeepCopy methods for parser package types.
Package main generates DeepCopy methods for parser package types.
corpusutil
Package corpusutil provides utilities for loading and managing the integration test corpus of real-world public OpenAPI specifications.
Package corpusutil provides utilities for loading and managing the integration test corpus of real-world public OpenAPI specifications.
httputil
Package httputil provides HTTP-related validation utilities and constants.
Package httputil provides HTTP-related validation utilities and constants.
issues
Package issues provides a unified issue type for validation and conversion problems.
Package issues provides a unified issue type for validation and conversion problems.
jsonpath
Package jsonpath provides a minimal JSONPath implementation for OpenAPI Overlay support.
Package jsonpath provides a minimal JSONPath implementation for OpenAPI Overlay support.
schemautil
Package schemautil provides utilities for working with OpenAPI schema types.
Package schemautil provides utilities for working with OpenAPI schema types.
severity
Package severity provides severity level constants and utilities for issues reported by validator, converter, differ, and generator packages.
Package severity provides severity level constants and utilities for issues reported by validator, converter, differ, and generator packages.
testutil
Package testutil provides test utilities and fixtures for unit tests.
Package testutil provides test utilities and fixtures for unit tests.
Package joiner provides joining for multiple OpenAPI Specification documents.
Package joiner provides joining for multiple OpenAPI Specification documents.
Package oaserrors provides structured error types for the oastools library.
Package oaserrors provides structured error types for the oastools library.
Package overlay provides support for OpenAPI Overlay Specification v1.0.0.
Package overlay provides support for OpenAPI Overlay Specification v1.0.0.
Package parser provides parsing for OpenAPI Specification documents.
Package parser provides parsing for OpenAPI Specification documents.
internal/jsonhelpers
Package jsonhelpers provides helper functions for JSON marshaling and unmarshaling with support for extension fields (x-* properties) in OpenAPI specifications.
Package jsonhelpers provides helper functions for JSON marshaling and unmarshaling with support for extension fields (x-* properties) in OpenAPI specifications.
Package validator provides validation for OpenAPI Specification documents.
Package validator provides validation for OpenAPI Specification documents.

Jump to

Keyboard shortcuts

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