Skip to content

Commit

Permalink
fix(mocks): array enums handled correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
anymaniax committed Apr 2, 2022
1 parent df688ea commit 0656ae5
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/core/generators/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const getResponsesMockDefinition = ({
}) => {
return asyncReduce(
response.types.success,
async (acc, { value: definition, originalSchema }) => {
async (acc, { value: definition, originalSchema, imports }) => {
if (!definition || generalJSTypesWithArray.includes(definition)) {
const value = getMockScalarJsTypes(definition);

Expand Down Expand Up @@ -149,6 +149,7 @@ export const getResponsesMockDefinition = ({
}
: {}),
},
imports,
mockOptions: mockOptionsWithoutFunc,
operationId,
tags,
Expand Down
9 changes: 6 additions & 3 deletions src/core/getters/combine.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const combineSchemasMock = async ({
tags,
combine,
context,
imports,
}: {
item: SchemaObject & { name: string; path?: string; specKey?: string };
items: (SchemaObject | ReferenceObject)[];
Expand All @@ -22,8 +23,9 @@ export const combineSchemasMock = async ({
tags: string[];
combine?: { properties: string[] };
context: ContextSpecs;
imports: GeneratorImport[];
}) => {
let imports: GeneratorImport[] = [];
let combineImports: GeneratorImport[] = [];
let properties: string[] = [...(combine?.properties || [])];
const value = await asyncReduce(
items,
Expand All @@ -44,9 +46,10 @@ export const combineSchemasMock = async ({
operationId,
tags,
context,
imports,
});

imports = [...imports, ...resolvedValue.imports];
combineImports = [...combineImports, ...resolvedValue.imports];
properties = [...properties, ...(resolvedValue.properties || [])];

if (!index && !combine) {
Expand Down Expand Up @@ -77,7 +80,7 @@ export const combineSchemasMock = async ({

return {
value,
imports,
imports: combineImports,
name: item.name,
properties,
};
Expand Down
5 changes: 5 additions & 0 deletions src/core/getters/object.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ export const getMockObject = async ({
tags,
combine,
context,
imports,
}: {
item: SchemaObject & { name: string; path?: string; specKey?: string };
operationId: string;
mockOptions?: MockOptions;
tags: string[];
combine?: { properties: string[] };
context: ContextSpecs;
imports: GeneratorImport[];
}): Promise<MockDefinition> => {
if (isReference(item)) {
return resolveMockValue({
Expand All @@ -36,6 +38,7 @@ export const getMockObject = async ({
operationId,
tags,
context,
imports,
});
}

Expand Down Expand Up @@ -83,6 +86,7 @@ export const getMockObject = async ({
operationId,
tags,
context,
imports,
});

imports = [...imports, ...resolvedValue.imports];
Expand Down Expand Up @@ -125,6 +129,7 @@ export const getMockObject = async ({
operationId,
tags,
context,
imports,
});

return {
Expand Down
27 changes: 21 additions & 6 deletions src/core/getters/scalar.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { getMockObject } from './object.mock';

export const getMockScalar = async ({
item,
imports,
mockOptions,
operationId,
tags,
Expand All @@ -26,6 +27,7 @@ export const getMockScalar = async ({
isRef?: boolean;
specKey?: string;
};
imports: GeneratorImport[];
mockOptions?: MockOptions;
operationId: string;
isRef?: boolean;
Expand Down Expand Up @@ -97,7 +99,12 @@ export const getMockScalar = async ({
return { value: [], imports: [], name: item.name };
}

const { value, enums, imports, name } = await resolveMockValue({
const {
value,
enums,
imports: resolvedImports,
name,
} = await resolveMockValue({
schema: {
...item.items,
name: item.name,
Expand All @@ -109,25 +116,32 @@ export const getMockScalar = async ({
operationId,
tags,
context,
imports,
});

if (enums) {
const enumImp = imports.find(
(imp) => name.replace('[]', '') === imp.name,
);
const enumValue = enumImp?.name || name;
return {
value: `[...Array(faker.datatype.number({min:1, max: ${enums.length}}))].reduce(({values, enums}) => {
const newValue = enums[faker.datatype.number({min:1, max: enums.length})];
const newValue = enums[faker.datatype.number({min:0, max: enums.length - 1})];
return {
values: [...values, newValue],
enums: enums.filter((v: ${name}) => newValue !== v)
enums: enums.filter((v: ${enumValue}) => newValue !== v)
}
},{ values: [], enums: Object.values(${name})})`,
imports,
},{ values: [], enums: Object.values(${enumValue})}).values`,
imports: enumImp
? [...resolvedImports, { ...enumImp, values: true }]
: resolvedImports,
name: item.name,
};
}

return {
value: `[...Array(faker.datatype.number({min: 1, max: 10}))].map(() => (${value}))`,
imports,
imports: resolvedImports,
name: item.name,
};
}
Expand Down Expand Up @@ -165,6 +179,7 @@ export const getMockScalar = async ({
tags,
combine,
context,
imports,
});
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/core/resolvers/value.mock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SchemaObject } from 'openapi3-ts';
import { ContextSpecs, MockOptions } from '../../types';
import { GeneratorImport } from '../../types/generator';
import { MockDefinition } from '../../types/mocks';
import { isReference } from '../../utils/is';
import { getRefInfo } from '../getters/ref';
Expand Down Expand Up @@ -49,13 +50,15 @@ export const resolveMockValue = async ({
tags,
combine,
context,
imports,
}: {
schema: SchemaObject & { name: string; path?: string; specKey?: string };
operationId: string;
mockOptions?: MockOptions;
tags: string[];
combine?: { properties: string[] };
context: ContextSpecs;
imports: GeneratorImport[];
}): Promise<MockDefinition> => {
if (isReference(schema)) {
const { name, specKey } = await getRefInfo(schema.$ref, {
Expand All @@ -78,6 +81,7 @@ export const resolveMockValue = async ({
tags,
combine,
context,
imports,
});
}

Expand All @@ -88,5 +92,6 @@ export const resolveMockValue = async ({
tags,
combine,
context,
imports,
});
};
11 changes: 10 additions & 1 deletion src/core/writers/singleMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,16 @@ export const writeSingleMode = async ({
data += generateClientImports(
output.client,
implementation,
schemasPath ? [{ exports: imports, dependency: schemasPath }] : [],
schemasPath
? [
{
exports: imports.filter(
(imp) => !importsMSW.some((impMSW) => imp.name === impMSW.name),
),
dependency: schemasPath,
},
]
: [],
specsName,
!!output.schemas,
isSyntheticDefaultImportsAllowed,
Expand Down
9 changes: 8 additions & 1 deletion src/core/writers/tagsMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ export const writeTagsMode = async ({
data += generateClientImports(
output.client,
implementation,
[{ exports: imports, dependency: schemasPathRelative }],
[
{
exports: imports.filter(
(imp) => !importsMSW.some((impMSW) => imp.name === impMSW.name),
),
dependency: schemasPathRelative,
},
],
specsName,
!!output.schemas,
isSyntheticDefaultImportsAllowed,
Expand Down

1 comment on commit 0656ae5

@vercel
Copy link

@vercel vercel bot commented on 0656ae5 Apr 2, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.