Skip to content

Commit

Permalink
fix: set array function name by array schema (#528)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-tymoshenko committed Sep 9, 2022
1 parent 26ef3cd commit 3539f2d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
21 changes: 9 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,15 @@ function resolveRef (location, ref) {
return { schema, schemaId, jsonPointer }
}

const arrayItemsReferenceSerializersMap = new Map()
const objectReferenceSerializersMap = new Map()
const contextFunctionsNamesBySchema = new Map()

let rootSchemaId = null
let refResolver = null
let validator = null
let contextFunctions = null

function build (schema, options) {
arrayItemsReferenceSerializersMap.clear()
objectReferenceSerializersMap.clear()
contextFunctionsNamesBySchema.clear()

contextFunctions = []
options = options || {}
Expand Down Expand Up @@ -168,8 +166,7 @@ function build (schema, options) {
validator = null
rootSchemaId = null
contextFunctions = null
arrayItemsReferenceSerializersMap.clear()
objectReferenceSerializersMap.clear()
contextFunctionsNamesBySchema.clear()

return stringifyFunc
}
Expand Down Expand Up @@ -542,12 +539,12 @@ function toJSON (variableName) {
function buildObject (location) {
const schema = location.schema

if (objectReferenceSerializersMap.has(schema)) {
return objectReferenceSerializersMap.get(schema)
if (contextFunctionsNamesBySchema.has(schema)) {
return contextFunctionsNamesBySchema.get(schema)
}

const functionName = generateFuncName()
objectReferenceSerializersMap.set(schema, functionName)
contextFunctionsNamesBySchema.set(schema, functionName)

const schemaId = location.schemaId === rootSchemaId ? '' : location.schemaId
let functionCode = `
Expand Down Expand Up @@ -589,12 +586,12 @@ function buildArray (location) {

const itemsSchema = itemsLocation.schema

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

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

const schemaId = location.schemaId === rootSchemaId ? '' : location.schemaId
let functionCode = `
Expand Down
25 changes: 25 additions & 0 deletions test/array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,31 @@ test('object array with anyOf and symbol', (t) => {
t.equal(value, '[{"name":"name-0","option":"Foo"},{"name":"name-1","option":"Bar"}]')
})

test('different arrays with same item schemas', (t) => {
t.plan(1)

const schema = {
type: 'object',
properties: {
array1: {
type: 'array',
items: [{ type: 'string' }],
additionalItems: false
},
array2: {
type: 'array',
items: { $ref: '#/properties/array1/items' },
additionalItems: true
}
}
}

const stringify = build(schema)
const data = { array1: ['bar'], array2: ['foo', 'bar'] }

t.equal(stringify(data), '{"array1":["bar"],"array2":["foo","bar"]}')
})

const largeArray = new Array(2e4).fill({ a: 'test', b: 1 })
buildTest({
title: 'large array with default mechanism',
Expand Down

0 comments on commit 3539f2d

Please sign in to comment.