mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-07 04:20:14 +00:00
feat: Resolver tests for valid and invalid recursive models
This commit is contained in:
committed by
Dave Shanley
parent
5f92de63a4
commit
e8a954d5ae
@@ -2,11 +2,12 @@ package resolver
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
"github.com/pb33f/libopenapi/index"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/yaml.v3"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewResolver(t *testing.T) {
|
||||
@@ -25,7 +26,6 @@ func Benchmark_ResolveDocumentStripe(b *testing.B) {
|
||||
}
|
||||
|
||||
func TestResolver_ResolveComponents_CircularSpec(t *testing.T) {
|
||||
|
||||
circular, _ := ioutil.ReadFile("../test_specs/circular-tests.yaml")
|
||||
var rootNode yaml.Node
|
||||
yaml.Unmarshal(circular, &rootNode)
|
||||
@@ -43,7 +43,6 @@ func TestResolver_ResolveComponents_CircularSpec(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestResolver_CheckForCircularReferences(t *testing.T) {
|
||||
|
||||
circular, _ := ioutil.ReadFile("../test_specs/circular-tests.yaml")
|
||||
var rootNode yaml.Node
|
||||
yaml.Unmarshal(circular, &rootNode)
|
||||
@@ -62,8 +61,41 @@ func TestResolver_CheckForCircularReferences(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestResolver_DeepJourney(t *testing.T) {
|
||||
func TestResolver_CircularReferencesRequiredValid(t *testing.T) {
|
||||
circular, _ := ioutil.ReadFile("../test_specs/swagger-valid-recursive-model.yaml")
|
||||
var rootNode yaml.Node
|
||||
yaml.Unmarshal(circular, &rootNode)
|
||||
|
||||
index := index.NewSpecIndex(&rootNode)
|
||||
|
||||
resolver := NewResolver(index)
|
||||
assert.NotNil(t, resolver)
|
||||
|
||||
circ := resolver.CheckForCircularReferences()
|
||||
assert.Len(t, circ, 0)
|
||||
|
||||
_, err := yaml.Marshal(resolver.resolvedRoot)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestResolver_CircularReferencesRequiredInvalid(t *testing.T) {
|
||||
circular, _ := ioutil.ReadFile("../test_specs/swagger-invalid-recursive-model.yaml")
|
||||
var rootNode yaml.Node
|
||||
yaml.Unmarshal(circular, &rootNode)
|
||||
|
||||
index := index.NewSpecIndex(&rootNode)
|
||||
|
||||
resolver := NewResolver(index)
|
||||
assert.NotNil(t, resolver)
|
||||
|
||||
circ := resolver.CheckForCircularReferences()
|
||||
assert.Len(t, circ, 2)
|
||||
|
||||
_, err := yaml.Marshal(resolver.resolvedRoot)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestResolver_DeepJourney(t *testing.T) {
|
||||
var journey []*index.Reference
|
||||
for f := 0; f < 200; f++ {
|
||||
journey = append(journey, nil)
|
||||
@@ -71,11 +103,9 @@ func TestResolver_DeepJourney(t *testing.T) {
|
||||
index := index.NewSpecIndex(nil)
|
||||
resolver := NewResolver(index)
|
||||
assert.Nil(t, resolver.extractRelatives(nil, nil, journey, false))
|
||||
|
||||
}
|
||||
|
||||
func TestResolver_ResolveComponents_Stripe(t *testing.T) {
|
||||
|
||||
stripe, _ := ioutil.ReadFile("../test_specs/stripe.yaml")
|
||||
var rootNode yaml.Node
|
||||
yaml.Unmarshal(stripe, &rootNode)
|
||||
@@ -86,15 +116,13 @@ func TestResolver_ResolveComponents_Stripe(t *testing.T) {
|
||||
assert.NotNil(t, resolver)
|
||||
|
||||
circ := resolver.Resolve()
|
||||
assert.Len(t, circ, 23)
|
||||
assert.Len(t, circ, 3)
|
||||
|
||||
assert.Len(t, resolver.GetNonPolymorphicCircularErrors(), 3)
|
||||
assert.Len(t, resolver.GetPolymorphicCircularErrors(), 20)
|
||||
|
||||
}
|
||||
|
||||
func TestResolver_ResolveComponents_BurgerShop(t *testing.T) {
|
||||
|
||||
mixedref, _ := ioutil.ReadFile("../test_specs/burgershop.openapi.yaml")
|
||||
var rootNode yaml.Node
|
||||
yaml.Unmarshal(mixedref, &rootNode)
|
||||
@@ -106,11 +134,9 @@ func TestResolver_ResolveComponents_BurgerShop(t *testing.T) {
|
||||
|
||||
circ := resolver.Resolve()
|
||||
assert.Len(t, circ, 0)
|
||||
|
||||
}
|
||||
|
||||
func TestResolver_ResolveComponents_PolyNonCircRef(t *testing.T) {
|
||||
|
||||
yml := `paths:
|
||||
/hey:
|
||||
get:
|
||||
@@ -141,11 +167,9 @@ components:
|
||||
|
||||
circ := resolver.CheckForCircularReferences()
|
||||
assert.Len(t, circ, 0)
|
||||
|
||||
}
|
||||
|
||||
func TestResolver_ResolveComponents_MixedRef(t *testing.T) {
|
||||
|
||||
mixedref, _ := ioutil.ReadFile("../test_specs/mixedref-burgershop.openapi.yaml")
|
||||
var rootNode yaml.Node
|
||||
yaml.Unmarshal(mixedref, &rootNode)
|
||||
@@ -157,11 +181,9 @@ func TestResolver_ResolveComponents_MixedRef(t *testing.T) {
|
||||
|
||||
circ := resolver.Resolve()
|
||||
assert.Len(t, circ, 10)
|
||||
|
||||
}
|
||||
|
||||
func TestResolver_ResolveComponents_k8s(t *testing.T) {
|
||||
|
||||
k8s, _ := ioutil.ReadFile("../test_specs/k8s.json")
|
||||
var rootNode yaml.Node
|
||||
yaml.Unmarshal(k8s, &rootNode)
|
||||
@@ -172,12 +194,11 @@ func TestResolver_ResolveComponents_k8s(t *testing.T) {
|
||||
assert.NotNil(t, resolver)
|
||||
|
||||
circ := resolver.Resolve()
|
||||
assert.Len(t, circ, 1)
|
||||
assert.Len(t, circ, 0)
|
||||
}
|
||||
|
||||
// Example of how to resolve the Stripe OpenAPI specification, and check for circular reference errors
|
||||
func ExampleNewResolver() {
|
||||
|
||||
// create a yaml.Node reference as a root node.
|
||||
var rootNode yaml.Node
|
||||
|
||||
@@ -203,5 +224,4 @@ func ExampleNewResolver() {
|
||||
fmt.Printf("There are %d circular reference errors, %d of them are polymorphic errors, %d are not",
|
||||
len(circularErrors), len(resolver.GetPolymorphicCircularErrors()), len(resolver.GetNonPolymorphicCircularErrors()))
|
||||
// Output: There are 23 circular reference errors, 20 of them are polymorphic errors, 3 are not
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user