Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle items: [{ $ref }] #734

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,9 +564,14 @@ function buildArray (context, location) {

if (Array.isArray(itemsSchema)) {
for (let i = 0; i < itemsSchema.length; i++) {
const item = itemsSchema[i]
let item = itemsSchema[i]
let itemLocation = itemsLocation.getPropertyLocation(i)
if (item.$ref) {
itemLocation = resolveRef(context, itemLocation)
item = itemLocation.schema
}
functionCode += `value = obj[${i}]`
const tmpRes = buildValue(context, itemsLocation.getPropertyLocation(i), 'value')
const tmpRes = buildValue(context, itemLocation, 'value')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't buildValue itself will resolve $ref?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, I only looked into the code enough to fix the problem with minimal changes. But my problem wasn't with buildValue, it was that buildArrayTypeCondition was given item.type which was undefined since item was still {$ref }.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only changed the buildValue line to use the itemLocation I defined above so that it wouldn't need to do the itemsLocation.getPropertyLocation(i) a second time.

functionCode += `
if (${i} < arrayLength) {
if (${buildArrayTypeCondition(item.type, `[${i}]`)}) {
Expand Down
28 changes: 28 additions & 0 deletions test/array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,34 @@ buildTest({
dates: [new Date(1), new Date(2)]
})

buildTest({
title: 'dates tuple $ref',
definitions: {
dateTime: {
type: 'string',
format: 'date-time'
}
},
type: 'object',
properties: {
dates: {
type: 'array',
minItems: 2,
maxItems: 2,
items: [
{
$ref: '#/definitions/dateTime'
},
{
$ref: '#/definitions/dateTime'
}
]
}
}
}, {
dates: [new Date(1), new Date(2)]
})

buildTest({
title: 'string array',
type: 'object',
Expand Down