@@ -44,17 +44,20 @@ function resolveExternal (parser, options) {
44
44
* @param {string } path - The full path of `obj`, possibly with a JSON Pointer in the hash
45
45
* @param {$Refs } $refs
46
46
* @param {$RefParserOptions } options
47
+ * @param {Set } seen - Internal.
47
48
*
48
49
* @returns {Promise[] }
49
50
* Returns an array of promises. There will be one promise for each JSON reference in `obj`.
50
51
* If `obj` does not contain any JSON references, then the array will be empty.
51
52
* If any of the JSON references point to files that contain additional JSON references,
52
53
* then the corresponding promise will internally reference an array of promises.
53
54
*/
54
- function crawl ( obj , path , $refs , options ) {
55
+ function crawl ( obj , path , $refs , options , seen ) {
56
+ seen = seen || new Set ( ) ;
55
57
let promises = [ ] ;
56
58
57
- if ( obj && typeof obj === "object" && ! ArrayBuffer . isView ( obj ) ) {
59
+ if ( obj && typeof obj === "object" && ! ArrayBuffer . isView ( obj ) && ! seen . has ( obj ) ) {
60
+ seen . add ( obj ) ; // Track previously seen objects to avoid infinite recursion
58
61
if ( $Ref . isExternal$Ref ( obj ) ) {
59
62
promises . push ( resolve$Ref ( obj , path , $refs , options ) ) ;
60
63
}
@@ -67,7 +70,7 @@ function crawl (obj, path, $refs, options) {
67
70
promises . push ( resolve$Ref ( value , keyPath , $refs , options ) ) ;
68
71
}
69
72
else {
70
- promises = promises . concat ( crawl ( value , keyPath , $refs , options ) ) ;
73
+ promises = promises . concat ( crawl ( value , keyPath , $refs , options , seen ) ) ;
71
74
}
72
75
}
73
76
}
0 commit comments