From 6b1be15bd61bd50ca6a13b7fc64b46102da0403b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20S=C3=A4fsten?= Date: Fri, 25 Oct 2024 21:41:09 +0200 Subject: [PATCH] fix: faker bigint support --- packages/mock/src/faker/getters/scalar.ts | 22 +++++++++++++++++++++- tests/configs/mock.config.ts | 7 ++++--- tests/specifications/format.yaml | 18 ++++++++++++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/packages/mock/src/faker/getters/scalar.ts b/packages/mock/src/faker/getters/scalar.ts index 0345c5e74..29ef6bf7a 100644 --- a/packages/mock/src/faker/getters/scalar.ts +++ b/packages/mock/src/faker/getters/scalar.ts @@ -122,7 +122,7 @@ export const getMockScalar = ({ case 'number': case 'integer': { let value = getNullable( - `faker.number.int({min: ${item.minimum}, max: ${item.maximum}})`, + `${getNumberType(type, item.format, context.output.override.useBigInt)}({min: ${item.minimum}, max: ${item.maximum}})`, item.nullable, ); let numberImports: GeneratorImport[] = []; @@ -327,3 +327,23 @@ function getItemType(item: MockSchemaObject) { if (!type) return; return ['string', 'number'].includes(type) ? type : undefined; } + +/** + * Checks type and format and returns the correct faker number function + * + * Will default to int if no specific type is found + */ +function getNumberType(type?: string, format?: string, useBigInt?: boolean) { + switch (type) { + case 'integer': + return format == 'int64' && useBigInt === true + ? 'faker.number.bigInt' + : 'faker.number.int'; + case 'number': + return format == 'double' || format == 'float' + ? 'faker.number.float' + : 'faker.number.int'; + default: + return 'faker.number.int'; + } +} diff --git a/tests/configs/mock.config.ts b/tests/configs/mock.config.ts index e391a76b3..773cad75d 100644 --- a/tests/configs/mock.config.ts +++ b/tests/configs/mock.config.ts @@ -147,14 +147,15 @@ export default defineConfig({ mock: true, }, }, - useDates: { + formats: { input: '../specifications/format.yaml', output: { - target: '../generated/mock/useDates/endpoints.ts', - schemas: '../generated/mock/useDates/model', + target: '../generated/mock/formats/endpoints.ts', + schemas: '../generated/mock/formats/model', mock: true, override: { useDates: true, + useBigInt: true, }, }, }, diff --git a/tests/specifications/format.yaml b/tests/specifications/format.yaml index 7b69126a1..a1873867b 100644 --- a/tests/specifications/format.yaml +++ b/tests/specifications/format.yaml @@ -41,9 +41,27 @@ components: - birthDate - createdAt properties: + id: + type: integer + format: int64 birthDate: type: string format: date createdAt: type: string format: date-time + age: + type: integer + legCount: + type: number + weight: + type: number + format: double + height: + type: number + format: float + chipNumbers: + type: array + items: + type: integer + format: int64