From f46b46c41d572be256f95881f3d04e589cabb5c4 Mon Sep 17 00:00:00 2001 From: Victor Elias Date: Fri, 13 Sep 2024 20:05:29 -0300 Subject: [PATCH] api/schema: Fix TypeScript generation for AI types Gotta remove the title or it will create duplicate identifiers for each field. --- packages/api/src/schema/compile-schemas.js | 28 +++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/api/src/schema/compile-schemas.js b/packages/api/src/schema/compile-schemas.js index 72977a4ff..43fe5c2c3 100644 --- a/packages/api/src/schema/compile-schemas.js +++ b/packages/api/src/schema/compile-schemas.js @@ -24,6 +24,31 @@ const write = (dir, data) => { console.log(`wrote ${dir}`); }; +// Remove the title from the schema to avoid conflicts with the TypeScript type name +function removeAllTitles(schema) { + if (schema.title) { + delete schema.title; + } + + if (schema.properties) { + for (const key in schema.properties) { + if (schema.properties[key]) { + schema.properties[key] = removeAllTitles(schema.properties[key]); + } + } + } + + if (schema.items) { + if (Array.isArray(schema.items)) { + schema.items = schema.items.map((item) => removeAllTitles(item)); + } else { + schema.items = removeAllTitles(schema.items); + } + } + + return schema; +} + const schemaDir = path.resolve(__dirname, "."); process.chdir(schemaDir); @@ -64,7 +89,8 @@ const data = _.merge({}, ...subSchemas); const index = []; let types = []; - for (const [name, schema] of Object.entries(data.components.schemas)) { + for (let [name, schema] of Object.entries(data.components.schemas)) { + schema = removeAllTitles(schema); schema.title = name; const type = await generateTypes(schema); types.push(type);