Skip to content

Commit 7205e12

Browse files
authored
fix(openapi-typescript): prevent type error when items is boolean (#2260)
* fix(openapi-typescript): prevent type error when items is boolean * fix: add EOL * fix: linting * add changeset
1 parent b8f1a00 commit 7205e12

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

.changeset/ninety-swans-kiss.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-typescript": patch
3+
---
4+
5+
Prevents a `TypeError` due to a bad in operator for `type: "array"` when a `boolean` is set on `items`

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor
322322
}
323323
// standard array type
324324
else if (schemaObject.items) {
325-
if ("type" in schemaObject.items && schemaObject.items.type === "array") {
325+
if (hasKey(schemaObject.items, "type") && schemaObject.items.type === "array") {
326326
itemType = ts.factory.createArrayTypeNode(transformSchemaObject(schemaObject.items, options));
327327
} else {
328328
itemType = transformSchemaObject(schemaObject.items, options);
@@ -579,3 +579,13 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor
579579

580580
return coreObjectType.length ? ts.factory.createTypeLiteralNode(coreObjectType) : undefined;
581581
}
582+
583+
/**
584+
* Check if an object has a key
585+
* @param possibleObject - The object to check
586+
* @param key - The key to check for
587+
* @returns True if the object has the key, false otherwise
588+
*/
589+
function hasKey<K extends string>(possibleObject: unknown, key: K): possibleObject is { [key in K]: unknown } {
590+
return typeof possibleObject === "object" && possibleObject !== null && key in possibleObject;
591+
}

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

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ describe("transformSchemaObject > array", () => {
1818
// options: DEFAULT_OPTIONS,
1919
},
2020
],
21+
// Prevents: "TypeError: Cannot use 'in' operator to search for 'type' in true"
22+
[
23+
"boolean items",
24+
{
25+
given: { type: "array", items: true },
26+
want: "unknown[]",
27+
},
28+
],
2129
[
2230
"tuple > tuple items",
2331
{

0 commit comments

Comments
 (0)