Skip to content

fix: do not resolve meta refs #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions ref-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ function jsonSchemaResolver (options) {
* @param {URI} baseUri
* @param {*} json
*/
function mapIds (ee, baseUri, json) {
function mapIds (ee, baseUri, json, parentKey) {
if (!(json instanceof Object)) return

if (json.$id) {
Expand Down Expand Up @@ -218,10 +218,10 @@ function mapIds (ee, baseUri, json) {

const fields = Object.keys(json)
for (const prop of fields) {
if (prop === '$ref') {
if (prop === '$ref' && (typeof json.$ref === 'string') && parentKey !== 'properties') {
ee.emit('$ref', json, baseUri, json[prop])
}
mapIds(ee, baseUri, json[prop])
mapIds(ee, baseUri, json[prop], parentKey)
}
}

Expand Down
9 changes: 9 additions & 0 deletions test/ref-resolver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,12 @@ test('absolute $ref #2', t => {
t.equal(out.properties.address.$ref, '#/definitions/def-0')
t.equal(out.properties.houses.items.$ref, '#/definitions/def-0')
})

test('do not resolve meta refs', t => {
t.plan(1)
const schema = factory('metaRef')

const resolver = RefResolver({ clone: true })
const out = resolver.resolve(schema, {})
t.same(out.properties.$ref, { type: 'string' })
})
7 changes: 7 additions & 0 deletions test/schemas/metaRef.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
$id: 'schemaWithMetaRef',
type: 'object',
properties: {
$ref: { type: 'string' }
}
}