Skip to content

Commit

Permalink
fix: faker bigint support (#1675)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marfyy authored Oct 25, 2024
1 parent 9a54567 commit b52f0aa
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
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

0 comments on commit b52f0aa

Please sign in to comment.