diff --git a/index.js b/index.js index 5457ff06..b0427c3f 100644 --- a/index.js +++ b/index.js @@ -70,8 +70,7 @@ 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 @@ -79,8 +78,7 @@ let validator = null let contextFunctions = null function build (schema, options) { - arrayItemsReferenceSerializersMap.clear() - objectReferenceSerializersMap.clear() + contextFunctionsNamesBySchema.clear() contextFunctions = [] options = options || {} @@ -168,8 +166,7 @@ function build (schema, options) { validator = null rootSchemaId = null contextFunctions = null - arrayItemsReferenceSerializersMap.clear() - objectReferenceSerializersMap.clear() + contextFunctionsNamesBySchema.clear() return stringifyFunc } @@ -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 = ` @@ -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 = ` diff --git a/test/array.test.js b/test/array.test.js index b1fe96ff..996f6c36 100644 --- a/test/array.test.js +++ b/test/array.test.js @@ -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',