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

Recursive schema evaluation - stop evaluating initial value for empty array #1282

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,25 @@ describe('JSONSchemaBridge', () => {
firstName: { type: 'string', default: 'John' },
middleName: { $ref: '#/definitions/lastName' },
lastName: { type: 'string' },
recursive: {
recursiveObject: {
type: 'object',
properties: {
field: { type: 'string' },
recursive: { $ref: '#/definitions/recursive' },
recursiveObject: { $ref: '#/definitions/recursiveObject' },
},
},
recursiveArray: {
type: 'array',
items: { $ref: '#/definitions/recursiveArray' },
},
bloodlineNode: {
type: 'object',
properties: {
name: { type: 'string' },
children: {
type: 'array',
items: { $ref: '#/definitions/bloodlineNode' },
},
},
},
},
Expand Down Expand Up @@ -135,7 +149,13 @@ describe('JSONSchemaBridge', () => {
minimum: 1000,
multipleOf: 3,
},
recursive: { $ref: '#/definitions/recursive' },
recursiveObject: { $ref: '#/definitions/recursiveObject' },
recursiveArray: {
$ref: '#/definitions/recursiveArray',
},
bloodline: {
$ref: '#/definitions/bloodlineNode',
},
arrayWithAllOf: {
type: 'array',
items: {
Expand Down Expand Up @@ -888,7 +908,9 @@ describe('JSONSchemaBridge', () => {
'complexNames',
'password',
'passwordNumeric',
'recursive',
'recursiveObject',
'recursiveArray',
'bloodline',
'arrayWithAllOf',
'nonObjectAnyOf',
'nonObjectAnyOfRequired',
Expand All @@ -911,13 +933,24 @@ describe('JSONSchemaBridge', () => {
]);
});

it('works with recursive types', () => {
expect(bridge.getSubfields('recursive')).toEqual(['field', 'recursive']);
expect(bridge.getSubfields('recursive.recursive')).toEqual([
it('works with recursive types - object', () => {
expect(bridge.getSubfields('recursiveObject')).toEqual([
'field',
'recursiveObject',
]);
expect(bridge.getSubfields('recursiveObject.recursiveObject')).toEqual([
'field',
'recursive',
'recursiveObject',
]);
});
it('works with recursive types - array', () => {
expect(bridge.getSubfields('recursiveArray')).toEqual([]);
});

it('works with recursive types - mixed', () => {
expect(bridge.getSubfields('bloodline')).toEqual(['name', 'children']);
expect(bridge.getSubfields('bloodline.children')).toEqual([]);
});

it('works with primitives', () => {
expect(bridge.getSubfields('personalData.firstName')).toEqual([]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,15 @@ export default class JSONSchemaBridge extends Bridge {
}

if (type === 'array') {
if (!field.minItems) {
return [];
}
const item = this.getInitialValue(joinName(name, '$'));
if (item === undefined) {
return [];
}

const length = field.minItems || 0;
return Array.from({ length }, () => item);
return Array.from({ length: field.minItems }, () => item);
}

if (type === 'object') {
Expand Down
18 changes: 8 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading