Skip to content

Commit 3c4e084

Browse files
refactor: remove array items schema cloning (#518)
1 parent 7d68fb2 commit 3c4e084

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

index.js

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
const merge = require('@fastify/deepmerge')()
66
const clone = require('rfdc')({ proto: true })
7-
const fjsCloned = Symbol('fast-json-stringify.cloned')
87
const { randomUUID } = require('crypto')
98

109
const validate = require('./schema-validator')
@@ -581,33 +580,23 @@ function buildObject (location) {
581580
}
582581

583582
function buildArray (location) {
584-
let schema = location.schema
585-
586-
// default to any items type
587-
if (!schema.items) {
588-
schema.items = {}
589-
}
583+
const schema = location.schema
590584

591585
let itemsLocation = mergeLocation(location, 'items')
586+
itemsLocation.schema = itemsLocation.schema || {}
592587

593-
if (schema.items.$ref) {
594-
if (!schema[fjsCloned]) {
595-
location.schema = clone(location.schema)
596-
schema = location.schema
597-
schema[fjsCloned] = true
598-
}
599-
600-
location = resolveRef(location, schema.items.$ref)
601-
itemsLocation = location
602-
schema.items = location.schema
588+
if (itemsLocation.schema.$ref) {
589+
itemsLocation = resolveRef(itemsLocation, itemsLocation.schema.$ref)
603590
}
604591

605-
if (arrayItemsReferenceSerializersMap.has(schema.items)) {
606-
return arrayItemsReferenceSerializersMap.get(schema.items)
592+
const itemsSchema = itemsLocation.schema
593+
594+
if (arrayItemsReferenceSerializersMap.has(itemsSchema)) {
595+
return arrayItemsReferenceSerializersMap.get(itemsSchema)
607596
}
608597

609598
const functionName = generateFuncName()
610-
arrayItemsReferenceSerializersMap.set(schema.items, functionName)
599+
arrayItemsReferenceSerializersMap.set(itemsSchema, functionName)
611600

612601
const schemaId = location.schemaId === rootSchemaId ? '' : location.schemaId
613602
let functionCode = `
@@ -632,8 +621,8 @@ function buildArray (location) {
632621

633622
if (!schema.additionalItems) {
634623
functionCode += `
635-
if (arrayLength > ${schema.items.length}) {
636-
throw new Error(\`Item at ${schema.items.length} does not match schema definition.\`)
624+
if (arrayLength > ${itemsSchema.length}) {
625+
throw new Error(\`Item at ${itemsSchema.length} does not match schema definition.\`)
637626
}
638627
`
639628
}
@@ -650,9 +639,9 @@ function buildArray (location) {
650639
let jsonOutput = ''
651640
`
652641

653-
if (Array.isArray(schema.items)) {
654-
for (let i = 0; i < schema.items.length; i++) {
655-
const item = schema.items[i]
642+
if (Array.isArray(itemsSchema)) {
643+
for (let i = 0; i < itemsSchema.length; i++) {
644+
const item = itemsSchema[i]
656645
const tmpRes = buildValue(mergeLocation(itemsLocation, i), `obj[${i}]`)
657646
functionCode += `
658647
if (${i} < arrayLength) {
@@ -672,7 +661,7 @@ function buildArray (location) {
672661

673662
if (schema.additionalItems) {
674663
functionCode += `
675-
for (let i = ${schema.items.length}; i < arrayLength; i++) {
664+
for (let i = ${itemsSchema.length}; i < arrayLength; i++) {
676665
let json = JSON.stringify(obj[i])
677666
jsonOutput += json
678667
if (i < arrayLength - 1) {

0 commit comments

Comments
 (0)