-
-
Notifications
You must be signed in to change notification settings - Fork 208
Closed
Labels
good first issueGood for newcomersGood for newcomerssemver-minorIssue or PR that should land as semver minorIssue or PR that should land as semver minor
Description
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure it has not already been reported
Fastify version
3.20.2
Plugin version
No response
Node.js version
v14.16.0
Operating system
Linux
Operating system version (i.e. 20.04, 11.3, 10)
Ubuntu 20.04.2
Description
refFinder fails to find a $ref by $id (docs), when the schema is nested, e.g.:
{
"title": "Example Schema",
"type": "object",
"properties": {
"nested": {
"type": "object",
"properties": {
"firstName": {
"$ref": "#foo"
},
"lastName": {
"$ref": "#foo"
}
}
}
},
"definitions": {
"foo": {
"$id": "#foo",
"type": "string"
}
}
}There are actually two errors (source):
- if there is no
externalSchema, you'll getTypeError: Cannot convert undefined or null to objectwhen executingfor (var key of Object.keys(externalSchema)) { - if
externalSchemais specified,refFinderwill fail to dereference$refand a property will end up resolved toundefinedwhich will triggerTypeError: Cannot read property 'type' of undefinedon the later stages inbuildCode
Looks like refFinder should look up for definitions starting from root instead of schema in the same way as it done for processing of #/definitions/foo refs.
Steps to Reproduce
const fastJson = require('fast-json-stringify');
const stringify = fastJson({
title: 'Example Schema',
type: 'object',
properties: {
firstName: { $ref: '#foo' },
lastName: { $ref: '#foo' },
nested: {
type: 'object',
properties: {
firstName: { $ref: '#foo' },
lastName: { $ref: '#foo' },
},
},
},
definitions: {
foo: {
$id: '#foo',
type: 'string',
},
},
});
console.log(
stringify({
firstName: 'Matteo',
lastName: 'Collina',
nested: {
firstName: 'Matteo',
lastName: 'Collina',
},
}),
);Expected Behavior
The nested $ref should be resolved. Here is an example of schema validation: https://www.jsonschemavalidator.net/s/sMCUkqfP
Relequestual
Metadata
Metadata
Assignees
Labels
good first issueGood for newcomersGood for newcomerssemver-minorIssue or PR that should land as semver minorIssue or PR that should land as semver minor