Skip to content

Commit 14699ca

Browse files
fix: Prevent buildObject to create recursive call (#387)
* Fixed lint issues and just have intended changes * Moved sinclair Typebox to Devdependencies
1 parent ac63f60 commit 14699ca

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ function buildObject (location, code, name) {
909909
`
910910
}
911911

912-
if (objectReferenceSerializersMap.has(schema)) {
912+
if (objectReferenceSerializersMap.has(schema) && objectReferenceSerializersMap.get(schema) !== name) {
913913
code += `
914914
return ${objectReferenceSerializersMap.get(schema)}(input)
915915
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
},
3131
"homepage": "https://github.com/fastify/fast-json-stringify#readme",
3232
"devDependencies": {
33+
"@sinclair/typebox": "^0.23.3",
3334
"benchmark": "^2.1.4",
3435
"compile-json-stringify": "^0.1.2",
3536
"is-my-json-valid": "^2.20.0",

test/typebox.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict'
2+
3+
const test = require('tap').test
4+
const build = require('..')
5+
6+
test('nested object in pattern properties for typebox', (t) => {
7+
const { Type } = require('@sinclair/typebox')
8+
9+
t.plan(1)
10+
11+
const nestedSchema = Type.Object({
12+
nestedKey1: Type.String()
13+
})
14+
15+
const RootSchema = Type.Object({
16+
key1: Type.Record(Type.String(), nestedSchema),
17+
key2: Type.Record(Type.String(), nestedSchema)
18+
})
19+
20+
const schema = RootSchema
21+
const stringify = build(schema)
22+
23+
const value = stringify({
24+
key1: {
25+
nestedKey: {
26+
nestedKey1: 'value1'
27+
}
28+
},
29+
key2: {
30+
nestedKey: {
31+
nestedKey1: 'value2'
32+
}
33+
}
34+
})
35+
t.equal(value, '{"key1":{"nestedKey":{"nestedKey1":"value1"}},"key2":{"nestedKey":{"nestedKey1":"value2"}}}')
36+
})

0 commit comments

Comments
 (0)