Skip to content

Commit a37a438

Browse files
committed
refactor output
1 parent d993cea commit a37a438

7 files changed

Lines changed: 91 additions & 46 deletions

File tree

packages/openapi-ts-tests/faker/v1/__snapshots__/3.1.x/faker-basic-typed/@faker-js/faker.gen.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,28 @@ import type { Bar, Foo } from '../types.gen';
66

77
export type Options = {
88
faker?: Faker;
9-
includeOptional?: 'always' | 'random' | false;
10-
useDefault?: 'always' | 'random' | false;
9+
/**
10+
* Whether to include optional properties.
11+
* Provide a number between 0 and 1 to randomly include based on that probability.
12+
* @default true
13+
*/
14+
includeOptional?: boolean | number;
15+
/**
16+
* Whether to use schema default values instead of generating fake data.
17+
* Provide a number between 0 and 1 to randomly use defaults based on that probability.
18+
* @default false
19+
*/
20+
useDefault?: boolean | number;
1121
};
1222

13-
const resolveCondition = (condition: 'always' | 'random' | false, faker: Faker): boolean => condition === 'always' || condition === 'random' && faker.datatype.boolean();
23+
const resolveCondition = (condition: boolean | number, faker: Faker): boolean => condition === true || typeof condition === 'number' && faker.datatype.boolean({ probability: condition });
1424

1525
const ensureFaker = (options?: Options): Faker => options?.faker ?? faker;
1626

1727
export const fakeFoo = (options?: Options): Foo => ({
1828
name: ensureFaker(options).string.sample(),
1929
age: ensureFaker(options).number.int(),
20-
...!resolveCondition(options?.includeOptional ?? 'always', ensureFaker(options)) ? {} : { active: ensureFaker(options).datatype.boolean() }
30+
...!resolveCondition(options?.includeOptional ?? true, ensureFaker(options)) ? {} : { active: ensureFaker(options).datatype.boolean() }
2131
});
2232

2333
export const fakeBar = (options?: Options): Bar => ensureFaker(options).helpers.arrayElement(['baz', 'qux']);

packages/openapi-ts-tests/faker/v1/__snapshots__/3.1.x/faker-basic/@faker-js/faker.gen.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,28 @@ import { faker, type Faker } from '@faker-js/faker';
44

55
export type Options = {
66
faker?: Faker;
7-
includeOptional?: 'always' | 'random' | false;
8-
useDefault?: 'always' | 'random' | false;
7+
/**
8+
* Whether to include optional properties.
9+
* Provide a number between 0 and 1 to randomly include based on that probability.
10+
* @default true
11+
*/
12+
includeOptional?: boolean | number;
13+
/**
14+
* Whether to use schema default values instead of generating fake data.
15+
* Provide a number between 0 and 1 to randomly use defaults based on that probability.
16+
* @default false
17+
*/
18+
useDefault?: boolean | number;
919
};
1020

11-
const resolveCondition = (condition: 'always' | 'random' | false, faker: Faker): boolean => condition === 'always' || condition === 'random' && faker.datatype.boolean();
21+
const resolveCondition = (condition: boolean | number, faker: Faker): boolean => condition === true || typeof condition === 'number' && faker.datatype.boolean({ probability: condition });
1222

1323
const ensureFaker = (options?: Options): Faker => options?.faker ?? faker;
1424

1525
export const fakeFoo = (options?: Options) => ({
1626
name: ensureFaker(options).string.sample(),
1727
age: ensureFaker(options).number.int(),
18-
...!resolveCondition(options?.includeOptional ?? 'always', ensureFaker(options)) ? {} : { active: ensureFaker(options).datatype.boolean() }
28+
...!resolveCondition(options?.includeOptional ?? true, ensureFaker(options)) ? {} : { active: ensureFaker(options).datatype.boolean() }
1929
});
2030

2131
export const fakeBar = (options?: Options) => ensureFaker(options).helpers.arrayElement(['baz', 'qux']);

packages/openapi-ts-tests/faker/v1/__snapshots__/3.1.x/faker-m1/@faker-js/faker.gen.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,21 @@ import type { Active, Anything, Nothing, NumericEnum, Pet, PetList, PetOrTag, Pr
66

77
export type Options = {
88
faker?: Faker;
9-
includeOptional?: 'always' | 'random' | false;
10-
useDefault?: 'always' | 'random' | false;
9+
/**
10+
* Whether to include optional properties.
11+
* Provide a number between 0 and 1 to randomly include based on that probability.
12+
* @default true
13+
*/
14+
includeOptional?: boolean | number;
15+
/**
16+
* Whether to use schema default values instead of generating fake data.
17+
* Provide a number between 0 and 1 to randomly use defaults based on that probability.
18+
* @default false
19+
*/
20+
useDefault?: boolean | number;
1121
};
1222

13-
const resolveCondition = (condition: 'always' | 'random' | false, faker: Faker): boolean => condition === 'always' || condition === 'random' && faker.datatype.boolean();
23+
const resolveCondition = (condition: boolean | number, faker: Faker): boolean => condition === true || typeof condition === 'number' && faker.datatype.boolean({ probability: condition });
1424

1525
const ensureFaker = (options?: Options): Faker => options?.faker ?? faker;
1626

@@ -45,7 +55,7 @@ export const fakeTag = (options?: Options): Tag => ({
4555

4656
export const fakePet = (options?: Options): Pet => ({
4757
name: ensureFaker(options).string.sample(),
48-
...!resolveCondition(options?.includeOptional ?? 'always', ensureFaker(options)) ? {} : { tag: fakeTag(options) }
58+
...!resolveCondition(options?.includeOptional ?? true, ensureFaker(options)) ? {} : { tag: fakeTag(options) }
4959
});
5060

5161
export const fakePetOrTag = (options?: Options): PetOrTag => fakePet(options);

packages/openapi-ts-tests/faker/v1/__snapshots__/3.1.x/faker-m2/@faker-js/faker.gen.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,21 @@ import type { Address, BoundedFloat, BoundedInt, DateOnly, DateTime, DefaultBool
66

77
export type Options = {
88
faker?: Faker;
9-
includeOptional?: 'always' | 'random' | false;
10-
useDefault?: 'always' | 'random' | false;
9+
/**
10+
* Whether to include optional properties.
11+
* Provide a number between 0 and 1 to randomly include based on that probability.
12+
* @default true
13+
*/
14+
includeOptional?: boolean | number;
15+
/**
16+
* Whether to use schema default values instead of generating fake data.
17+
* Provide a number between 0 and 1 to randomly use defaults based on that probability.
18+
* @default false
19+
*/
20+
useDefault?: boolean | number;
1121
};
1222

13-
const resolveCondition = (condition: 'always' | 'random' | false, faker: Faker): boolean => condition === 'always' || condition === 'random' && faker.datatype.boolean();
23+
const resolveCondition = (condition: boolean | number, faker: Faker): boolean => condition === true || typeof condition === 'number' && faker.datatype.boolean({ probability: condition });
1424

1525
const ensureFaker = (options?: Options): Faker => options?.faker ?? faker;
1626

@@ -74,14 +84,14 @@ export const fakeNullableInt = (options?: Options): NullableInt => ensureFaker(o
7484

7585
export const fakeObjectWithDefaultProp = (options?: Options): ObjectWithDefaultProp => ({
7686
name: ensureFaker(options).string.sample(),
77-
...!resolveCondition(options?.includeOptional ?? 'always', ensureFaker(options)) ? {} : { status: resolveCondition(options?.useDefault ?? false, ensureFaker(options)) ? 'active' : ensureFaker(options).string.sample() }
87+
...!resolveCondition(options?.includeOptional ?? true, ensureFaker(options)) ? {} : { status: resolveCondition(options?.useDefault ?? false, ensureFaker(options)) ? 'active' : ensureFaker(options).string.sample() }
7888
});
7989

8090
export const fakeUserProfile = (options?: Options): UserProfile => ({
8191
id: ensureFaker(options).number.int(),
8292
name: ensureFaker(options).string.sample(),
83-
...!resolveCondition(options?.includeOptional ?? 'always', ensureFaker(options)) ? {} : { bio: ensureFaker(options).string.sample() },
84-
...!resolveCondition(options?.includeOptional ?? 'always', ensureFaker(options)) ? {} : { age: ensureFaker(options).number.int() }
93+
...!resolveCondition(options?.includeOptional ?? true, ensureFaker(options)) ? {} : { bio: ensureFaker(options).string.sample() },
94+
...!resolveCondition(options?.includeOptional ?? true, ensureFaker(options)) ? {} : { age: ensureFaker(options).number.int() }
8595
});
8696

8797
export const fakeAddress = (options?: Options): Address => ({
@@ -92,7 +102,7 @@ export const fakeAddress = (options?: Options): Address => ({
92102
export const fakePerson = (options?: Options): Person => ({
93103
name: fakeShortName(options),
94104
email: fakeEmail(options),
95-
...!resolveCondition(options?.includeOptional ?? 'always', ensureFaker(options)) ? {} : { address: fakeAddress(options) }
105+
...!resolveCondition(options?.includeOptional ?? true, ensureFaker(options)) ? {} : { address: fakeAddress(options) }
96106
});
97107

98108
export const fakePersonList = (options?: Options): PersonList => ensureFaker(options).helpers.multiple(() => fakePerson(options), { count: { min: 1, max: 20 } });

packages/openapi-ts/src/plugins/@faker-js/faker/v1/plugin.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,29 @@ export const handlerV1: FakerJsFakerPlugin['Handler'] = ({ plugin }) => {
2323
.prop('faker', (p) => p.optional().type($.type(fakerTypeSymbol)))
2424
.prop('includeOptional', (p) =>
2525
p
26+
.doc([
27+
'Whether to include optional properties.',
28+
'Provide a number between 0 and 1 to randomly include based on that probability.',
29+
'@default true',
30+
])
2631
.optional()
27-
.type($.type.or($.type.literal('always'), $.type.literal('random'), $.type.literal(false))),
32+
.type($.type.or($.type('boolean'), $.type('number'))),
2833
)
2934
.prop('useDefault', (p) =>
3035
p
36+
.doc([
37+
'Whether to use schema default values instead of generating fake data.',
38+
'Provide a number between 0 and 1 to randomly use defaults based on that probability.',
39+
'@default false',
40+
])
3141
.optional()
32-
.type($.type.or($.type.literal('always'), $.type.literal('random'), $.type.literal(false))),
42+
.type($.type.or($.type('boolean'), $.type('number'))),
3343
);
3444
plugin.node($.type.alias(optionsSymbol).export().type(optionsType));
3545

36-
// Emit helper: const resolveCondition = (condition: 'always' | 'random' | false, faker: Faker): boolean =>
37-
// condition === 'always' || condition === 'random' && faker.datatype.boolean()
38-
const conditionParamType = $.type.or(
39-
$.type.literal('always'),
40-
$.type.literal('random'),
41-
$.type.literal(false),
42-
);
46+
// Emit helper: const resolveCondition = (condition: boolean | number, faker: Faker): boolean =>
47+
// condition === true || typeof condition === 'number' && faker.datatype.boolean({ probability: condition })
48+
const conditionParamType = $.type.or($.type('boolean'), $.type('number'));
4349
const resolveConditionFn = $.func()
4450
.arrow()
4551
.param('condition', (p) => p.type(conditionParamType))
@@ -48,13 +54,16 @@ export const handlerV1: FakerJsFakerPlugin['Handler'] = ({ plugin }) => {
4854
.do(
4955
$.return(
5056
$.binary(
51-
$('condition').eq($.literal('always')),
57+
$('condition').eq($.literal(true)),
5258
'||',
5359
$(
5460
$.binary(
55-
$('condition').eq($.literal('random')),
61+
$('condition').typeofExpr().eq($.literal('number')),
5662
'&&',
57-
$('faker').attr('datatype').attr('boolean').call(),
63+
$('faker')
64+
.attr('datatype')
65+
.attr('boolean')
66+
.call($.object().prop('probability', $('condition'))),
5867
),
5968
),
6069
),

packages/openapi-ts/src/plugins/@faker-js/faker/v1/toAst/object.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import type { FakerJsFakerPlugin } from '../../types';
1010
*
1111
* Required properties are always included. Optional properties are
1212
* conditionally included based on `options.includeOptional`:
13-
* - `'always'` or `undefined` (default) — always included
14-
* - `'random'` — included when `faker.datatype.boolean()` returns true
13+
* - `true` or `undefined` (default) — always included
14+
* - number (0.0-1.0) — included with that probability
1515
* - `false` — omitted entirely
1616
*/
1717
export function objectToExpression({
@@ -36,13 +36,9 @@ export function objectToExpression({
3636
obj.prop(name, result.expression);
3737
} else {
3838
// Optional property: conditionally spread based on options.includeOptional
39-
// Generated: ...(!resolveCondition(options?.includeOptional ?? 'always', faker) ? {} : { prop: value })
39+
// Generated: ...(!resolveCondition(options?.includeOptional ?? true, faker) ? {} : { prop: value })
4040
const includeCondition = $('resolveCondition').call(
41-
$.binary(
42-
$(fakerCtx.optionsId).attr('includeOptional').optional(),
43-
'??',
44-
$.literal('always'),
45-
),
41+
$.binary($(fakerCtx.optionsId).attr('includeOptional').optional(), '??', $.literal(true)),
4642
fakerCtx.fakerAccessor,
4743
);
4844
const propObj = $.object().prop(name, result.expression);

packages/openapi-ts/src/plugins/@faker-js/faker/v1/walker.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export interface VisitorConfig {
2121
schemaExtractor?: SchemaExtractor<ProcessorContext>;
2222
}
2323

24-
const LITERAL_FALSE: FakerResult = { expression: $('undefined'), usesFaker: false };
24+
const FALLBACK_UNDEFINED: FakerResult = { expression: $('undefined'), usesFaker: false };
2525

2626
export function createVisitor(
2727
config: VisitorConfig,
@@ -81,10 +81,10 @@ export function createVisitor(
8181
if (items.length > 0) {
8282
return items[0]!;
8383
}
84-
return LITERAL_FALSE;
84+
return FALLBACK_UNDEFINED;
8585
},
8686
never() {
87-
return LITERAL_FALSE;
87+
return FALLBACK_UNDEFINED;
8888
},
8989
null() {
9090
return { expression: $.fromValue(null), usesFaker: false };
@@ -149,11 +149,11 @@ export function createVisitor(
149149
return { expression: $.array(...elements), usesFaker: anyChildUsesFaker };
150150
},
151151
undefined() {
152-
return LITERAL_FALSE;
152+
return FALLBACK_UNDEFINED;
153153
},
154154
union(items, schemas) {
155155
if (items.length === 0) {
156-
return LITERAL_FALSE;
156+
return FALLBACK_UNDEFINED;
157157
}
158158
// If one variant is null, randomly choose between non-null and null
159159
const nullIndex = schemas.findIndex((s) => s.type === 'null');
@@ -170,11 +170,11 @@ export function createVisitor(
170170
return items[0]!;
171171
},
172172
unknown() {
173-
return LITERAL_FALSE;
173+
return FALLBACK_UNDEFINED;
174174
},
175175

176176
void() {
177-
return LITERAL_FALSE;
177+
return FALLBACK_UNDEFINED;
178178
},
179179
};
180180
}

0 commit comments

Comments
 (0)