mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-07 20:47:45 +00:00
(feat): Circular / resolving errors returned with document creation. #18
Tristan made a good point, part of the doc building process performs a resolving check and circular reference check, which is ignored by the returned errors. So resolving errors are now unpacked into standard errors and returned. Not sure why gofmt has shifted everything around however.
This commit is contained in:
@@ -351,9 +351,7 @@ func TestStripeAsDoc(t *testing.T) {
|
||||
info, _ := datamodel.ExtractSpecInfo(data)
|
||||
var err []error
|
||||
lowDoc, err = lowv3.CreateDocument(info)
|
||||
if err != nil {
|
||||
panic("broken something")
|
||||
}
|
||||
assert.Len(t, err, 21)
|
||||
d := NewDocument(lowDoc)
|
||||
fmt.Println(d)
|
||||
}
|
||||
@@ -387,9 +385,7 @@ func TestCircularReferencesDoc(t *testing.T) {
|
||||
info, _ := datamodel.ExtractSpecInfo(data)
|
||||
var err []error
|
||||
lowDoc, err = lowv3.CreateDocument(info)
|
||||
if err != nil {
|
||||
panic("broken something")
|
||||
}
|
||||
assert.Len(t, err, 3)
|
||||
d := NewDocument(lowDoc)
|
||||
assert.Len(t, d.Components.Schemas, 9)
|
||||
assert.Len(t, d.Index.GetCircularReferences(), 3)
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
package v2
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/pb33f/libopenapi/datamodel"
|
||||
"github.com/pb33f/libopenapi/datamodel/low"
|
||||
"github.com/pb33f/libopenapi/datamodel/low/base"
|
||||
@@ -141,7 +142,15 @@ func CreateDocument(info *datamodel.SpecInfo) (*Swagger, []error) {
|
||||
|
||||
// create resolver and check for circular references.
|
||||
resolve := resolver.NewResolver(idx)
|
||||
_ = resolve.CheckForCircularReferences()
|
||||
resolvingErrors := resolve.CheckForCircularReferences()
|
||||
|
||||
if len(resolvingErrors) > 0 {
|
||||
for r := range resolvingErrors {
|
||||
errors = append(errors,
|
||||
fmt.Errorf("%s (%s) [%d:%d]", resolvingErrors[r].Error.Error(),
|
||||
resolvingErrors[r].Path, resolvingErrors[r].Node.Line, resolvingErrors[r].Node.Column))
|
||||
}
|
||||
}
|
||||
|
||||
extractionFuncs := []documentFunction{
|
||||
extractInfo,
|
||||
|
||||
@@ -334,3 +334,13 @@ func TestCreateDocument_InfoBad(t *testing.T) {
|
||||
}
|
||||
assert.Len(t, err, 1)
|
||||
}
|
||||
|
||||
func TestCircularReferenceError(t *testing.T) {
|
||||
|
||||
data, _ := ioutil.ReadFile("../../../test_specs/swagger-circular-tests.yaml")
|
||||
info, _ := datamodel.ExtractSpecInfo(data)
|
||||
circDoc, err := CreateDocument(info)
|
||||
assert.NotNil(t, circDoc)
|
||||
assert.Len(t, err, 3)
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package v3
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/pb33f/libopenapi/datamodel"
|
||||
"github.com/pb33f/libopenapi/datamodel/low"
|
||||
"github.com/pb33f/libopenapi/datamodel/low/base"
|
||||
@@ -25,12 +26,21 @@ func CreateDocument(info *datamodel.SpecInfo) (*Document, []error) {
|
||||
idx := index.NewSpecIndex(info.RootNode)
|
||||
doc.Index = idx
|
||||
|
||||
var errors []error
|
||||
|
||||
// create resolver and check for circular references.
|
||||
resolve := resolver.NewResolver(idx)
|
||||
_ = resolve.CheckForCircularReferences()
|
||||
resolvingErrors := resolve.CheckForCircularReferences()
|
||||
|
||||
if len(resolvingErrors) > 0 {
|
||||
for r := range resolvingErrors {
|
||||
errors = append(errors,
|
||||
fmt.Errorf("%s: %s [%d:%d]", resolvingErrors[r].Error.Error(),
|
||||
resolvingErrors[r].Path, resolvingErrors[r].Node.Line, resolvingErrors[r].Node.Column))
|
||||
}
|
||||
}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
var errors []error
|
||||
|
||||
doc.Extensions = low.ExtractExtensions(info.RootNode.Content[0])
|
||||
|
||||
|
||||
@@ -57,6 +57,15 @@ func BenchmarkCreateDocument_k8s(b *testing.B) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCircularReferenceError(t *testing.T) {
|
||||
|
||||
data, _ := ioutil.ReadFile("../../../test_specs/circular-tests.yaml")
|
||||
info, _ := datamodel.ExtractSpecInfo(data)
|
||||
circDoc, err := CreateDocument(info)
|
||||
assert.NotNil(t, circDoc)
|
||||
assert.Len(t, err, 3)
|
||||
}
|
||||
|
||||
func BenchmarkCreateDocument_Stripe(b *testing.B) {
|
||||
data, _ := ioutil.ReadFile("../../../test_specs/stripe.yaml")
|
||||
info, _ := datamodel.ExtractSpecInfo(data)
|
||||
@@ -85,9 +94,7 @@ func TestCreateDocumentStripe(t *testing.T) {
|
||||
data, _ := ioutil.ReadFile("../../../test_specs/stripe.yaml")
|
||||
info, _ := datamodel.ExtractSpecInfo(data)
|
||||
d, err := CreateDocument(info)
|
||||
if err != nil {
|
||||
panic("broken something")
|
||||
}
|
||||
assert.Len(t, err, 21)
|
||||
|
||||
assert.Equal(t, "3.0.0", d.Version.Value)
|
||||
assert.Equal(t, "Stripe API", d.Info.Value.Title.Value)
|
||||
|
||||
@@ -48,7 +48,7 @@ definitions:
|
||||
assert.NoError(t, err)
|
||||
|
||||
v2Doc, docErr := doc.BuildV2Model()
|
||||
assert.Len(t, docErr, 1)
|
||||
assert.Len(t, docErr, 2)
|
||||
assert.Nil(t, v2Doc)
|
||||
}
|
||||
|
||||
|
||||
51
test_specs/swagger-circular-tests.yaml
Normal file
51
test_specs/swagger-circular-tests.yaml
Normal file
@@ -0,0 +1,51 @@
|
||||
swagger: "2.0"
|
||||
paths:
|
||||
/burgers:
|
||||
post:
|
||||
responses:
|
||||
200:
|
||||
schema:
|
||||
$ref: '#/definitions/Nine'
|
||||
definitions:
|
||||
One:
|
||||
description: "test one"
|
||||
properties:
|
||||
things:
|
||||
"$ref": "#/definitions/Two"
|
||||
Two:
|
||||
description: "test two"
|
||||
properties:
|
||||
testThing:
|
||||
"$ref": "#/definitions/One"
|
||||
Three:
|
||||
description: "test three"
|
||||
properties:
|
||||
tester:
|
||||
"$ref": "#/definitions/Four"
|
||||
bester:
|
||||
"$ref": "#/definitions/Seven"
|
||||
yester:
|
||||
"$ref": "#/definitions/Seven"
|
||||
Four:
|
||||
description: "test four"
|
||||
properties:
|
||||
lemons:
|
||||
"$ref": "#/definitions/Nine"
|
||||
Five:
|
||||
properties:
|
||||
rice:
|
||||
"$ref": "#/definitions/Six"
|
||||
Six:
|
||||
properties:
|
||||
mints:
|
||||
"$ref": "#/definitions/Nine"
|
||||
Seven:
|
||||
properties:
|
||||
wow:
|
||||
"$ref": "#/definitions/Three"
|
||||
Nine:
|
||||
description: done.
|
||||
Ten:
|
||||
properties:
|
||||
yeah:
|
||||
"$ref": "#/definitions/Ten"
|
||||
Reference in New Issue
Block a user