Skip to content

Commit

Permalink
Merge pull request #53 from csmig/very-deeply-nested
Browse files Browse the repository at this point in the history
handle $ref nested more than two levels
  • Loading branch information
w3nl authored Sep 13, 2024
2 parents 56d93f6 + 91037a7 commit 028e003
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/dereference.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ export const dereferenceSync = (schema) => {
} else {
// object
if ('$ref' in current && typeof current.$ref === 'string') {
return resolveRefSync(cloned, current.$ref)
let ref = current
do {
ref = resolveRefSync(cloned, ref.$ref)
} while (ref?.$ref)
return ref
}

for (const key in current) {
Expand Down
14 changes: 13 additions & 1 deletion src/dereference.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,15 @@ test('dereferenceSync', async (t) => {
}
},
FirstName: {
type: 'string'
$ref: '#/schemas/NameComponent'
},
LastName: {
$ref: '#/schemas/NameComponent'
},
NameComponent: {
$ref: '#/schemas/StringType'
},
StringType: {
type: 'string'
}
}
Expand Down Expand Up @@ -136,6 +142,12 @@ test('dereferenceSync', async (t) => {
},
LastName: {
type: 'string'
},
NameComponent: {
type: 'string'
},
StringType: {
type: 'string'
}
}
})
Expand Down

0 comments on commit 028e003

Please sign in to comment.