Skip to content
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

allOf withing $ref within allOf is ignored #368

Closed
2 tasks done
LEVI-RIVKIN opened this issue Nov 22, 2021 · 1 comment
Closed
2 tasks done

allOf withing $ref within allOf is ignored #368

LEVI-RIVKIN opened this issue Nov 22, 2021 · 1 comment

Comments

@LEVI-RIVKIN
Copy link
Contributor

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

3.24.0

Plugin version

No response

Node.js version

14.17.3

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

10

Description

When trying to use an allOf within a $ref which is itself within an allOf, the inner allOf is simply ignored.

Steps to Reproduce

const fastJson = require('fast-json-stringify');

const externalSchema = {
	nameSchema: {
		$id: 'nameSchema',
		type: 'object',
		properties: {
			name: {
				type: 'string'
			}
		}
	},
	user: {
		$id: 'user',
		type: 'object',
		allOf: [{
			$ref: 'nameSchema#'
		}, {
			properties: {
				id: {
					type: 'number'
				}
			}
		}]
	},
	fullUser: {
		$id: 'fullUser',
		type: 'object',
		allOf: [{
			$ref: 'user#'
		}, {
			properties: {
				age: {
					type: 'number'
				}
			}
		}]
	}
};

const user = { id: 1, age: 5, name: 'Test' };

// using the 'user' schema it's working as expected
const userStringify = fastJson({
	title: 'User Schema',
	$ref: 'user#'
}, { schema: externalSchema });
console.log('userStringify', userStringify(user));
// prints: `userStringify {"name":"Test","id":1}`

// using the 'fullUser' schema it ignores all the 'user' properties
const fullUserStringify = fastJson({
	title: 'Full User Schema',
	$ref: 'fullUser#'
}, { schema: externalSchema });
console.log('fullUserStringify', fullUserStringify(user));
// prints: `fullUserStringify {"age":5}`

I've traced the issue and it seems to be in the buildCodeWithAllOfs function. There, in case the inner allOf is immediate, it loops, otherwise it calls buildCode. The buildCode function then unpacks the $ref and handles it, but it doesn't take into account it could include another allOf inside.
Adding the following lines at the end of the buildCode function seems to solve it:

if (schema.allOf) {
	const builtCode = buildCodeWithAllOfs(location, code, laterCode, name);
	code += builtCode.code;
	laterCode += builtCode.laterCode;
}

Expected Behavior

Should not ignore the inner allOf.

@mcollina
Copy link
Member

Good finding!
Would you like to send a Pull Request to address this issue? Remember to add unit tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants