Skip to content

Commit f7af6d4

Browse files
committed
fix: wrap arrayExpression in z.array()
1 parent 474bdb4 commit f7af6d4

File tree

3 files changed

+44
-25
lines changed
  • packages

3 files changed

+44
-25
lines changed

packages/openapi-ts-tests/test/__snapshots__/3.0.x/plugins/zod/default/zod.gen.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,14 @@ export const zArrayWithProperties = z.array(z.object({
170170
/**
171171
* This is a simple array with any of properties
172172
*/
173-
export const zArrayWithAnyOfProperties = z.union([
173+
export const zArrayWithAnyOfProperties = z.array(z.union([
174174
z.object({
175175
foo: z.string().optional().default('test')
176176
}),
177177
z.object({
178178
bar: z.string().optional()
179179
})
180-
]);
180+
]));
181181

182182
export const zAnyOfAnyAndNull = z.object({
183183
data: z.unknown().optional()
@@ -187,14 +187,14 @@ export const zAnyOfAnyAndNull = z.object({
187187
* This is a simple array with any of properties
188188
*/
189189
export const zAnyOfArrays = z.object({
190-
results: z.union([
190+
results: z.array(z.union([
191191
z.object({
192192
foo: z.string().optional()
193193
}),
194194
z.object({
195195
bar: z.string().optional()
196196
})
197-
]).optional()
197+
])).optional()
198198
});
199199

200200
/**
@@ -522,10 +522,10 @@ export const zConstValue = z.enum([
522522
*/
523523
export const zCompositionWithNestedAnyOfAndNull = z.object({
524524
propA: z.union([
525-
z.union([
525+
z.array(z.union([
526526
z3eNum1Период,
527527
zConstValue
528-
]),
528+
])),
529529
z.null()
530530
]).optional()
531531
});
@@ -748,10 +748,10 @@ export const zModelWithAdditionalPropertiesEqTrue = z.object({
748748

749749
export const zNestedAnyOfArraysNullable = z.object({
750750
nullableArray: z.union([
751-
z.union([
751+
z.array(z.union([
752752
z.string(),
753753
z.boolean()
754-
]),
754+
])),
755755
z.null()
756756
]).optional()
757757
});
@@ -853,11 +853,11 @@ export const zModelWithConstantSizeArray = z.unknown();
853853

854854
export const zModelWithAnyOfConstantSizeArray = z.unknown();
855855

856-
export const zModelWithPrefixItemsConstantSizeArray = z.union([
856+
export const zModelWithPrefixItemsConstantSizeArray = z.array(z.union([
857857
zModelWithInteger,
858858
z.number(),
859859
z.string()
860-
]);
860+
]));
861861

862862
export const zModelWithAnyOfConstantSizeArrayNullable = z.unknown();
863863

packages/openapi-ts-tests/test/__snapshots__/3.1.x/plugins/zod/default/zod.gen.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,14 @@ export const zArrayWithProperties = z.array(z.object({
170170
/**
171171
* This is a simple array with any of properties
172172
*/
173-
export const zArrayWithAnyOfProperties = z.union([
173+
export const zArrayWithAnyOfProperties = z.array(z.union([
174174
z.object({
175175
foo: z.string().optional().default('test')
176176
}),
177177
z.object({
178178
bar: z.string().optional()
179179
})
180-
]);
180+
]));
181181

182182
export const zAnyOfAnyAndNull = z.object({
183183
data: z.union([
@@ -190,14 +190,14 @@ export const zAnyOfAnyAndNull = z.object({
190190
* This is a simple array with any of properties
191191
*/
192192
export const zAnyOfArrays = z.object({
193-
results: z.union([
193+
results: z.array(z.union([
194194
z.object({
195195
foo: z.string().optional()
196196
}),
197197
z.object({
198198
bar: z.string().optional()
199199
})
200-
]).optional()
200+
])).optional()
201201
});
202202

203203
/**
@@ -500,14 +500,14 @@ export const zCompositionWithAnyOfAnonymous = z.object({
500500
*/
501501
export const zCompositionWithNestedAnyAndTypeNull = z.object({
502502
propA: z.union([
503-
z.union([
503+
z.array(z.union([
504504
zModelWithDictionary,
505505
z.null()
506-
]),
507-
z.union([
506+
])),
507+
z.array(z.union([
508508
zModelWithArray,
509509
z.null()
510-
])
510+
]))
511511
]).optional()
512512
});
513513

@@ -523,10 +523,10 @@ export const zConstValue = z.literal('ConstValue');
523523
*/
524524
export const zCompositionWithNestedAnyOfAndNull = z.object({
525525
propA: z.union([
526-
z.union([
526+
z.array(z.union([
527527
z3eNum1Период,
528528
zConstValue
529-
]),
529+
])),
530530
z.null()
531531
]).optional()
532532
});
@@ -745,10 +745,10 @@ export const zModelWithAdditionalPropertiesEqTrue = z.object({
745745

746746
export const zNestedAnyOfArraysNullable = z.object({
747747
nullableArray: z.union([
748-
z.union([
748+
z.array(z.union([
749749
z.string(),
750750
z.boolean()
751-
]),
751+
])),
752752
z.null()
753753
]).optional()
754754
});

packages/openapi-ts/src/plugins/zod/plugin.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,32 @@ const arrayTypeToZodSchema = ({
100100
arrayExpression = compiler.callExpression({
101101
functionName: compiler.propertyAccessExpression({
102102
expression: zIdentifier,
103-
name: unionIdentifier,
103+
name: compiler.identifier({ text: 'array' }),
104104
}),
105105
parameters: [
106-
compiler.arrayLiteralExpression({
107-
elements: itemExpressions,
106+
compiler.callExpression({
107+
functionName: compiler.propertyAccessExpression({
108+
expression: zIdentifier,
109+
name: unionIdentifier,
110+
}),
111+
parameters: [
112+
compiler.arrayLiteralExpression({
113+
elements: itemExpressions,
114+
}),
115+
],
108116
}),
109117
],
118+
119+
// compiler.callExpression({
120+
// functionName: compiler.propertyAccessExpression({
121+
// expression: zIdentifier,
122+
// name: unionIdentifier,
123+
// }),
124+
// parameters: [
125+
// compiler.arrayLiteralExpression({
126+
// elements: itemExpressions,
127+
// }),
128+
// ],
110129
});
111130
}
112131
}

0 commit comments

Comments
 (0)