openapi

package
v0.0.0-...-27b1c5b Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2025 License: MIT Imports: 8 Imported by: 0

README

OpenAPI Testing Package

The OpenAPI package provides comprehensive testing capabilities for OpenAPI specifications using pb33f.io's libopenapi library. This package integrates seamlessly with the GoWright testing framework to validate OpenAPI specifications, detect breaking changes, and identify circular references.

Features

  • Specification Validation: Validates OpenAPI specifications against the OpenAPI 3.0.3 standard
  • Breaking Changes Detection: Compares specifications across git commits to identify breaking changes
  • Circular Reference Detection: Identifies circular references in schema definitions
  • GoWright Integration: Seamless integration with the GoWright testing framework
  • Flexible Test Builder: Fluent API for building customized test suites

Installation

The package uses pb33f.io's libopenapi library. Dependencies are automatically managed through Go modules:

go mod tidy

Quick Start

Basic Usage
package main

import (
    "fmt"
    "github.com/gowright/framework/pkg/openapi"
)

func main() {
    // Create an OpenAPI tester
    tester, err := openapi.NewOpenAPITester("path/to/your/openapi.yaml")
    if err != nil {
        panic(err)
    }

    // Validate the specification
    result := tester.ValidateSpec()
    fmt.Printf("Validation: %s (Passed: %t)\n", result.Message, result.Passed)

    // Check for circular references
    circularResult := tester.DetectCircularReferences()
    fmt.Printf("Circular References: %s (Passed: %t)\n", circularResult.Message, circularResult.Passed)

    // Check for breaking changes (requires git)
    breakingResult := tester.CheckBreakingChanges("HEAD~1")
    fmt.Printf("Breaking Changes: %s (Passed: %t)\n", breakingResult.Message, breakingResult.Passed)
}
GoWright Integration
package main

import (
    "github.com/gowright/framework/pkg/core"
    "github.com/gowright/framework/pkg/openapi"
)

func main() {
    // Create OpenAPI integration
    integration, err := openapi.NewOpenAPIIntegration("openapi.yaml")
    if err != nil {
        panic(err)
    }

    // Create a full test suite
    suite := integration.CreateFullTestSuite("HEAD~1")

    // Run with GoWright
    gowright := core.NewGoWright()
    gowright.AddTestSuite(suite)
    gowright.RunTests()
}
Test Builder Pattern
package main

import (
    "github.com/gowright/framework/pkg/openapi"
)

func main() {
    // Build a customized test suite
    suite, err := openapi.NewOpenAPITestBuilder("openapi.yaml").
        WithValidation(true).
        WithCircularReferenceDetection(true).
        WithBreakingChangesDetection(true, "HEAD~1").
        Build()

    if err != nil {
        panic(err)
    }

    // Use the suite with GoWright or run tests individually
    for _, test := range suite.Tests {
        // Run individual tests
    }
}

API Reference

OpenAPITester

The main testing class that provides OpenAPI validation capabilities.

Methods
  • NewOpenAPITester(specPath string) (*OpenAPITester, error): Creates a new tester instance
  • ValidateSpec() *TestResult: Validates the OpenAPI specification
  • DetectCircularReferences() *TestResult: Detects circular references
  • CheckBreakingChanges(previousCommit string) *TestResult: Compares with previous version
  • RunAllTests(previousCommit string) []*TestResult: Runs all available tests
  • GetSummary(results []*TestResult) string: Returns a summary of test results
OpenAPIIntegration

Provides integration with the GoWright testing framework.

Methods
  • NewOpenAPIIntegration(specPath string) (*OpenAPIIntegration, error): Creates integration instance
  • CreateValidationTest() core.TestCase: Creates a validation test case
  • CreateCircularReferenceTest() core.TestCase: Creates a circular reference test case
  • CreateBreakingChangesTest(previousCommit string) core.TestCase: Creates a breaking changes test case
  • CreateFullTestSuite(previousCommit string) *core.TestSuite: Creates a complete test suite
OpenAPITestBuilder

Fluent API for building customized test suites.

Methods
  • NewOpenAPITestBuilder(specPath string) *OpenAPITestBuilder: Creates a new builder
  • WithPreviousCommit(commit string) *OpenAPITestBuilder: Sets previous commit for breaking changes
  • WithValidation(enabled bool) *OpenAPITestBuilder: Enables/disables validation testing
  • WithCircularReferenceDetection(enabled bool) *OpenAPITestBuilder: Enables/disables circular reference detection
  • WithBreakingChangesDetection(enabled bool, commit string) *OpenAPITestBuilder: Enables/disables breaking changes detection
  • Build() (*core.TestSuite, error): Builds the test suite

Test Results

All test methods return TestResult objects with the following structure:

type TestResult struct {
    TestName     string                // Name of the test
    Passed       bool                  // Whether the test passed
    Message      string                // Summary message
    Details      []string              // Detailed information
    Errors       []ValidationError     // Validation errors found
    Warnings     []ValidationWarning   // Validation warnings
}
Validation Errors
type ValidationError struct {
    Path        string    // Path in the specification where error occurred
    Message     string    // Error message
    Severity    string    // Error severity level
    Line        int       // Line number (if available)
    Column      int       // Column number (if available)
}
Breaking Changes
type BreakingChange struct {
    Type        string      // Type of breaking change
    Path        string      // Path where change occurred
    OldValue    interface{} // Previous value
    NewValue    interface{} // New value
    Description string      // Description of the change
    Impact      string      // Impact on API consumers
}

Validation Features

Specification Validation
  • Validates OpenAPI version
  • Checks required fields (info, paths)
  • Validates path items and operations
  • Validates component schemas
  • Checks response definitions
  • Validates parameter definitions
Breaking Changes Detection

The package detects various types of breaking changes:

  • PATH_REMOVED: API paths that were removed
  • OPERATION_REMOVED: HTTP operations that were removed
  • REQUIRED_PARAMETER_ADDED: New required parameters
  • SCHEMA_REMOVED: Schema definitions that were removed
  • RESPONSE_REMOVED: Response definitions that were removed
Circular Reference Detection
  • Detects circular references in schema definitions
  • Provides detailed reference chains
  • Identifies problematic paths in the specification

Git Integration

The breaking changes detection feature requires git to be available and the specification file to be tracked in git. The package uses git commands to retrieve previous versions of the specification for comparison.

Examples

See the examples/openapi_testing_example.go file for comprehensive usage examples including:

  • Basic validation
  • Comprehensive testing
  • Breaking changes detection
  • Test builder pattern usage
  • GoWright framework integration

Error Handling

The package provides detailed error information for various scenarios:

  • File not found errors
  • Invalid YAML/JSON parsing errors
  • OpenAPI specification parsing errors
  • Git command execution errors
  • Validation errors with specific paths and messages

Best Practices

  1. Version Control: Keep your OpenAPI specifications in version control for breaking changes detection
  2. Continuous Integration: Integrate OpenAPI testing into your CI/CD pipeline
  3. Comprehensive Testing: Use all three test types (validation, circular references, breaking changes) for thorough coverage
  4. Error Handling: Always check for errors when creating tester instances
  5. Test Organization: Use the test builder pattern for complex test configurations

Dependencies

  • github.com/pb33f/libopenapi: Core OpenAPI parsing and validation
  • github.com/pb33f/libopenapi-validator: Additional validation capabilities
  • github.com/stretchr/testify: Testing utilities (for tests)
  • github.com/gowright/framework/pkg/core: GoWright framework integration

Contributing

When contributing to this package:

  1. Ensure all tests pass
  2. Add tests for new functionality
  3. Update documentation for API changes
  4. Follow Go best practices and conventions
  5. Test with various OpenAPI specification formats

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BreakingChange

type BreakingChange struct {
	Type        string
	Path        string
	OldValue    interface{}
	NewValue    interface{}
	Description string
	Impact      string
}

BreakingChange represents a breaking change detected between versions

type CircularReference

type CircularReference struct {
	Path        string
	RefChain    []string
	Description string
}

CircularReference represents a circular reference in the OpenAPI spec

type OpenAPIBreakingChangesTest

type OpenAPIBreakingChangesTest struct {
	// contains filtered or unexported fields
}

OpenAPIBreakingChangesTest implements the Test interface for breaking changes detection

func (*OpenAPIBreakingChangesTest) Execute

Execute runs the breaking changes detection test

func (*OpenAPIBreakingChangesTest) GetName

func (t *OpenAPIBreakingChangesTest) GetName() string

GetName returns the test name

type OpenAPICircularReferenceTest

type OpenAPICircularReferenceTest struct {
	// contains filtered or unexported fields
}

OpenAPICircularReferenceTest implements the Test interface for circular reference detection

func (*OpenAPICircularReferenceTest) Execute

Execute runs the circular reference detection test

func (*OpenAPICircularReferenceTest) GetName

func (t *OpenAPICircularReferenceTest) GetName() string

GetName returns the test name

type OpenAPIIntegration

type OpenAPIIntegration struct {
	// contains filtered or unexported fields
}

OpenAPIIntegration provides integration with the GoWright testing framework

func NewOpenAPIIntegration

func NewOpenAPIIntegration(specPath string) (*OpenAPIIntegration, error)

NewOpenAPIIntegration creates a new OpenAPI integration instance

func (*OpenAPIIntegration) CreateBreakingChangesTest

func (oi *OpenAPIIntegration) CreateBreakingChangesTest(previousCommit string) core.Test

CreateBreakingChangesTest creates a GoWright test for breaking changes detection

func (*OpenAPIIntegration) CreateCircularReferenceTest

func (oi *OpenAPIIntegration) CreateCircularReferenceTest() core.Test

CreateCircularReferenceTest creates a GoWright test for circular reference detection

func (*OpenAPIIntegration) CreateFullTestSuite

func (oi *OpenAPIIntegration) CreateFullTestSuite(previousCommit string) *core.TestSuite

CreateFullTestSuite creates a complete test suite for OpenAPI testing

func (*OpenAPIIntegration) CreateValidationTest

func (oi *OpenAPIIntegration) CreateValidationTest() core.Test

CreateValidationTest creates a GoWright test for OpenAPI validation

type OpenAPITestBuilder

type OpenAPITestBuilder struct {
	// contains filtered or unexported fields
}

OpenAPITestBuilder provides a fluent interface for building OpenAPI tests

func NewOpenAPITestBuilder

func NewOpenAPITestBuilder(specPath string) *OpenAPITestBuilder

NewOpenAPITestBuilder creates a new OpenAPI test builder

func (*OpenAPITestBuilder) Build

func (b *OpenAPITestBuilder) Build() (*core.TestSuite, error)

Build creates the test suite based on the builder configuration

func (*OpenAPITestBuilder) WithBreakingChangesDetection

func (b *OpenAPITestBuilder) WithBreakingChangesDetection(enabled bool, commit string) *OpenAPITestBuilder

WithBreakingChangesDetection enables or disables breaking changes detection

func (*OpenAPITestBuilder) WithCircularReferenceDetection

func (b *OpenAPITestBuilder) WithCircularReferenceDetection(enabled bool) *OpenAPITestBuilder

WithCircularReferenceDetection enables or disables circular reference detection

func (*OpenAPITestBuilder) WithPreviousCommit

func (b *OpenAPITestBuilder) WithPreviousCommit(commit string) *OpenAPITestBuilder

WithPreviousCommit sets the previous commit for breaking changes detection

func (*OpenAPITestBuilder) WithValidation

func (b *OpenAPITestBuilder) WithValidation(enabled bool) *OpenAPITestBuilder

WithValidation enables or disables validation testing

type OpenAPITester

type OpenAPITester struct {
	// contains filtered or unexported fields
}

OpenAPITester provides comprehensive OpenAPI specification testing capabilities

func NewOpenAPITester

func NewOpenAPITester(specPath string) (*OpenAPITester, error)

NewOpenAPITester creates a new OpenAPI tester instance

func (*OpenAPITester) CheckBreakingChanges

func (t *OpenAPITester) CheckBreakingChanges(previousCommit string) *TestResult

CheckBreakingChanges compares the current spec with the previous version from git

func (*OpenAPITester) DetectCircularReferences

func (t *OpenAPITester) DetectCircularReferences() *TestResult

DetectCircularReferences detects circular references in the OpenAPI specification

func (*OpenAPITester) GetSummary

func (t *OpenAPITester) GetSummary(results []*TestResult) string

GetSummary returns a summary of all test results

func (*OpenAPITester) RunAllTests

func (t *OpenAPITester) RunAllTests(previousCommit string) []*TestResult

RunAllTests runs all available OpenAPI tests

func (*OpenAPITester) ValidateSpec

func (t *OpenAPITester) ValidateSpec() *TestResult

ValidateSpec validates the OpenAPI specification against the OpenAPI standard

type OpenAPIValidationTest

type OpenAPIValidationTest struct {
	// contains filtered or unexported fields
}

OpenAPIValidationTest implements the Test interface for OpenAPI validation

func (*OpenAPIValidationTest) Execute

Execute runs the OpenAPI validation test

func (*OpenAPIValidationTest) GetName

func (t *OpenAPIValidationTest) GetName() string

GetName returns the test name

type TestResult

type TestResult struct {
	TestName string
	Passed   bool
	Message  string
	Details  []string
	Errors   []ValidationError
	Warnings []ValidationWarning
}

TestResult represents the result of an OpenAPI test

type ValidationError

type ValidationError struct {
	Path     string
	Message  string
	Severity string
	Line     int
	Column   int
}

ValidationError represents a validation error found in the OpenAPI spec

type ValidationWarning

type ValidationWarning struct {
	Path       string
	Message    string
	Suggestion string
}

ValidationWarning represents a validation warning

Jump to

Keyboard shortcuts

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