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

fix: faker bigint support #1675

Merged
merged 1 commit into from
Oct 25, 2024
Merged
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
22 changes: 21 additions & 1 deletion packages/mock/src/faker/getters/scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand Down Expand Up @@ -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';
}
}
7 changes: 4 additions & 3 deletions tests/configs/mock.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
Expand Down
18 changes: 18 additions & 0 deletions tests/specifications/format.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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