Fixer Package Deep Dive
Try it Online
No installation required! Try the fixer in your browser →
The fixer package provides automatic fixes for common OpenAPI Specification validation errors, supporting both OAS 2.0 and OAS 3.x documents.
Table of Contents
- Overview
- Fix Types
- API Styles
- Practical Examples
- Generic Naming Strategies
- Configuration Reference
- Best Practices
Overview
The fixer analyzes OAS documents and applies fixes for issues that would cause validation failures. It preserves the input file format (JSON or YAML) for output consistency.
Common use cases:
- Add missing path parameters automatically
- Rename schemas with invalid characters (e.g., Response[User])
- Remove unused schema definitions
- Clean up empty path items
Fix Types
| Fix Type | Default | Description |
|---|---|---|
FixTypeMissingPathParameter |
✅ Enabled | Adds Parameter objects for undeclared path template variables |
FixTypeRenamedGenericSchema |
❌ Disabled | Renames schemas containing URL-unsafe characters |
FixTypePrunedUnusedSchema |
❌ Disabled | Removes unreferenced schema definitions |
FixTypePrunedEmptyPath |
❌ Disabled | Removes paths with no HTTP operations |
Why are some fixes disabled by default?
Schema renaming and pruning involve expensive operations (walking all references, computing unused schemas) that can significantly slow down processing of large specifications. Enable them explicitly when needed.
API Styles
Functional Options (Recommended)
result, err := fixer.FixWithOptions(
fixer.WithFilePath("openapi.yaml"),
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Applied %d fixes\n", result.FixCount)
Struct-Based (Reusable)
f := fixer.New()
f.InferTypes = true
result1, _ := f.Fix("api1.yaml")
result2, _ := f.Fix("api2.yaml")
Enable Specific Fixes
result, err := fixer.FixWithOptions(
fixer.WithFilePath("api.yaml"),
fixer.WithEnabledFixes(
fixer.FixTypeMissingPathParameter,
fixer.FixTypeRenamedGenericSchema,
fixer.FixTypePrunedUnusedSchema,
),
)
Enable ALL Fixes
f := fixer.New()
f.EnabledFixes = []fixer.FixType{} // Empty slice enables all
result, _ := f.Fix("api.yaml")
Practical Examples
See also: Basic example, Functional options on pkg.go.dev
Basic Fixing
result, err := fixer.FixWithOptions(
fixer.WithFilePath("openapi.yaml"),
)
if err != nil {
log.Fatal(err)
}
for _, fix := range result.Fixes {
fmt.Printf("Fixed: %s at %s\n", fix.Type, fix.Path)
}
Type Inference
See also: Type inference example on pkg.go.dev
When enabled, the fixer infers parameter types from naming conventions:
| Pattern | Inferred Type |
|---|---|
*id, *Id, *ID |
integer |
*uuid, *guid |
string (format: uuid) |
| Everything else | string |
result, err := fixer.FixWithOptions(
fixer.WithFilePath("openapi.yaml"),
fixer.WithInferTypes(true),
)
Dry-Run Mode
See also: Dry-run example on pkg.go.dev
Preview fixes without applying them:
result, err := fixer.FixWithOptions(
fixer.WithFilePath("openapi.yaml"),
fixer.WithDryRun(true),
)
fmt.Printf("Would apply %d fixes\n", result.FixCount)
// result.Document is unchanged
Generic Schema Renaming
See also: Generic naming example on pkg.go.dev
result, err := fixer.FixWithOptions(
fixer.WithFilePath("api.yaml"),
fixer.WithEnabledFixes(fixer.FixTypeRenamedGenericSchema),
fixer.WithGenericNaming(fixer.GenericNamingOf),
)
// Response[User] → ResponseOfUser
Generic Naming Strategies
See also: Naming config example, Strategy example on pkg.go.dev
When fixing invalid schema names like Response[User]:
| Strategy | Result |
|---|---|
GenericNamingUnderscore |
Response_User_ |
GenericNamingOf |
ResponseOfUser |
GenericNamingFor |
ResponseForUser |
GenericNamingFlattened |
ResponseUser |
GenericNamingDot |
Response.User |
Configure with WithGenericNaming() or WithGenericNamingConfig().
Configuration Reference
Functional Options
| Option | Description |
|---|---|
WithFilePath(path) |
Path to specification file |
WithParsed(result) |
Pre-parsed ParseResult |
WithInferTypes(bool) |
Infer parameter types from names |
WithEnabledFixes(fixes...) |
Specific fix types to enable |
WithGenericNaming(strategy) |
Naming strategy for generic schemas |
WithGenericNamingConfig(cfg) |
Custom naming configuration |
WithDryRun(bool) |
Preview without applying |
Fixer Fields
| Field | Type | Description |
|---|---|---|
InferTypes |
bool |
Enable type inference |
EnabledFixes |
[]FixType |
Fix types to apply (empty = all) |
GenericNamingConfig |
*GenericNamingConfig |
Custom naming rules |
DryRun |
bool |
Preview mode |
FixResult Fields
| Field | Type | Description |
|---|---|---|
Document |
any |
Fixed document |
Fixes |
[]Fix |
Applied fixes with details |
FixCount |
int |
Total fixes applied |
SourceFormat |
SourceFormat |
Preserved format |
Best Practices
- Start with defaults -
FixTypeMissingPathParameterhandles the most common issue - Enable expensive fixes only when needed - Schema pruning/renaming can be slow on large specs
- Use dry-run in CI - Verify what would change before applying
- Validate after fixing - Ensure the fixed document is valid
- Pipeline usage -
oastools fix api.yaml | oastools validate -q -
Learn More
For additional examples and complete API documentation:
- 📦 API Reference on pkg.go.dev - Complete API documentation with all examples
- 🔧 Selective fixes example - Enable specific fix types
- 🗑️ Prune unused schemas - Remove unreferenced definitions
- 📁 Prune empty paths - Clean up empty path items
- ✅ Check results example - Inspect applied fixes