Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ const Location = require('./lib/location')
const validate = require('./lib/schema-validator')
const mergeSchemas = require('./lib/merge-schemas')

let largeArraySize = 2e4
let largeArrayMechanism = 'default'

const DEFAULT_MAX_DEPTH = 100
const NAMED_FRAGMENT_REF = /^#[a-z_][-\w._]*$/i

Expand Down Expand Up @@ -345,6 +342,8 @@ function build (schema, options) {
recursiveSchemas: new Set(),
recursivePaths: new Set(),
buildingSet: new Set(),
largeArraySize: 2e4,
largeArrayMechanism: 'default',
uid: 0
}

Expand Down Expand Up @@ -374,7 +373,7 @@ function build (schema, options) {

if (options.largeArrayMechanism) {
if (validLargeArrayMechanisms.has(options.largeArrayMechanism)) {
largeArrayMechanism = options.largeArrayMechanism
context.largeArrayMechanism = options.largeArrayMechanism
} else {
throw new Error(`Unsupported large array mechanism ${options.largeArrayMechanism}`)
}
Expand All @@ -385,11 +384,11 @@ function build (schema, options) {
let parsedNumber

if (largeArraySizeType === 'string' && Number.isFinite((parsedNumber = Number.parseInt(options.largeArraySize, 10)))) {
largeArraySize = parsedNumber
context.largeArraySize = parsedNumber
} else if (largeArraySizeType === 'number' && Number.isInteger(options.largeArraySize)) {
largeArraySize = options.largeArraySize
context.largeArraySize = options.largeArraySize
} else if (largeArraySizeType === 'bigint') {
largeArraySize = Number(options.largeArraySize)
context.largeArraySize = Number(options.largeArraySize)
} else {
throw new Error(`Unsupported large array size. Expected integer-like, got ${typeof options.largeArraySize} with value ${options.largeArraySize}`)
}
Expand Down Expand Up @@ -895,8 +894,8 @@ function buildArray (context, location, input) {
`
}

if (largeArrayMechanism === 'json-stringify') {
functionCode += `if (arrayLength >= ${largeArraySize}) return JSON.stringify(obj)\n`
if (context.largeArrayMechanism === 'json-stringify') {
functionCode += `if (arrayLength >= ${context.largeArraySize}) return JSON.stringify(obj)\n`
}

functionCode += `
Expand Down Expand Up @@ -980,8 +979,8 @@ function buildArray (context, location, input) {
`
}

if (largeArrayMechanism === 'json-stringify') {
inlinedCode += `if (arrayLength_${objVar} >= ${largeArraySize}) json += JSON.stringify(${objVar})\n else {`
if (context.largeArrayMechanism === 'json-stringify') {
inlinedCode += `if (arrayLength_${objVar} >= ${context.largeArraySize}) json += JSON.stringify(${objVar})\n else {`
}

inlinedCode += `
Expand Down Expand Up @@ -1038,7 +1037,7 @@ function buildArray (context, location, input) {
json += JSON_STR_END_ARRAY
`

if (largeArrayMechanism === 'json-stringify') {
if (context.largeArrayMechanism === 'json-stringify') {
inlinedCode += '}'
}

Expand Down
25 changes: 25 additions & 0 deletions test/array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,31 @@ test('error on invalid largeArrayMechanism', (t) => {
}), Error('Unsupported large array mechanism invalid'))
})

test('does not leak largeArrayMechanism across build() calls', (t) => {
t.plan(2)

const users = []
for (let i = 0; i < 100; i++) users.push({ name: 'user' + i, password: 'secret' + i })

const schema = {
type: 'array',
items: {
type: 'object',
properties: {
name: { type: 'string' }
}
}
}

// An unrelated build() opts into json-stringify for its own schema.
const optedIn = build({ type: 'array' }, { largeArrayMechanism: 'json-stringify', largeArraySize: 100 })
t.assert.equal(optedIn(users).includes('password'), true)

// A later build() without options must not inherit the mechanism.
const stringify = build(schema)
t.assert.equal(stringify(users).includes('password'), false)
})

function buildTest (schema, toStringify, options) {
test(`render a ${schema.title} as JSON`, (t) => {
t.plan(3)
Expand Down
Loading