fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #2152 from hey-api/fix/zod-bigint-default

fix(validators): correctly generate default value for BigInt

authored by

Lubos and committed by
GitHub
ced57a77 bfa2c5ba

+40 -20
+5
.changeset/tame-stingrays-boil.md
··· 1 + --- 2 + '@hey-api/openapi-ts': patch 3 + --- 4 + 5 + fix(validators): correctly generate default value for `BigInt`
+1 -1
packages/openapi-ts-tests/test/__snapshots__/2.0.x/plugins/@hey-api/transformers/type-format-valibot/valibot.gen.ts
··· 4 4 5 5 export const vFoo = v.object({ 6 6 bar: v.optional(v.pipe(v.number(), v.integer())), 7 - foo: v.bigint(), 7 + foo: v.optional(v.bigint(), BigInt(0)), 8 8 id: v.string() 9 9 }); 10 10
+1 -1
packages/openapi-ts-tests/test/__snapshots__/2.0.x/plugins/@hey-api/transformers/type-format-zod/zod.gen.ts
··· 4 4 5 5 export const zFoo = z.object({ 6 6 bar: z.number().int().optional(), 7 - foo: z.coerce.bigint(), 7 + foo: z.coerce.bigint().default(BigInt(0)), 8 8 id: z.string() 9 9 }); 10 10
+1 -1
packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@hey-api/transformers/type-format-valibot/valibot.gen.ts
··· 4 4 5 5 export const vFoo = v.object({ 6 6 bar: v.optional(v.pipe(v.number(), v.integer())), 7 - foo: v.bigint(), 7 + foo: v.optional(v.bigint(), BigInt(0)), 8 8 id: v.string() 9 9 }); 10 10
+1 -1
packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/@hey-api/transformers/type-format-zod/zod.gen.ts
··· 4 4 5 5 export const zFoo = z.object({ 6 6 bar: z.number().int().optional(), 7 - foo: z.coerce.bigint(), 7 + foo: z.coerce.bigint().default(BigInt(0)), 8 8 id: z.string() 9 9 }); 10 10
+1 -1
packages/openapi-ts-tests/test/__snapshots__/3.1.x/plugins/@hey-api/transformers/type-format-valibot/valibot.gen.ts
··· 4 4 5 5 export const vFoo = v.object({ 6 6 bar: v.optional(v.pipe(v.number(), v.integer())), 7 - foo: v.bigint(), 7 + foo: v.optional(v.bigint(), BigInt(0)), 8 8 id: v.string() 9 9 }); 10 10
+1 -1
packages/openapi-ts-tests/test/__snapshots__/3.1.x/plugins/@hey-api/transformers/type-format-zod/zod.gen.ts
··· 4 4 5 5 export const zFoo = z.object({ 6 6 bar: z.number().int().optional(), 7 - foo: z.coerce.bigint(), 7 + foo: z.coerce.bigint().default(BigInt(0)), 8 8 id: z.string() 9 9 }); 10 10
+5 -6
packages/openapi-ts-tests/test/openapi-ts.config.ts
··· 51 51 // 'invalid', 52 52 // 'servers-entry.yaml', 53 53 // ), 54 - // path: path.resolve(__dirname, 'spec', '3.1.x', 'read-write-only.yaml'), 55 - path: '', 54 + path: path.resolve(__dirname, 'spec', '3.1.x', 'type-format.yaml'), 56 55 // path: 'http://localhost:4000/', 57 56 // path: 'https://get.heyapi.dev/', 58 57 // path: 'https://get.heyapi.dev/hey-api/backend?branch=main&version=1.0.0', ··· 115 114 // operationId: false, 116 115 // responseStyle: 'data', 117 116 // throwOnError: true, 118 - // transformer: '@hey-api/transformers', 117 + transformer: '@hey-api/transformers', 119 118 // transformer: true, 120 - // validator: 'zod', 119 + validator: 'zod', 121 120 }, 122 121 { 123 122 // bigInt: true, ··· 146 145 { 147 146 // comments: false, 148 147 // exportFromIndex: true, 149 - // name: 'valibot', 148 + name: 'valibot', 150 149 }, 151 150 { 152 151 // comments: false, 153 152 // exportFromIndex: true, 154 - // name: 'zod', 153 + name: 'zod', 155 154 }, 156 155 ], 157 156 // useOptions: false,
+1
packages/openapi-ts-tests/test/spec/2.0.x/type-format.yaml
··· 19 19 bar: 20 20 type: integer 21 21 foo: 22 + default: 0 22 23 format: int64 23 24 type: integer 24 25 id:
+1
packages/openapi-ts-tests/test/spec/3.0.x/type-format.yaml
··· 20 20 bar: 21 21 type: integer 22 22 foo: 23 + default: 0 23 24 format: int64 24 25 type: integer 25 26 id:
+1
packages/openapi-ts-tests/test/spec/3.1.x/type-format.yaml
··· 20 20 bar: 21 21 type: integer 22 22 foo: 23 + default: 0 23 24 format: int64 24 25 type: integer 25 26 id:
+10 -5
packages/openapi-ts/src/plugins/valibot/plugin.ts
··· 267 267 value, 268 268 }: { 269 269 isBigInt: boolean; 270 - value: number; 270 + value: unknown; 271 271 }) => { 272 272 const expression = compiler.valueToExpression({ value }); 273 273 274 - if (isBigInt) { 274 + if ( 275 + isBigInt && 276 + (typeof value === 'bigint' || 277 + typeof value === 'number' || 278 + typeof value === 'string' || 279 + typeof value === 'boolean') 280 + ) { 275 281 return compiler.callExpression({ 276 282 functionName: 'BigInt', 277 283 parameters: [expression], ··· 1104 1110 let callParameter: ts.Expression | undefined; 1105 1111 1106 1112 if (schema.default !== undefined) { 1107 - callParameter = compiler.valueToExpression({ 1108 - value: schema.default, 1109 - }); 1113 + const isBigInt = schema.type === 'integer' && schema.format === 'int64'; 1114 + callParameter = numberParameter({ isBigInt, value: schema.default }); 1110 1115 if (callParameter) { 1111 1116 pipes = [ 1112 1117 compiler.callExpression({
+11 -3
packages/openapi-ts/src/plugins/zod/plugin.ts
··· 268 268 value, 269 269 }: { 270 270 isBigInt: boolean; 271 - value: number; 271 + value: unknown; 272 272 }) => { 273 273 const expression = compiler.valueToExpression({ value }); 274 274 275 - if (isBigInt) { 275 + if ( 276 + isBigInt && 277 + (typeof value === 'bigint' || 278 + typeof value === 'number' || 279 + typeof value === 'string' || 280 + typeof value === 'boolean') 281 + ) { 276 282 return compiler.callExpression({ 277 283 functionName: 'BigInt', 278 284 parameters: [expression], ··· 1076 1082 } 1077 1083 1078 1084 if (schema.default !== undefined) { 1079 - const callParameter = compiler.valueToExpression({ 1085 + const isBigInt = schema.type === 'integer' && schema.format === 'int64'; 1086 + const callParameter = numberParameter({ 1087 + isBigInt, 1080 1088 value: schema.default, 1081 1089 }); 1082 1090 if (callParameter) {