Skip to content

Commit 67ed1b7

Browse files
authored
fix(openapi-typescript):transformSchemaObject support two-dimensional… (#1489)
* fix(openapi-typescript):transformSchemaObject support two-dimensional array * chore(openapi-typescript): format prettier
1 parent 8612b08 commit 67ed1b7

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

packages/openapi-typescript/src/transform/schema-object.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,16 @@ function transformSchemaObjectCore(
302302
}
303303
// standard array type
304304
else if (schemaObject.items) {
305-
itemType = transformSchemaObject(schemaObject.items, options);
305+
if (
306+
"type" in schemaObject.items &&
307+
schemaObject.items.type === "array"
308+
) {
309+
itemType = ts.factory.createArrayTypeNode(
310+
transformSchemaObject(schemaObject.items, options),
311+
);
312+
} else {
313+
itemType = transformSchemaObject(schemaObject.items, options);
314+
}
306315
}
307316

308317
const min: number =
@@ -350,9 +359,9 @@ function transformSchemaObjectCore(
350359
}
351360
}
352361

353-
return ts.isTupleTypeNode(itemType)
362+
return ts.isTupleTypeNode(itemType) || ts.isArrayTypeNode(itemType)
354363
? itemType
355-
: ts.factory.createArrayTypeNode(itemType); // wrap itemType in array type, but only if not a tuple already
364+
: ts.factory.createArrayTypeNode(itemType); // wrap itemType in array type, but only if not a tuple or array already
356365
}
357366

358367
// polymorphic, or 3.1 nullable

packages/openapi-typescript/test/transform/schema-object/object.test.ts

+36
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,42 @@ describe("transformSchemaObject > object", () => {
337337
},
338338
},
339339
],
340+
[
341+
"options > two-dimensional array",
342+
{
343+
given: {
344+
type: "object",
345+
properties: {
346+
array: {
347+
type: "array",
348+
items: {
349+
items: [
350+
{
351+
type: "string",
352+
},
353+
{
354+
type: "boolean",
355+
},
356+
],
357+
type: "array",
358+
maxItems: 2,
359+
minItems: 2,
360+
},
361+
},
362+
},
363+
},
364+
want: `{
365+
array?: [
366+
string,
367+
boolean
368+
][];
369+
}`,
370+
options: {
371+
...DEFAULT_OPTIONS,
372+
ctx: { ...DEFAULT_OPTIONS.ctx },
373+
},
374+
},
375+
],
340376
];
341377

342378
for (const [

0 commit comments

Comments
 (0)