OpenAPI Specification
Learn how to generate OpenAPI specifications from Go code with automatic parameter discovery and schema generation
3 minute read
The Rivaas OpenAPI package provides automatic OpenAPI 3.0.4 and 3.1.2 specification generation from Go code. Uses struct tags and reflection with a clean, type-safe API. Minimal boilerplate required.
Features
- Clean API - Builder-style
API.Generate()method for specification generation - Type-Safe Version Selection -
V30xandV31xconstants with IDE autocomplete - Fluent HTTP Method Constructors -
GET(),POST(),PUT(), etc. for clean operation definitions - Functional Options - Consistent
With*pattern for all configuration - Type-Safe Warning Diagnostics -
diagpackage for fine-grained warning control - Automatic Parameter Discovery - Extracts query, path, header, and cookie parameters from struct tags
- Schema Generation - Converts Go types to OpenAPI schemas automatically
- Swagger UI Configuration - Built-in, customizable Swagger UI settings
- Semantic Operation IDs - Auto-generates operation IDs from HTTP methods and paths
- Security Schemes - Support for Bearer, API Key, OAuth2, and OpenID Connect
- Collision-Resistant Naming - Schema names use
pkgname.TypeNameformat to prevent collisions - Built-in Validation - Validates generated specs against official OpenAPI meta-schemas
- Standalone Validator - Validate external OpenAPI specs with pre-compiled schemas
Quick Start
Here’s a 30-second example to get you started:
package main
import (
"context"
"fmt"
"log"
"rivaas.dev/openapi"
)
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
type CreateUserRequest struct {
Name string `json:"name" validate:"required"`
Email string `json:"email" validate:"required,email"`
}
func main() {
api := openapi.MustNew(
openapi.WithTitle("My API", "1.0.0"),
openapi.WithInfoDescription("API for managing users"),
openapi.WithServer("http://localhost:8080", "Local development"),
openapi.WithBearerAuth("bearerAuth", "JWT authentication"),
)
result, err := api.Generate(context.Background(),
openapi.GET("/users/:id",
openapi.WithSummary("Get user"),
openapi.WithResponse(200, User{}),
openapi.WithSecurity("bearerAuth"),
),
openapi.POST("/users",
openapi.WithSummary("Create user"),
openapi.WithRequest(CreateUserRequest{}),
openapi.WithResponse(201, User{}),
),
openapi.DELETE("/users/:id",
openapi.WithSummary("Delete user"),
openapi.WithResponse(204, nil),
openapi.WithSecurity("bearerAuth"),
),
)
if err != nil {
log.Fatal(err)
}
// Check for warnings (optional)
if len(result.Warnings) > 0 {
fmt.Printf("Generated with %d warnings\n", len(result.Warnings))
}
fmt.Println(string(result.JSON))
}
How It Works
- API configuration is done through functional options with
With*prefix - Operations are defined using HTTP method constructors:
GET(),POST(), etc. - Types are automatically converted to OpenAPI schemas using reflection
- Parameters are discovered from struct tags:
path,query,header,cookie - Validation is optional but recommended for production use
Learning Path
Follow these guides to master OpenAPI specification generation with Rivaas:
- Installation - Get started with the openapi package
- Basic Usage - Learn the fundamentals of generating specifications
- Configuration - Configure API info, servers, and version selection
- Security - Add authentication and authorization schemes
- Operations - Define HTTP operations with methods and options
- Auto-Discovery - Use struct tags for automatic parameter discovery
- Schema Generation - Understand Go type to OpenAPI schema conversion
- Swagger UI - Customize the Swagger UI interface
- Validation - Validate generated specifications
- Diagnostics - Handle warnings with type-safe diagnostics
- Advanced Usage - Extensions, custom operation IDs, and strict mode
- Examples - See real-world usage patterns
Next Steps
- Start with Installation to set up the openapi package
- Explore the API Reference for complete technical details
- Check out code examples on GitHub
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.