From 4471a4039800014ef19965e6d2b3dc85cbeda37c Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Tue, 9 Apr 2024 21:06:36 +0200 Subject: [PATCH 1/2] Fix allOf with $ref property (#705) --- index.js | 2 +- test/allof.test.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 22e1d341..2507f10a 100644 --- a/index.js +++ b/index.js @@ -438,7 +438,7 @@ function cloneOriginSchema (context, schema, schemaId) { for (const key in schema) { let value = schema[key] - if (key === '$ref' && value.charAt(0) === '#') { + if (key === '$ref' && typeof value === 'string' && value.charAt(0) === '#') { value = schemaId + value } diff --git a/test/allof.test.js b/test/allof.test.js index 4c5e3ba5..5c80873d 100644 --- a/test/allof.test.js +++ b/test/allof.test.js @@ -717,3 +717,39 @@ test('external recursive allOfs', (t) => { const stringify = build(schema, { schema: { externalSchema } }) t.equal(stringify(data), '{"a":{"bar":"42","foo":{}},"b":{"bar":"42","foo":{}}}') }) + +test('do not crash with $ref prop', (t) => { + t.plan(1) + + const schema = { + title: 'object with $ref', + type: 'object', + properties: { + outside: { + $ref: '#/$defs/outside' + } + }, + $defs: { + inside: { + type: 'object', + properties: { + $ref: { + type: 'string' + } + } + }, + outside: { + allOf: [{ + $ref: '#/$defs/inside' + }] + } + } + } + const stringify = build(schema) + const value = stringify({ + outside: { + $ref: 'true' + } + }) + t.equal(value, '{"outside":{"$ref":"true"}}') +}) From e87268d1c5987abeaa3116039566fdf84c50096d Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Wed, 10 Apr 2024 10:32:59 +0200 Subject: [PATCH 2/2] Bumped v8.14.1 Signed-off-by: Matteo Collina --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 27c7d017..a5fd6a16 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fast-json-stringify", - "version": "5.14.0", + "version": "5.14.1", "description": "Stringify your JSON at max speed", "main": "index.js", "type": "commonjs",