@@ -16,7 +16,7 @@ module.exports = dereference;
16
16
*/
17
17
function dereference ( parser , options ) {
18
18
// console.log('Dereferencing $ref pointers in %s', parser.$refs._root$Ref.path);
19
- let dereferenced = crawl ( parser . schema , parser . $refs . _root$Ref . path , "#" , [ ] , [ ] , { } , parser . $refs , options ) ;
19
+ let dereferenced = crawl ( parser . schema , parser . $refs . _root$Ref . path , "#" , new Set ( ) , new Set ( ) , new Map ( ) , parser . $refs , options ) ;
20
20
parser . $refs . circular = dereferenced . circular ;
21
21
parser . schema = dereferenced . value ;
22
22
}
@@ -27,9 +27,9 @@ function dereference (parser, options) {
27
27
* @param {* } obj - The value to crawl. If it's not an object or array, it will be ignored.
28
28
* @param {string } path - The full path of `obj`, possibly with a JSON Pointer in the hash
29
29
* @param {string } pathFromRoot - The path of `obj` from the schema root
30
- * @param {object[] } parents - An array of the parent objects that have already been dereferenced
31
- * @param {object[] } processedObjects - An array of all the objects that have already been processed
32
- * @param {object } dereferencedCache - An map of all the dereferenced objects
30
+ * @param {Set< object> } parents - An array of the parent objects that have already been dereferenced
31
+ * @param {Set< object> } processedObjects - An array of all the objects that have already been processed
32
+ * @param {Map<string, object> } dereferencedCache - An map of all the dereferenced objects
33
33
* @param {$Refs } $refs
34
34
* @param {$RefParserOptions } options
35
35
* @returns {{value: object, circular: boolean} }
@@ -41,18 +41,18 @@ function crawl (obj, path, pathFromRoot, parents, processedObjects, dereferenced
41
41
circular : false
42
42
} ;
43
43
44
- if ( options . dereference . circular === "ignore" || processedObjects . indexOf ( obj ) === - 1 ) {
44
+ if ( options . dereference . circular === "ignore" || ! processedObjects . has ( obj ) ) {
45
45
if ( obj && typeof obj === "object" && ! ArrayBuffer . isView ( obj ) ) {
46
- parents . push ( obj ) ;
47
- processedObjects . push ( obj ) ;
46
+ parents . add ( obj ) ;
47
+ processedObjects . add ( obj ) ;
48
48
49
49
if ( $Ref . isAllowed$Ref ( obj , options ) ) {
50
50
dereferenced = dereference$Ref ( obj , path , pathFromRoot , parents , processedObjects , dereferencedCache , $refs , options ) ;
51
51
result . circular = dereferenced . circular ;
52
52
result . value = dereferenced . value ;
53
53
}
54
54
else {
55
- for ( let key of Object . keys ( obj ) ) {
55
+ for ( const key of Object . keys ( obj ) ) {
56
56
let keyPath = Pointer . join ( path , key ) ;
57
57
let keyPathFromRoot = Pointer . join ( pathFromRoot , key ) ;
58
58
let value = obj [ key ] ;
@@ -67,7 +67,7 @@ function crawl (obj, path, pathFromRoot, parents, processedObjects, dereferenced
67
67
}
68
68
}
69
69
else {
70
- if ( parents . indexOf ( value ) === - 1 ) {
70
+ if ( ! parents . has ( value ) ) {
71
71
dereferenced = crawl ( value , keyPath , keyPathFromRoot , parents , processedObjects , dereferencedCache , $refs , options ) ;
72
72
circular = dereferenced . circular ;
73
73
// Avoid pointless mutations; breaks frozen objects to no profit
@@ -85,7 +85,7 @@ function crawl (obj, path, pathFromRoot, parents, processedObjects, dereferenced
85
85
}
86
86
}
87
87
88
- parents . pop ( ) ;
88
+ parents . delete ( obj ) ;
89
89
}
90
90
}
91
91
@@ -98,9 +98,9 @@ function crawl (obj, path, pathFromRoot, parents, processedObjects, dereferenced
98
98
* @param {{$ref: string} } $ref - The JSON Reference to resolve
99
99
* @param {string } path - The full path of `$ref`, possibly with a JSON Pointer in the hash
100
100
* @param {string } pathFromRoot - The path of `$ref` from the schema root
101
- * @param {object[] } parents - An array of the parent objects that have already been dereferenced
102
- * @param {object[] } processedObjects - An array of all the objects that have already been dereferenced
103
- * @param {object } dereferencedCache - An map of all the dereferenced objects
101
+ * @param {Set< object> } parents - An array of the parent objects that have already been dereferenced
102
+ * @param {Set< object> } processedObjects - An array of all the objects that have already been dereferenced
103
+ * @param {Map<string, object> } dereferencedCache - An map of all the dereferenced objects
104
104
* @param {$Refs } $refs
105
105
* @param {$RefParserOptions } options
106
106
* @returns {{value: object, circular: boolean} }
@@ -110,9 +110,8 @@ function dereference$Ref ($ref, path, pathFromRoot, parents, processedObjects, d
110
110
111
111
let $refPath = url . resolve ( path , $ref . $ref ) ;
112
112
113
- if ( dereferencedCache [ $refPath ] ) {
114
- const cache = dereferencedCache [ $refPath ] ;
115
-
113
+ const cache = dereferencedCache . get ( $refPath ) ;
114
+ if ( cache ) {
116
115
const refKeys = Object . keys ( $ref ) ;
117
116
if ( refKeys . length > 1 ) {
118
117
const extraKeys = { } ;
@@ -142,7 +141,7 @@ function dereference$Ref ($ref, path, pathFromRoot, parents, processedObjects, d
142
141
143
142
// Check for circular references
144
143
let directCircular = pointer . circular ;
145
- let circular = directCircular || parents . indexOf ( pointer . value ) !== - 1 ;
144
+ let circular = directCircular || parents . has ( pointer . value ) ;
146
145
circular && foundCircularReference ( path , $refs , options ) ;
147
146
148
147
// Dereference the JSON reference
@@ -175,7 +174,7 @@ function dereference$Ref ($ref, path, pathFromRoot, parents, processedObjects, d
175
174
176
175
// only cache if no extra properties than $ref
177
176
if ( Object . keys ( $ref ) . length === 1 ) {
178
- dereferencedCache [ $refPath ] = dereferencedObject ;
177
+ dereferencedCache . set ( $refPath , dereferencedObject ) ;
179
178
}
180
179
181
180
return dereferencedObject ;
0 commit comments