Skip to content

Commit b477ad8

Browse files
authored
fix(zmodel): check cyclic inheritance (#1931)
1 parent f609c86 commit b477ad8

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

packages/schema/src/language-server/validator/datamodel-validator.ts

+24
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ export default class DataModelValidator implements AstValidator<DataModel> {
3333
validateDuplicatedDeclarations(dm, getModelFieldsWithBases(dm), accept);
3434
this.validateAttributes(dm, accept);
3535
this.validateFields(dm, accept);
36+
37+
if (dm.superTypes.length > 0) {
38+
this.validateInheritance(dm, accept);
39+
}
3640
}
3741

3842
private validateFields(dm: DataModel, accept: ValidationAcceptor) {
@@ -407,6 +411,26 @@ export default class DataModelValidator implements AstValidator<DataModel> {
407411
});
408412
}
409413
}
414+
415+
private validateInheritance(dm: DataModel, accept: ValidationAcceptor) {
416+
const seen = [dm];
417+
const todo: DataModel[] = dm.superTypes.map((superType) => superType.ref!);
418+
while (todo.length > 0) {
419+
const current = todo.shift()!;
420+
if (seen.includes(current)) {
421+
accept(
422+
'error',
423+
`Circular inheritance detected: ${seen.map((m) => m.name).join(' -> ')} -> ${current.name}`,
424+
{
425+
node: dm,
426+
}
427+
);
428+
return;
429+
}
430+
seen.push(current);
431+
todo.push(...current.superTypes.map((superType) => superType.ref!));
432+
}
433+
}
410434
}
411435

412436
export interface MissingOppositeRelationData {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { loadModelWithError } from '../../utils';
2+
3+
describe('Cyclic inheritance', () => {
4+
it('abstract inheritance', async () => {
5+
const errors = await loadModelWithError(
6+
`
7+
abstract model A extends B {}
8+
abstract model B extends A {}
9+
model C extends B {
10+
id Int @id
11+
}
12+
`
13+
);
14+
expect(errors).toContain('Circular inheritance detected: A -> B -> A');
15+
expect(errors).toContain('Circular inheritance detected: B -> A -> B');
16+
expect(errors).toContain('Circular inheritance detected: C -> B -> A -> B');
17+
});
18+
19+
it('delegate inheritance', async () => {
20+
const errors = await loadModelWithError(
21+
`
22+
model A extends B {
23+
typeA String
24+
@@delegate(typeA)
25+
}
26+
model B extends A {
27+
typeB String
28+
@@delegate(typeB)
29+
}
30+
model C extends B {
31+
id Int @id
32+
}
33+
`
34+
);
35+
expect(errors).toContain('Circular inheritance detected: A -> B -> A');
36+
expect(errors).toContain('Circular inheritance detected: B -> A -> B');
37+
expect(errors).toContain('Circular inheritance detected: C -> B -> A -> B');
38+
});
39+
});

packages/sdk/src/utils.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -544,16 +544,24 @@ export function getModelFieldsWithBases(model: DataModel, includeDelegate = true
544544
}
545545
}
546546

547-
export function getRecursiveBases(dataModel: DataModel, includeDelegate = true): DataModel[] {
547+
export function getRecursiveBases(
548+
dataModel: DataModel,
549+
includeDelegate = true,
550+
seen = new Set<DataModel>()
551+
): DataModel[] {
548552
const result: DataModel[] = [];
553+
if (seen.has(dataModel)) {
554+
return result;
555+
}
556+
seen.add(dataModel);
549557
dataModel.superTypes.forEach((superType) => {
550558
const baseDecl = superType.ref;
551559
if (baseDecl) {
552560
if (!includeDelegate && isDelegateModel(baseDecl)) {
553561
return;
554562
}
555563
result.push(baseDecl);
556-
result.push(...getRecursiveBases(baseDecl, includeDelegate));
564+
result.push(...getRecursiveBases(baseDecl, includeDelegate, seen));
557565
}
558566
});
559567
return result;

0 commit comments

Comments
 (0)