beaverspec

command module
v0.0.0-...-81fd40d Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: MIT Imports: 5 Imported by: 0

README ΒΆ

BeaverSpec 🦫

A modern OpenAPI code generator for Go

BeaverSpec generates production-ready Go server and client code from OpenAPI 3.x specifications β€” no JVM required, no YAML-hell, just a single Go binary.

Go Version OpenAPI

Why BeaverSpec?

  • No JVM β€” single Go binary, install in one command
  • Go-idiomatic output β€” structs, interfaces, error returns, context.Context
  • Four frameworks β€” net/http, Chi, Echo, Gin β€” switch with one flag
  • Template customization β€” override any template with your own text/template file
  • Remote specs β€” point at a URL, no need to download specs manually
  • Selective generation β€” models only, server only, or everything

Status

Phase 1: βœ… Complete β€” Core architecture, OpenAPI parsing, Go models generation Phase 2: βœ… Complete β€” Server/client generation, framework support, security, validation, config system, integration polish

Features

  • βœ… Complete OpenAPI 3.x parsing (local files and remote URLs)
  • βœ… Go models with JSON tags and Validate() methods
  • βœ… allOf (struct embedding), oneOf/anyOf (discriminator, union types), enums, maps, arrays
  • βœ… HTTP client with typed methods per endpoint
  • βœ… Server handler interface + mock implementations
  • βœ… Four framework targets: net-http, chi, echo, gin
  • βœ… Request/response validation with typed error models
  • βœ… Content negotiation (multiple produces/consumes types)
  • βœ… Security scheme extraction (Bearer, API Key, Basic, OAuth2)
  • βœ… Middleware generation (logging, CORS, rate limiter β€” stdlib only)
  • βœ… Graceful shutdown example main.go per framework
  • βœ… External $ref resolution (local files and URLs)
  • βœ… Template customization and override system
  • βœ… beaver.yaml config file with full schema support

Quick Start

# Install
go install github.com/imerljak/beaverspec/cmd/beaver@latest

# Generate from a local spec
beaver -spec openapi.yaml -output ./generated -module github.com/example/myapi

# Generate from a remote spec
beaver -spec https://petstore3.swagger.io/api/v3/openapi.json \
  -output ./generated -module github.com/example/petstore

# Choose a framework
beaver -spec openapi.yaml -output ./generated -module github.com/example/myapi -framework chi

# Preview without writing files
beaver -spec openapi.yaml -output ./generated -module github.com/example/myapi -dry-run

Generated Files

For a spec with endpoints and schemas, beaver generates up to 9 files:

generated/
β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ models.go          # Typed structs, json tags, Validate() methods
β”‚   └── models_test.go     # Auto-generated validation tests
β”œβ”€β”€ client/
β”‚   └── client.go          # Typed HTTP client
β”œβ”€β”€ server/
β”‚   β”œβ”€β”€ interface.go       # Handler interface β€” implement this
β”‚   β”œβ”€β”€ mocks.go           # Mock implementations for quick start
β”‚   β”œβ”€β”€ handlers.go        # HTTP adapter (routes to interface)
β”‚   β”œβ”€β”€ routes.go          # Route registration per tag group
β”‚   └── middleware.go      # Logging, CORS, rate limiter (stdlib)
└── cmd/server/
    └── main.go            # Runnable example entrypoint (edit freely)

Config File

Create beaver.yaml next to your spec to avoid repeating flags:

spec: ./openapi.yaml
output: ./generated
module: github.com/example/myapi
framework: chi

generate:
  models: true
  client: true
  server: true

exclude:
  tags: [internal]

types:
  overrides:
    - format: uuid
      go: "github.com/google/uuid".UUID

Then just run beaver with no flags.

CLI Reference

Flag Default Description
-spec β€” OpenAPI spec path or URL (required)
-output . Output directory
-module github.com/example/project Go module path
-framework net-http Server framework: net-http, chi, echo, gin
-config auto Config file path (auto-discovers beaver.yaml next to spec)
-package models Package name for generated models
-generator go Generator to use
-dry-run false Print files that would be generated, don't write
-debug false Enable debug logging
-version β€” Print version and exit

Project Structure

beaverspec/
β”œβ”€β”€ cmd/beaver/          # CLI entry point
β”œβ”€β”€ pkg/
β”‚   β”œβ”€β”€ core/            # Core types and interfaces
β”‚   β”œβ”€β”€ parser/          # OpenAPI parsing and normalization
β”‚   β”œβ”€β”€ config/          # Config file loading and merging
β”‚   β”œβ”€β”€ codegen/         # Validation codegen helpers
β”‚   └── template/        # Template engine
β”œβ”€β”€ generators/
β”‚   └── golang/          # Go generator + templates
β”‚       └── templates/   # Per-framework Go templates
└── examples/            # Example OpenAPI specs

Documentation

Development

go build -o beaver ./cmd/beaver
go test ./...
go run ./cmd/beaver -spec examples/petstore.yaml -output /tmp/gen -module github.com/example/petstore -dry-run

License

MIT License β€” see LICENSE for details.

Acknowledgments


Beavers are nature's engineers, known for building robust, well-structured dams. 🦫

Documentation ΒΆ

The Go Gopher

There is no documentation for this package.

Directories ΒΆ

Path Synopsis
cmd
beaver command
generators
pkg

Jump to

Keyboard shortcuts

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