@@ -83,12 +83,12 @@ class Pointer {
83
83
this . value = unwrapOrThrow ( obj ) ;
84
84
85
85
for ( let i = 0 ; i < tokens . length ; i ++ ) {
86
- if ( resolveIf$Ref ( this , options ) ) {
86
+ if ( resolveIf$Ref ( this , options , pathFromRoot ) ) {
87
87
// The $ref path has changed, so append the remaining tokens to the path
88
88
this . path = Pointer . join ( this . path , tokens . slice ( i ) ) ;
89
89
}
90
90
91
- if ( typeof this . value === "object" && this . value !== null && "$ref" in this . value ) {
91
+ if ( typeof this . value === "object" && this . value !== null && ! isRootPath ( pathFromRoot ) && "$ref" in this . value ) {
92
92
return this ;
93
93
}
94
94
@@ -103,7 +103,7 @@ class Pointer {
103
103
104
104
// Resolve the final value
105
105
if ( ! this . value || ( this . value . $ref && url . resolve ( this . path , this . value . $ref ) !== pathFromRoot ) ) {
106
- resolveIf$Ref ( this , options ) ;
106
+ resolveIf$Ref ( this , options , pathFromRoot ) ;
107
107
}
108
108
109
109
return this ;
@@ -224,15 +224,16 @@ class Pointer {
224
224
*
225
225
* @param pointer
226
226
* @param options
227
+ * @param [pathFromRoot] - the path of place that initiated resolving
227
228
* @returns - Returns `true` if the resolution path changed
228
229
*/
229
- function resolveIf$Ref ( pointer : any , options : any ) {
230
+ function resolveIf$Ref ( pointer : any , options : any , pathFromRoot ?: any ) {
230
231
// Is the value a JSON reference? (and allowed?)
231
232
232
233
if ( $Ref . isAllowed$Ref ( pointer . value , options ) ) {
233
234
const $refPath = url . resolve ( pointer . path , pointer . value . $ref ) ;
234
235
235
- if ( $refPath === pointer . path ) {
236
+ if ( $refPath === pointer . path && ! isRootPath ( pathFromRoot ) ) {
236
237
// The value is a reference to itself, so there's nothing to do.
237
238
pointer . circular = true ;
238
239
} else {
@@ -294,3 +295,7 @@ function unwrapOrThrow(value: any) {
294
295
295
296
return value ;
296
297
}
298
+
299
+ function isRootPath ( pathFromRoot : any ) : boolean {
300
+ return typeof pathFromRoot == "string" && Pointer . parse ( pathFromRoot ) . length == 0 ;
301
+ }
0 commit comments