Skip to content

Commit

Permalink
api/schema: Fix TypeScript generation for AI types
Browse files Browse the repository at this point in the history
Gotta remove the title or it will create duplicate identifiers
for each field.
  • Loading branch information
victorges committed Sep 13, 2024
1 parent 0be0ef2 commit f46b46c
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion packages/api/src/schema/compile-schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit f46b46c

Please sign in to comment.