Skip to content

Commit

Permalink
fix(openapi-typescript):transformSchemaObject support two-dimensional… (
Browse files Browse the repository at this point in the history
#1489)

* fix(openapi-typescript):transformSchemaObject support two-dimensional array

* chore(openapi-typescript): format prettier
  • Loading branch information
liangskyli authored Dec 24, 2023
1 parent 8612b08 commit 67ed1b7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/openapi-typescript/src/transform/schema-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,16 @@ function transformSchemaObjectCore(
}
// standard array type
else if (schemaObject.items) {
itemType = transformSchemaObject(schemaObject.items, options);
if (
"type" in schemaObject.items &&
schemaObject.items.type === "array"
) {
itemType = ts.factory.createArrayTypeNode(
transformSchemaObject(schemaObject.items, options),
);
} else {
itemType = transformSchemaObject(schemaObject.items, options);
}
}

const min: number =
Expand Down Expand Up @@ -350,9 +359,9 @@ function transformSchemaObjectCore(
}
}

return ts.isTupleTypeNode(itemType)
return ts.isTupleTypeNode(itemType) || ts.isArrayTypeNode(itemType)
? itemType
: ts.factory.createArrayTypeNode(itemType); // wrap itemType in array type, but only if not a tuple already
: ts.factory.createArrayTypeNode(itemType); // wrap itemType in array type, but only if not a tuple or array already
}

// polymorphic, or 3.1 nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,42 @@ describe("transformSchemaObject > object", () => {
},
},
],
[
"options > two-dimensional array",
{
given: {
type: "object",
properties: {
array: {
type: "array",
items: {
items: [
{
type: "string",
},
{
type: "boolean",
},
],
type: "array",
maxItems: 2,
minItems: 2,
},
},
},
},
want: `{
array?: [
string,
boolean
][];
}`,
options: {
...DEFAULT_OPTIONS,
ctx: { ...DEFAULT_OPTIONS.ctx },
},
},
],
];

for (const [
Expand Down

0 comments on commit 67ed1b7

Please sign in to comment.