mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-09 12:37:49 +00:00
Merge branch 'main' into ordered-libopenapi
This commit is contained in:
@@ -435,7 +435,7 @@ func TestStripeAsDoc(t *testing.T) {
|
|||||||
info, _ := datamodel.ExtractSpecInfo(data)
|
info, _ := datamodel.ExtractSpecInfo(data)
|
||||||
var err error
|
var err error
|
||||||
lowDoc, err = lowv3.CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
|
lowDoc, err = lowv3.CreateDocumentFromConfig(info, datamodel.NewDocumentConfiguration())
|
||||||
assert.Len(t, utils.UnwrapErrors(err), 2)
|
assert.Len(t, utils.UnwrapErrors(err), 1)
|
||||||
d := NewDocument(lowDoc)
|
d := NewDocument(lowDoc)
|
||||||
assert.NotNil(t, d)
|
assert.NotNil(t, d)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -262,7 +262,7 @@ func TestCreateDocumentStripe(t *testing.T) {
|
|||||||
data, _ := os.ReadFile("../../../test_specs/stripe.yaml")
|
data, _ := os.ReadFile("../../../test_specs/stripe.yaml")
|
||||||
info, _ := datamodel.ExtractSpecInfo(data)
|
info, _ := datamodel.ExtractSpecInfo(data)
|
||||||
d, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
|
d, err := CreateDocumentFromConfig(info, &datamodel.DocumentConfiguration{})
|
||||||
assert.Len(t, utils.UnwrapErrors(err), 2)
|
assert.Len(t, utils.UnwrapErrors(err), 1)
|
||||||
|
|
||||||
assert.Equal(t, "3.0.0", d.Version.Value)
|
assert.Equal(t, "3.0.0", d.Version.Value)
|
||||||
assert.Equal(t, "Stripe API", d.Info.Value.Title.Value)
|
assert.Equal(t, "Stripe API", d.Info.Value.Title.Value)
|
||||||
|
|||||||
@@ -311,7 +311,7 @@ func TestDocument_ResolveStripe(t *testing.T) {
|
|||||||
rolo := model.Index.GetRolodex()
|
rolo := model.Index.GetRolodex()
|
||||||
rolo.Resolve()
|
rolo.Resolve()
|
||||||
|
|
||||||
assert.Equal(t, 2, len(model.Index.GetRolodex().GetCaughtErrors()))
|
assert.Equal(t, 1, len(model.Index.GetRolodex().GetCaughtErrors()))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDocument_RenderAndReload_ChangeCheck_Asana(t *testing.T) {
|
func TestDocument_RenderAndReload_ChangeCheck_Asana(t *testing.T) {
|
||||||
|
|||||||
@@ -198,7 +198,7 @@ func (resolver *Resolver) Resolve() []*ResolvingError {
|
|||||||
}
|
}
|
||||||
|
|
||||||
resolver.resolvingErrors = append(resolver.resolvingErrors, &ResolvingError{
|
resolver.resolvingErrors = append(resolver.resolvingErrors, &ResolvingError{
|
||||||
ErrorRef: fmt.Errorf("infinite circular reference detected: %s", circRef.Start.Name),
|
ErrorRef: fmt.Errorf("infinite circular reference detected: %s", circRef.Start.Definition),
|
||||||
Node: circRef.LoopPoint.Node,
|
Node: circRef.LoopPoint.Node,
|
||||||
Path: circRef.GenerateJourneyPath(),
|
Path: circRef.GenerateJourneyPath(),
|
||||||
})
|
})
|
||||||
@@ -354,7 +354,8 @@ func (resolver *Resolver) VisitReference(ref *Reference, seen map[string]bool, j
|
|||||||
} else {
|
} else {
|
||||||
resolver.circularReferences = append(resolver.circularReferences, circRef)
|
resolver.circularReferences = append(resolver.circularReferences, circRef)
|
||||||
}
|
}
|
||||||
|
r.Seen = true
|
||||||
|
r.Circular = true
|
||||||
foundDup.Seen = true
|
foundDup.Seen = true
|
||||||
foundDup.Circular = true
|
foundDup.Circular = true
|
||||||
}
|
}
|
||||||
@@ -429,30 +430,26 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
|
|||||||
}
|
}
|
||||||
|
|
||||||
// this is a safety check to prevent a stack overflow.
|
// this is a safety check to prevent a stack overflow.
|
||||||
if depth > 500 {
|
if depth > 100 {
|
||||||
def := "unknown"
|
def := "unknown"
|
||||||
if ref != nil {
|
if ref != nil {
|
||||||
def = ref.FullDefinition
|
def = ref.FullDefinition
|
||||||
}
|
}
|
||||||
if resolver.specIndex != nil && resolver.specIndex.logger != nil {
|
if resolver.specIndex != nil && resolver.specIndex.logger != nil {
|
||||||
resolver.specIndex.logger.Warn("libopenapi resolver: relative depth exceeded 500 levels, "+
|
resolver.specIndex.logger.Warn("libopenapi resolver: relative depth exceeded 100 levels, "+
|
||||||
"check for circular references - resolving may be incomplete",
|
"check for circular references - resolving may be incomplete",
|
||||||
"reference", def)
|
"reference", def)
|
||||||
}
|
}
|
||||||
|
|
||||||
loop := append(journey, ref)
|
loop := append(journey, ref)
|
||||||
circRef := &CircularReferenceResult{
|
circRef := &CircularReferenceResult{
|
||||||
Journey: loop,
|
Journey: loop,
|
||||||
Start: ref,
|
Start: ref,
|
||||||
LoopIndex: depth,
|
LoopIndex: depth,
|
||||||
LoopPoint: ref,
|
LoopPoint: ref,
|
||||||
|
IsInfiniteLoop: true,
|
||||||
}
|
}
|
||||||
resolver.circularReferences = append(resolver.circularReferences, circRef)
|
resolver.circularReferences = append(resolver.circularReferences, circRef)
|
||||||
resolver.resolvingErrors = append(resolver.resolvingErrors, &ResolvingError{
|
|
||||||
ErrorRef: fmt.Errorf("circular reference detected: %s", circRef.GenerateJourneyPath()),
|
|
||||||
Node: node,
|
|
||||||
Path: circRef.GenerateJourneyPath(),
|
|
||||||
})
|
|
||||||
ref.Circular = true
|
ref.Circular = true
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -464,7 +461,13 @@ func (resolver *Resolver) extractRelatives(ref *Reference, node, parent *yaml.No
|
|||||||
if utils.IsNodeMap(n) || utils.IsNodeArray(n) {
|
if utils.IsNodeMap(n) || utils.IsNodeArray(n) {
|
||||||
depth++
|
depth++
|
||||||
|
|
||||||
found = append(found, resolver.extractRelatives(ref, n, node, foundRelatives, journey, seen, resolve, depth)...)
|
foundRef, _ := resolver.specIndex.SearchIndexForReferenceByReference(ref)
|
||||||
|
if foundRef != nil && !foundRef.Circular {
|
||||||
|
found = append(found, resolver.extractRelatives(ref, n, node, foundRelatives, journey, seen, resolve, depth)...)
|
||||||
|
}
|
||||||
|
if foundRef == nil {
|
||||||
|
found = append(found, resolver.extractRelatives(ref, n, node, foundRelatives, journey, seen, resolve, depth)...)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -470,7 +470,7 @@ func TestResolver_DeepDepth(t *testing.T) {
|
|||||||
found := resolver.extractRelatives(ref, refA, nil, nil, nil, nil, false, 0)
|
found := resolver.extractRelatives(ref, refA, nil, nil, nil, nil, false, 0)
|
||||||
|
|
||||||
assert.Nil(t, found)
|
assert.Nil(t, found)
|
||||||
assert.Contains(t, buf.String(), "libopenapi resolver: relative depth exceeded 500 levels")
|
assert.Contains(t, buf.String(), "libopenapi resolver: relative depth exceeded 100 levels")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResolver_ResolveComponents_Stripe_NoRolodex(t *testing.T) {
|
func TestResolver_ResolveComponents_Stripe_NoRolodex(t *testing.T) {
|
||||||
@@ -492,7 +492,7 @@ func TestResolver_ResolveComponents_Stripe_NoRolodex(t *testing.T) {
|
|||||||
assert.NotNil(t, resolver)
|
assert.NotNil(t, resolver)
|
||||||
|
|
||||||
circ := resolver.CheckForCircularReferences()
|
circ := resolver.CheckForCircularReferences()
|
||||||
assert.Len(t, circ, 2)
|
assert.Len(t, circ, 1)
|
||||||
|
|
||||||
_, err := yaml.Marshal(resolver.resolvedRoot)
|
_, err := yaml.Marshal(resolver.resolvedRoot)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
@@ -521,9 +521,10 @@ func TestResolver_ResolveComponents_Stripe(t *testing.T) {
|
|||||||
// after resolving, the rolodex will have errors.
|
// after resolving, the rolodex will have errors.
|
||||||
rolo.Resolve()
|
rolo.Resolve()
|
||||||
|
|
||||||
assert.Len(t, rolo.GetCaughtErrors(), 2)
|
assert.Len(t, rolo.GetCaughtErrors(), 1)
|
||||||
assert.Len(t, rolo.GetRootIndex().GetResolver().GetNonPolymorphicCircularErrors(), 2)
|
assert.Len(t, rolo.GetRootIndex().GetResolver().GetNonPolymorphicCircularErrors(), 1)
|
||||||
assert.Len(t, rolo.GetRootIndex().GetResolver().GetPolymorphicCircularErrors(), 0)
|
assert.Len(t, rolo.GetRootIndex().GetResolver().GetPolymorphicCircularErrors(), 0)
|
||||||
|
assert.Len(t, rolo.GetRootIndex().GetResolver().GetSafeCircularReferences(), 25)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -769,9 +770,13 @@ func ExampleNewResolver() {
|
|||||||
// The Stripe API has a bunch of circular reference problems, mainly from polymorphism.
|
// The Stripe API has a bunch of circular reference problems, mainly from polymorphism.
|
||||||
// So let's print them out.
|
// So let's print them out.
|
||||||
//
|
//
|
||||||
fmt.Printf("There are %d circular reference errors, %d of them are polymorphic errors, %d are not",
|
fmt.Printf("There is %d circular reference error, %d of them are polymorphic errors, %d are not\n"+
|
||||||
len(circularErrors), len(resolver.GetPolymorphicCircularErrors()), len(resolver.GetNonPolymorphicCircularErrors()))
|
"with a total pf %d safe circular references.\n",
|
||||||
// Output: There are 2 circular reference errors, 0 of them are polymorphic errors, 2 are not
|
len(circularErrors), len(resolver.GetPolymorphicCircularErrors()), len(resolver.GetNonPolymorphicCircularErrors()),
|
||||||
|
len(resolver.GetSafeCircularReferences()))
|
||||||
|
// Output: There is 1 circular reference error, 0 of them are polymorphic errors, 1 are not
|
||||||
|
// with a total pf 25 safe circular references.
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleResolvingError() {
|
func ExampleResolvingError() {
|
||||||
|
|||||||
@@ -34,8 +34,10 @@ func (index *SpecIndex) SearchIndexForReferenceWithContext(ctx context.Context,
|
|||||||
|
|
||||||
func (index *SpecIndex) SearchIndexForReferenceByReferenceWithContext(ctx context.Context, searchRef *Reference) (*Reference, *SpecIndex, context.Context) {
|
func (index *SpecIndex) SearchIndexForReferenceByReferenceWithContext(ctx context.Context, searchRef *Reference) (*Reference, *SpecIndex, context.Context) {
|
||||||
|
|
||||||
if v, ok := index.cache.Load(searchRef.FullDefinition); ok {
|
if index.cache != nil {
|
||||||
return v.(*Reference), v.(*Reference).Index, context.WithValue(ctx, CurrentPathKey, v.(*Reference).RemoteLocation)
|
if v, ok := index.cache.Load(searchRef.FullDefinition); ok {
|
||||||
|
return v.(*Reference), v.(*Reference).Index, context.WithValue(ctx, CurrentPathKey, v.(*Reference).RemoteLocation)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ref := searchRef.FullDefinition
|
ref := searchRef.FullDefinition
|
||||||
@@ -105,13 +107,20 @@ func (index *SpecIndex) SearchIndexForReferenceByReferenceWithContext(ctx contex
|
|||||||
return r, r.Index, context.WithValue(ctx, CurrentPathKey, r.RemoteLocation)
|
return r, r.Index, context.WithValue(ctx, CurrentPathKey, r.RemoteLocation)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if r, ok := index.allComponentSchemaDefinitions[refAlt]; ok {
|
||||||
|
index.cache.Store(refAlt, r)
|
||||||
|
return r, r.Index, context.WithValue(ctx, CurrentPathKey, r.RemoteLocation)
|
||||||
|
}
|
||||||
|
|
||||||
// check the rolodex for the reference.
|
// check the rolodex for the reference.
|
||||||
if roloLookup != "" {
|
if roloLookup != "" {
|
||||||
|
|
||||||
if strings.Contains(roloLookup, "#") {
|
if strings.Contains(roloLookup, "#") {
|
||||||
roloLookup = strings.Split(roloLookup, "#")[0]
|
roloLookup = strings.Split(roloLookup, "#")[0]
|
||||||
}
|
}
|
||||||
|
if filepath.Base(roloLookup) == "root.yaml" {
|
||||||
|
return nil, index, ctx
|
||||||
|
}
|
||||||
rFile, err := index.rolodex.Open(roloLookup)
|
rFile, err := index.rolodex.Open(roloLookup)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, index, ctx
|
return nil, index, ctx
|
||||||
|
|||||||
Reference in New Issue
Block a user