Documentation
¶
Overview ¶
Package converter provides version conversion for OpenAPI Specification documents.
The converter supports OAS 2.0 ↔ OAS 3.x conversions, performing best-effort conversion with detailed issue tracking. Features converted include servers, schemas, parameters, security schemes, and request/response bodies. The converter preserves the input file format (JSON or YAML) in the ConversionResult.SourceFormat field, allowing tools to maintain format consistency when writing output.
Quick Start ¶
Convert a file:
result, err := converter.Convert("swagger.yaml", "3.0.3")
if err != nil {
log.Fatal(err)
}
if result.HasCriticalIssues() {
fmt.Printf("%d critical issue(s)\n", result.CriticalCount)
}
Or use a reusable Converter instance:
c := converter.New()
c.StrictMode = false
result1, _ := c.Convert("api1.yaml", "3.0.3")
result2, _ := c.Convert("api2.yaml", "3.0.3")
Conversion Issues ¶
The converter tracks three severity levels: Info (conversion choices), Warning (lossy conversions), and Critical (features that cannot be converted). Some OAS 3.x features (webhooks, callbacks, links, TRACE method) cannot convert to OAS 2.0. Some OAS 2.0 features (collectionFormat, allowEmptyValue) may not map perfectly to OAS 3.x. See the examples in example_test.go for handling issues.
Converting with the Validator Package ¶
Always validate converted documents for the target version:
convResult, _ := converter.Convert("swagger.yaml", "3.0.3")
data, _ := yaml.Marshal(convResult.Document)
tmpFile := "temp.yaml"
os.WriteFile(tmpFile, data, 0600)
valResult, _ := validator.Validate(tmpFile, true, false)
if !valResult.Valid {
fmt.Printf("Conversion produced invalid document\n")
}
See the exported ConversionResult and ConversionIssue types for complete details.
Example ¶
Example demonstrates basic conversion using the convenience function
package main
import (
"fmt"
"log"
"github.com/erraggy/oastools/converter"
)
func main() {
// Convert an OAS 2.0 specification to OAS 3.0.3
result, err := converter.Convert("testdata/petstore-2.0.yaml", "3.0.3")
if err != nil {
log.Fatal(err)
}
// Check for critical issues
if result.HasCriticalIssues() {
fmt.Printf("Conversion completed with %d critical issue(s)\n", result.CriticalCount)
return
}
fmt.Printf("Successfully converted from %s to %s\n", result.SourceVersion, result.TargetVersion)
fmt.Printf("Issues: %d info, %d warnings, %d critical\n",
result.InfoCount, result.WarningCount, result.CriticalCount)
}
Example (HandleConversionIssues) ¶
Example_handleConversionIssues demonstrates processing conversion issues
package main
import (
"fmt"
"github.com/erraggy/oastools/converter"
)
func main() {
result, _ := converter.Convert("openapi.yaml", "2.0")
// Categorize issues by severity
for _, issue := range result.Issues {
switch issue.Severity {
case converter.SeverityCritical:
fmt.Printf("CRITICAL [%s]: %s\n", issue.Path, issue.Message)
if issue.Context != "" {
fmt.Printf(" Context: %s\n", issue.Context)
}
case converter.SeverityWarning:
fmt.Printf("WARNING [%s]: %s\n", issue.Path, issue.Message)
case converter.SeverityInfo:
fmt.Printf("INFO [%s]: %s\n", issue.Path, issue.Message)
}
}
// Summary
fmt.Printf("\nSummary: %d critical, %d warnings, %d info\n",
result.CriticalCount, result.WarningCount, result.InfoCount)
}
Index ¶
Examples ¶
Constants ¶
const ( // SeverityInfo indicates informational messages about conversion choices SeverityInfo = severity.SeverityInfo // SeverityWarning indicates lossy conversions or best-effort transformations SeverityWarning = severity.SeverityWarning // SeverityCritical indicates features that cannot be converted (data loss) SeverityCritical = severity.SeverityCritical )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ConversionIssue ¶
ConversionIssue represents a single conversion issue or limitation
type ConversionResult ¶
type ConversionResult struct {
// Document contains the converted document (*parser.OAS2Document or *parser.OAS3Document)
Document any
// SourceVersion is the detected source OAS version string
SourceVersion string
// SourceOASVersion is the enumerated source OAS version
SourceOASVersion parser.OASVersion
// SourceFormat is the format of the source file (JSON or YAML)
SourceFormat parser.SourceFormat
// TargetVersion is the target OAS version string
TargetVersion string
// TargetOASVersion is the enumerated target OAS version
TargetOASVersion parser.OASVersion
// Issues contains all conversion issues grouped by severity
Issues []ConversionIssue
// InfoCount is the total number of info messages
InfoCount int
// WarningCount is the total number of warnings
WarningCount int
// CriticalCount is the total number of critical issues
CriticalCount int
// Success is true if conversion completed without critical issues
Success bool
// LoadTime is the time taken to load the source data
LoadTime time.Duration
// SourceSize is the size of the source data in bytes
SourceSize int64
}
ConversionResult contains the results of converting an OpenAPI specification
func Convert ¶
func Convert(specPath string, targetVersion string) (*ConversionResult, error)
Convert is a convenience function that converts an OpenAPI specification file to a target version with the specified options. It's equivalent to creating a Converter with New() and calling Convert().
For one-off conversion operations, this function provides a simpler API. For converting multiple files with the same configuration, create a Converter instance and reuse it.
Example:
result, err := converter.Convert("swagger.yaml", "3.0.3")
if err != nil {
log.Fatal(err)
}
if result.HasCriticalIssues() {
// Handle critical issues
}
func ConvertParsed ¶
func ConvertParsed(parseResult parser.ParseResult, targetVersion string) (*ConversionResult, error)
ConvertParsed is a convenience function that converts an already-parsed OpenAPI specification to a target version.
Example:
parseResult, _ := parser.Parse("swagger.yaml", false, true)
result, err := converter.ConvertParsed(*parseResult, "3.0.3")
func (*ConversionResult) HasCriticalIssues ¶
func (r *ConversionResult) HasCriticalIssues() bool
HasCriticalIssues returns true if there are any critical issues
func (*ConversionResult) HasWarnings ¶
func (r *ConversionResult) HasWarnings() bool
HasWarnings returns true if there are any warnings
type Converter ¶
type Converter struct {
// StrictMode causes conversion to fail on any issues (even warnings)
StrictMode bool
// IncludeInfo determines whether to include informational messages
IncludeInfo bool
// UserAgent is the User-Agent string used when fetching URLs
// Defaults to "oastools" if not set
UserAgent string
}
Converter handles OpenAPI specification version conversion
func (*Converter) Convert ¶
func (c *Converter) Convert(specPath string, targetVersion string) (*ConversionResult, error)
Convert converts an OpenAPI specification file to a target version
func (*Converter) ConvertParsed ¶
func (c *Converter) ConvertParsed(parseResult parser.ParseResult, targetVersionStr string) (*ConversionResult, error)
ConvertParsed converts an already-parsed OpenAPI specification to a target version