Skip to content

Commit

Permalink
fix(zmodel): check cyclic inheritance (#1931)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Dec 31, 2024
1 parent f609c86 commit b477ad8
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export default class DataModelValidator implements AstValidator<DataModel> {
validateDuplicatedDeclarations(dm, getModelFieldsWithBases(dm), accept);
this.validateAttributes(dm, accept);
this.validateFields(dm, accept);

if (dm.superTypes.length > 0) {
this.validateInheritance(dm, accept);
}
}

private validateFields(dm: DataModel, accept: ValidationAcceptor) {
Expand Down Expand Up @@ -407,6 +411,26 @@ export default class DataModelValidator implements AstValidator<DataModel> {
});
}
}

private validateInheritance(dm: DataModel, accept: ValidationAcceptor) {
const seen = [dm];
const todo: DataModel[] = dm.superTypes.map((superType) => superType.ref!);
while (todo.length > 0) {
const current = todo.shift()!;
if (seen.includes(current)) {
accept(
'error',
`Circular inheritance detected: ${seen.map((m) => m.name).join(' -> ')} -> ${current.name}`,
{
node: dm,
}
);
return;
}
seen.push(current);
todo.push(...current.superTypes.map((superType) => superType.ref!));
}
}
}

export interface MissingOppositeRelationData {
Expand Down
39 changes: 39 additions & 0 deletions packages/schema/tests/schema/validation/cyclic-inheritance.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { loadModelWithError } from '../../utils';

describe('Cyclic inheritance', () => {
it('abstract inheritance', async () => {
const errors = await loadModelWithError(
`
abstract model A extends B {}
abstract model B extends A {}
model C extends B {
id Int @id
}
`
);
expect(errors).toContain('Circular inheritance detected: A -> B -> A');
expect(errors).toContain('Circular inheritance detected: B -> A -> B');
expect(errors).toContain('Circular inheritance detected: C -> B -> A -> B');
});

it('delegate inheritance', async () => {
const errors = await loadModelWithError(
`
model A extends B {
typeA String
@@delegate(typeA)
}
model B extends A {
typeB String
@@delegate(typeB)
}
model C extends B {
id Int @id
}
`
);
expect(errors).toContain('Circular inheritance detected: A -> B -> A');
expect(errors).toContain('Circular inheritance detected: B -> A -> B');
expect(errors).toContain('Circular inheritance detected: C -> B -> A -> B');
});
});
12 changes: 10 additions & 2 deletions packages/sdk/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,16 +544,24 @@ export function getModelFieldsWithBases(model: DataModel, includeDelegate = true
}
}

export function getRecursiveBases(dataModel: DataModel, includeDelegate = true): DataModel[] {
export function getRecursiveBases(
dataModel: DataModel,
includeDelegate = true,
seen = new Set<DataModel>()
): DataModel[] {
const result: DataModel[] = [];
if (seen.has(dataModel)) {
return result;
}
seen.add(dataModel);
dataModel.superTypes.forEach((superType) => {
const baseDecl = superType.ref;
if (baseDecl) {
if (!includeDelegate && isDelegateModel(baseDecl)) {
return;
}
result.push(baseDecl);
result.push(...getRecursiveBases(baseDecl, includeDelegate));
result.push(...getRecursiveBases(baseDecl, includeDelegate, seen));
}
});
return result;
Expand Down

0 comments on commit b477ad8

Please sign in to comment.