Skip to content

Commit 7e1cd78

Browse files
fix: handle references in array items (#734)
* Handle items: [{ $ref }] Signed-off-by: rtkevin <kevin@rtvision.com> * Update array.test.js Signed-off-by: rtkevin <kevin@rtvision.com> * fix: update to latest fast-json-stringify * fix: linting issue --------- Signed-off-by: rtkevin <kevin@rtvision.com> Co-authored-by: Kalven Schraut <kalvens@rtvision.com>
1 parent 360bb6b commit 7e1cd78

2 files changed

Lines changed: 42 additions & 4 deletions

File tree

index.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -665,10 +665,15 @@ function buildArray (context, location, input) {
665665

666666
if (Array.isArray(itemsSchema)) {
667667
for (let i = 0, itemsSchemaLength = itemsSchema.length; i < itemsSchemaLength; i++) {
668-
const item = itemsSchema[i]
668+
let item = itemsSchema[i]
669+
let itemLocation = itemsLocation.getPropertyLocation(i)
670+
if (itemLocation.schema.$ref) {
671+
itemLocation = resolveRef(context, itemLocation)
672+
item = itemLocation.schema
673+
}
669674
const value = `value_${i}`
670675
functionCode += `const ${value} = obj[${i}]`
671-
const tmpRes = buildValue(context, itemsLocation.getPropertyLocation(i), value)
676+
const tmpRes = buildValue(context, itemLocation, value)
672677
functionCode += `
673678
if (${i} < arrayLength) {
674679
if (${buildArrayTypeCondition(item.type, value)}) {
@@ -747,10 +752,15 @@ function buildArray (context, location, input) {
747752
const localUid = context.uid++
748753
inlinedCode += `let addComma_${localUid} = false\n`
749754
for (let i = 0, itemsSchemaLength = itemsSchema.length; i < itemsSchemaLength; i++) {
750-
const item = itemsSchema[i]
755+
let item = itemsSchema[i]
756+
let itemLocation = itemsLocation.getPropertyLocation(i)
757+
if (itemLocation.schema.$ref) {
758+
itemLocation = resolveRef(context, itemLocation)
759+
item = itemLocation.schema
760+
}
751761
const value = `value_${i}_${context.uid++}`
752762
inlinedCode += `const ${value} = ${objVar}[${i}]`
753-
const tmpRes = buildValue(context, itemsLocation.getPropertyLocation(i), value)
763+
const tmpRes = buildValue(context, itemLocation, value)
754764
inlinedCode += `
755765
if (${i} < arrayLength_${objVar}) {
756766
if (${buildArrayTypeCondition(item.type, value)}) {

test/array.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,34 @@ buildTest({
6161
dates: [new Date(1), new Date(2)]
6262
})
6363

64+
buildTest({
65+
title: 'dates tuple $ref',
66+
definitions: {
67+
dateTime: {
68+
type: 'string',
69+
format: 'date-time'
70+
}
71+
},
72+
type: 'object',
73+
properties: {
74+
dates: {
75+
type: 'array',
76+
minItems: 2,
77+
maxItems: 2,
78+
items: [
79+
{
80+
$ref: '#/definitions/dateTime'
81+
},
82+
{
83+
$ref: '#/definitions/dateTime'
84+
}
85+
]
86+
}
87+
}
88+
}, {
89+
dates: [new Date(1), new Date(2)]
90+
})
91+
6492
buildTest({
6593
title: 'string array',
6694
type: 'object',

0 commit comments

Comments
 (0)