Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ninety-swans-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript": patch
---

Prevents a `TypeError` due to a bad in operator for `type: "array"` when a `boolean` is set on `items`
12 changes: 11 additions & 1 deletion packages/openapi-typescript/src/transform/schema-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor
}
// standard array type
else if (schemaObject.items) {
if ("type" in schemaObject.items && schemaObject.items.type === "array") {
if (hasKey(schemaObject.items, "type") && schemaObject.items.type === "array") {
itemType = ts.factory.createArrayTypeNode(transformSchemaObject(schemaObject.items, options));
} else {
itemType = transformSchemaObject(schemaObject.items, options);
Expand Down Expand Up @@ -579,3 +579,13 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor

return coreObjectType.length ? ts.factory.createTypeLiteralNode(coreObjectType) : undefined;
}

/**
* Check if an object has a key
* @param possibleObject - The object to check
* @param key - The key to check for
* @returns True if the object has the key, false otherwise
*/
function hasKey<K extends string>(possibleObject: unknown, key: K): possibleObject is { [key in K]: unknown } {
return typeof possibleObject === "object" && possibleObject !== null && key in possibleObject;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ describe("transformSchemaObject > array", () => {
// options: DEFAULT_OPTIONS,
},
],
// Prevents: "TypeError: Cannot use 'in' operator to search for 'type' in true"
[
"boolean items",
{
given: { type: "array", items: true },
want: "unknown[]",
},
],
[
"tuple > tuple items",
{
Expand Down