@@ -83,12 +83,12 @@ class Pointer {
8383 this . value = unwrapOrThrow ( obj ) ;
8484
8585 for ( let i = 0 ; i < tokens . length ; i ++ ) {
86- if ( resolveIf$Ref ( this , options ) ) {
86+ if ( resolveIf$Ref ( this , options , pathFromRoot ) ) {
8787 // The $ref path has changed, so append the remaining tokens to the path
8888 this . path = Pointer . join ( this . path , tokens . slice ( i ) ) ;
8989 }
9090
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 ) {
9292 return this ;
9393 }
9494
@@ -103,7 +103,7 @@ class Pointer {
103103
104104 // Resolve the final value
105105 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 ) ;
107107 }
108108
109109 return this ;
@@ -224,15 +224,16 @@ class Pointer {
224224 *
225225 * @param pointer
226226 * @param options
227+ * @param [pathFromRoot] - the path of place that initiated resolving
227228 * @returns - Returns `true` if the resolution path changed
228229 */
229- function resolveIf$Ref ( pointer : any , options : any ) {
230+ function resolveIf$Ref ( pointer : any , options : any , pathFromRoot ?: any ) {
230231 // Is the value a JSON reference? (and allowed?)
231232
232233 if ( $Ref . isAllowed$Ref ( pointer . value , options ) ) {
233234 const $refPath = url . resolve ( pointer . path , pointer . value . $ref ) ;
234235
235- if ( $refPath === pointer . path ) {
236+ if ( $refPath === pointer . path && ! isRootPath ( pathFromRoot ) ) {
236237 // The value is a reference to itself, so there's nothing to do.
237238 pointer . circular = true ;
238239 } else {
@@ -294,3 +295,7 @@ function unwrapOrThrow(value: any) {
294295
295296 return value ;
296297}
298+
299+ function isRootPath ( pathFromRoot : any ) : boolean {
300+ return typeof pathFromRoot == "string" && Pointer . parse ( pathFromRoot ) . length == 0 ;
301+ }
0 commit comments