Skip to content

Commit

Permalink
fix(mock): combine allOf with properties when one element
Browse files Browse the repository at this point in the history
  • Loading branch information
anymaniax committed Sep 23, 2022
1 parent 417da15 commit 65fcc68
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions src/core/getters/combine.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,41 +73,56 @@ export const combineSchemasMock = ({
combineImports.push(...resolvedValue.imports);
includedProperties.push(...(resolvedValue.includedProperties ?? []));

const value =
itemResolvedValue?.value && separator === 'oneOf'
? `${resolvedValue.value.slice(0, -1)},${itemResolvedValue.value}}`
: resolvedValue.value;
const isLastElement = index === arr.length - 1;

let currentValue = resolvedValue.value;

if (itemResolvedValue?.value && separator === 'oneOf') {
currentValue = `${resolvedValue.value.slice(0, -1)},${
itemResolvedValue.value
}}`;
}

if (itemResolvedValue?.value && separator !== 'oneOf' && isLastElement) {
currentValue = `${currentValue}${
itemResolvedValue?.value ? `,${itemResolvedValue.value}` : ''
}`;
}

const isObjectBounds =
!combine || (combine.separator === 'oneOf' && separator === 'allOf');

if (!index && isObjectBounds) {
if (resolvedValue.enums || separator === 'oneOf') {
if (arr.length === 1) {
return `faker.helpers.arrayElement([${value}])`;
return `faker.helpers.arrayElement([${currentValue}])`;
}
return `faker.helpers.arrayElement([${value},`;
return `faker.helpers.arrayElement([${currentValue},`;
}

if (arr.length === 1) {
if (resolvedValue.type !== 'object') {
return `${value}`;
return currentValue;
}
return `{${value}}`;
return `{${currentValue}}`;
}
return `{${value},`;

return `{${currentValue},`;
}
if (arr.length - 1 === index) {

if (isLastElement) {
if (resolvedValue.enums || separator === 'oneOf') {
return `${acc}${value}${!combine ? '])' : ''}`;
return `${acc}${currentValue}${!combine ? '])' : ''}`;
}
return `${acc}${value}${
itemResolvedValue?.value ? `,${itemResolvedValue.value}` : ''
}${isObjectBounds ? '}' : ''}`;

return `${acc}${currentValue}${isObjectBounds ? '}' : ''}`;
}
if (!value) {

if (!currentValue) {
return acc;
}
return `${acc}${value},`;

return `${acc}${currentValue},`;
}, '');

return {
Expand Down

0 comments on commit 65fcc68

Please sign in to comment.