Skip to content

Commit

Permalink
merge duplicate child schemas into one (#1507)
Browse files Browse the repository at this point in the history
  • Loading branch information
lnkarma authored Jul 7, 2024
1 parent 648fc6c commit 6b9eaf9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
10 changes: 10 additions & 0 deletions packages/core/src/generators/schema-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ export const generateSchemasDefinition = (
}
}
} else {
resolvedValue.schemas = resolvedValue.schemas.filter((schema) => {
if (schema.name !== schemaName) {
return true;
}

output += `${schema.model}\n`;
imports = imports.concat(schema.imports);

return false;
});
output += `export type ${schemaName} = ${resolvedValue.value};\n`;
}

Expand Down
46 changes: 27 additions & 19 deletions packages/core/src/getters/combine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,36 +157,31 @@ export const combineSchemas = ({

const isAllEnums = resolvedData.isEnum.every((v) => v);

let resolvedValue: ScalarValue | undefined;

if (schema.properties) {
resolvedValue = getScalar({ item: omit(schema, separator), name, context });
}

const value = combineValues({
resolvedData,
separator,
resolvedValue,
context,
});

if (isAllEnums && name && items.length > 1) {
const newEnum = `// eslint-disable-next-line @typescript-eslint/no-redeclare\nexport const ${pascal(
name,
)} = ${getCombineEnumValue(resolvedData)}`;

return {
value: `typeof ${pascal(name)}[keyof typeof ${pascal(name)}] ${nullable}\n\n${newEnum}`,
value: `typeof ${pascal(name)}[keyof typeof ${pascal(name)}] ${nullable}`,
imports: [
{
name: pascal(name),
},
...resolvedData.imports.map<GeneratorImport>((toImport) => ({
...toImport,
values: true,
})),
],
schemas: [...resolvedData.schemas],
schemas: [
...resolvedData.schemas,
{
imports: [
...resolvedData.imports.map<GeneratorImport>((toImport) => ({
...toImport,
values: true,
})),
],
model: newEnum,
name: name,
},
],
isEnum: false,
type: 'object' as SchemaType,
isRef: false,
Expand All @@ -196,6 +191,19 @@ export const combineSchemas = ({
};
}

let resolvedValue: ScalarValue | undefined;

if (schema.properties) {
resolvedValue = getScalar({ item: omit(schema, separator), name, context });
}

const value = combineValues({
resolvedData,
separator,
resolvedValue,
context,
});

return {
value: value + nullable,
imports: resolvedValue
Expand Down

0 comments on commit 6b9eaf9

Please sign in to comment.