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

refactor: remove array items schema cloning #518

Merged
merged 1 commit into from
Aug 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 15 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

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

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

function buildArray (location) {
let schema = location.schema

// default to any items type
if (!schema.items) {
schema.items = {}
}
const schema = location.schema

let itemsLocation = mergeLocation(location, 'items')
itemsLocation.schema = itemsLocation.schema || {}

if (schema.items.$ref) {
if (!schema[fjsCloned]) {
location.schema = clone(location.schema)
schema = location.schema
schema[fjsCloned] = true
}

location = resolveRef(location, schema.items.$ref)
itemsLocation = location
schema.items = location.schema
if (itemsLocation.schema.$ref) {
itemsLocation = resolveRef(itemsLocation, itemsLocation.schema.$ref)
}

if (arrayItemsReferenceSerializersMap.has(schema.items)) {
return arrayItemsReferenceSerializersMap.get(schema.items)
const itemsSchema = itemsLocation.schema

if (arrayItemsReferenceSerializersMap.has(itemsSchema)) {
return arrayItemsReferenceSerializersMap.get(itemsSchema)
}

const functionName = generateFuncName()
arrayItemsReferenceSerializersMap.set(schema.items, functionName)
arrayItemsReferenceSerializersMap.set(itemsSchema, functionName)

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

if (!schema.additionalItems) {
functionCode += `
if (arrayLength > ${schema.items.length}) {
throw new Error(\`Item at ${schema.items.length} does not match schema definition.\`)
if (arrayLength > ${itemsSchema.length}) {
throw new Error(\`Item at ${itemsSchema.length} does not match schema definition.\`)
}
`
}
Expand All @@ -650,9 +639,9 @@ function buildArray (location) {
let jsonOutput = ''
`

if (Array.isArray(schema.items)) {
for (let i = 0; i < schema.items.length; i++) {
const item = schema.items[i]
if (Array.isArray(itemsSchema)) {
for (let i = 0; i < itemsSchema.length; i++) {
const item = itemsSchema[i]
const tmpRes = buildValue(mergeLocation(itemsLocation, i), `obj[${i}]`)
functionCode += `
if (${i} < arrayLength) {
Expand All @@ -672,7 +661,7 @@ function buildArray (location) {

if (schema.additionalItems) {
functionCode += `
for (let i = ${schema.items.length}; i < arrayLength; i++) {
for (let i = ${itemsSchema.length}; i < arrayLength; i++) {
let json = JSON.stringify(obj[i])
jsonOutput += json
if (i < arrayLength - 1) {
Expand Down