Skip to content

Commit 3539f2d

Browse files
fix: set array function name by array schema (#528)
1 parent 26ef3cd commit 3539f2d

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

index.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,15 @@ function resolveRef (location, ref) {
7070
return { schema, schemaId, jsonPointer }
7171
}
7272

73-
const arrayItemsReferenceSerializersMap = new Map()
74-
const objectReferenceSerializersMap = new Map()
73+
const contextFunctionsNamesBySchema = new Map()
7574

7675
let rootSchemaId = null
7776
let refResolver = null
7877
let validator = null
7978
let contextFunctions = null
8079

8180
function build (schema, options) {
82-
arrayItemsReferenceSerializersMap.clear()
83-
objectReferenceSerializersMap.clear()
81+
contextFunctionsNamesBySchema.clear()
8482

8583
contextFunctions = []
8684
options = options || {}
@@ -168,8 +166,7 @@ function build (schema, options) {
168166
validator = null
169167
rootSchemaId = null
170168
contextFunctions = null
171-
arrayItemsReferenceSerializersMap.clear()
172-
objectReferenceSerializersMap.clear()
169+
contextFunctionsNamesBySchema.clear()
173170

174171
return stringifyFunc
175172
}
@@ -542,12 +539,12 @@ function toJSON (variableName) {
542539
function buildObject (location) {
543540
const schema = location.schema
544541

545-
if (objectReferenceSerializersMap.has(schema)) {
546-
return objectReferenceSerializersMap.get(schema)
542+
if (contextFunctionsNamesBySchema.has(schema)) {
543+
return contextFunctionsNamesBySchema.get(schema)
547544
}
548545

549546
const functionName = generateFuncName()
550-
objectReferenceSerializersMap.set(schema, functionName)
547+
contextFunctionsNamesBySchema.set(schema, functionName)
551548

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

590587
const itemsSchema = itemsLocation.schema
591588

592-
if (arrayItemsReferenceSerializersMap.has(itemsSchema)) {
593-
return arrayItemsReferenceSerializersMap.get(itemsSchema)
589+
if (contextFunctionsNamesBySchema.has(schema)) {
590+
return contextFunctionsNamesBySchema.get(schema)
594591
}
595592

596593
const functionName = generateFuncName()
597-
arrayItemsReferenceSerializersMap.set(itemsSchema, functionName)
594+
contextFunctionsNamesBySchema.set(schema, functionName)
598595

599596
const schemaId = location.schemaId === rootSchemaId ? '' : location.schemaId
600597
let functionCode = `

test/array.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,31 @@ test('object array with anyOf and symbol', (t) => {
327327
t.equal(value, '[{"name":"name-0","option":"Foo"},{"name":"name-1","option":"Bar"}]')
328328
})
329329

330+
test('different arrays with same item schemas', (t) => {
331+
t.plan(1)
332+
333+
const schema = {
334+
type: 'object',
335+
properties: {
336+
array1: {
337+
type: 'array',
338+
items: [{ type: 'string' }],
339+
additionalItems: false
340+
},
341+
array2: {
342+
type: 'array',
343+
items: { $ref: '#/properties/array1/items' },
344+
additionalItems: true
345+
}
346+
}
347+
}
348+
349+
const stringify = build(schema)
350+
const data = { array1: ['bar'], array2: ['foo', 'bar'] }
351+
352+
t.equal(stringify(data), '{"array1":["bar"],"array2":["foo","bar"]}')
353+
})
354+
330355
const largeArray = new Array(2e4).fill({ a: 'test', b: 1 })
331356
buildTest({
332357
title: 'large array with default mechanism',

0 commit comments

Comments
 (0)