Skip to content

Commit

Permalink
openapi3: shorten visited path reported on CircularReferenceCounter
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Fenoll <pierrefenoll@gmail.com>
  • Loading branch information
fenollp committed Nov 25, 2023
1 parent a53cd59 commit 187d909
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions openapi3/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -749,9 +749,9 @@ func (loader *Loader) resolveSchemaRef(doc *T, component *SchemaRef, documentPat
}
component.Value = &schema
} else {
if visitedLimit(visited, ref) {
if visitedLimit(&visited, ref) {
visited = append(visited, ref)
return fmt.Errorf("%s with length %d - %s", CircularReferenceError, len(visited), strings.Join(visited, " -> "))
return fmt.Errorf("%s with length %d - %s", CircularReferenceError, len(visited)-2, strings.Join(visited, " -> "))
}
visited = append(visited, ref)

Expand Down Expand Up @@ -1039,15 +1039,34 @@ func unescapeRefString(ref string) string {
return strings.Replace(strings.Replace(ref, "~1", "/", -1), "~0", "~", -1)
}

func visitedLimit(visited []string, ref string) bool {
func visitedLimit(visited *[]string, ref string) bool {
visitedCount := 0
for _, v := range visited {
for _, v := range *visited {
if v == ref {
visitedCount++
if visitedCount >= CircularReferenceCounter {
*visited = dedupe(*visited)
if visitedCount < CircularReferenceCounter {
break
}
return true
}
}
}
return false
}

func dedupe(xs []string) []string {
m := make(map[string]struct{}, len(xs))
for _, x := range xs {
m[x] = struct{}{}
}
ys := make([]string, 0, len(m))
for _, x := range xs {
if _, ok := m[x]; ok {
ys = append(ys, x)
}
delete(m, x)
}
return ys
}

0 comments on commit 187d909

Please sign in to comment.