Skip to content

Commit 8949868

Browse files
Fixed a bug that failed to detect certain circular references
1 parent 3059edf commit 8949868

File tree

8 files changed

+35
-18
lines changed

8 files changed

+35
-18
lines changed

dist/ref-parser.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ function crawl(obj, path, parents, $refs, options) {
149149
Object.keys(obj).forEach(function(key) {
150150
var keyPath = Pointer.join(path, key);
151151
var value = obj[key];
152+
var circular = false;
152153

153154
if ($Ref.isAllowed$Ref(value, options)) {
154155
// We found a $ref, so resolve it
@@ -157,8 +158,8 @@ function crawl(obj, path, parents, $refs, options) {
157158
var pointer = $refs._resolve($refPath, options);
158159

159160
// Check for circular references
160-
var circular = pointer.circular || parents.indexOf(pointer.value) !== -1;
161-
circular && (isCircular = foundCircularReference(keyPath, $refs, options));
161+
circular = pointer.circular || parents.indexOf(pointer.value) !== -1;
162+
circular && foundCircularReference(keyPath, $refs, options);
162163

163164
// Dereference the JSON reference
164165
var dereferencedValue = getDereferencedValue(value, pointer.value);
@@ -167,7 +168,6 @@ function crawl(obj, path, parents, $refs, options) {
167168
if (!circular) {
168169
// If the `crawl` method returns true, then dereferenced value is circular
169170
circular = crawl(dereferencedValue, pointer.path, parents, $refs, options);
170-
isCircular = isCircular || circular;
171171
}
172172

173173
// Replace the JSON reference with the dereferenced value
@@ -177,12 +177,15 @@ function crawl(obj, path, parents, $refs, options) {
177177
}
178178
else {
179179
if (parents.indexOf(value) === -1) {
180-
isCircular = crawl(value, keyPath, parents, $refs, options);
180+
circular = crawl(value, keyPath, parents, $refs, options);
181181
}
182182
else {
183-
isCircular = foundCircularReference(keyPath, $refs, options);
183+
circular = foundCircularReference(keyPath, $refs, options);
184184
}
185185
}
186+
187+
// Set the "isCircular" flag if this or any other property is circular
188+
isCircular = isCircular || circular;
186189
});
187190

188191
parents.pop();

dist/ref-parser.js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ref-parser.min.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ref-parser.min.js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/dereference.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ function crawl(obj, path, parents, $refs, options) {
4040
Object.keys(obj).forEach(function(key) {
4141
var keyPath = Pointer.join(path, key);
4242
var value = obj[key];
43+
var circular = false;
4344

4445
if ($Ref.isAllowed$Ref(value, options)) {
4546
// We found a $ref, so resolve it
@@ -48,8 +49,8 @@ function crawl(obj, path, parents, $refs, options) {
4849
var pointer = $refs._resolve($refPath, options);
4950

5051
// Check for circular references
51-
var circular = pointer.circular || parents.indexOf(pointer.value) !== -1;
52-
circular && (isCircular = foundCircularReference(keyPath, $refs, options));
52+
circular = pointer.circular || parents.indexOf(pointer.value) !== -1;
53+
circular && foundCircularReference(keyPath, $refs, options);
5354

5455
// Dereference the JSON reference
5556
var dereferencedValue = getDereferencedValue(value, pointer.value);
@@ -58,7 +59,6 @@ function crawl(obj, path, parents, $refs, options) {
5859
if (!circular) {
5960
// If the `crawl` method returns true, then dereferenced value is circular
6061
circular = crawl(dereferencedValue, pointer.path, parents, $refs, options);
61-
isCircular = isCircular || circular;
6262
}
6363

6464
// Replace the JSON reference with the dereferenced value
@@ -68,12 +68,15 @@ function crawl(obj, path, parents, $refs, options) {
6868
}
6969
else {
7070
if (parents.indexOf(value) === -1) {
71-
isCircular = crawl(value, keyPath, parents, $refs, options);
71+
circular = crawl(value, keyPath, parents, $refs, options);
7272
}
7373
else {
74-
isCircular = foundCircularReference(keyPath, $refs, options);
74+
circular = foundCircularReference(keyPath, $refs, options);
7575
}
7676
}
77+
78+
// Set the "isCircular" flag if this or any other property is circular
79+
isCircular = isCircular || circular;
7780
});
7881

7982
parents.pop();

tests/specs/circular/circular-ancestor.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ definitions:
88
$ref: '#/definitions/pet'
99
spouse:
1010
$ref: "#/definitions/person" # <--- circular reference to ancestor
11+
age:
12+
type: number
1113

1214
pet:
1315
title: pet

tests/specs/circular/circular.dereferenced.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ helper.dereferenced.circular =
6969
"pet": null,
7070
"name": {
7171
"type": "string"
72+
},
73+
"age": {
74+
"type": "number"
7275
}
7376
}
7477
},
@@ -107,6 +110,9 @@ helper.dereferenced.circular =
107110
"pet": null,
108111
"name": {
109112
"type": "string"
113+
},
114+
"age": {
115+
"type": "number"
110116
}
111117
}
112118
},

tests/specs/circular/circular.parsed.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ helper.parsed.circular =
5454
},
5555
"name": {
5656
"type": "string"
57+
},
58+
"age": {
59+
"type": "number"
5760
}
5861
}
5962
},

0 commit comments

Comments
 (0)