Skip to content

Commit da33881

Browse files
authored
fix: Zmodel linker doesn't recursively visit base types when building resolution scopes (#992)
1 parent fa0dafb commit da33881

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

packages/schema/src/language-server/zmodel-linker.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -519,12 +519,17 @@ export class ZModelLinker extends DefaultLinker {
519519

520520
private resolveDataModel(node: DataModel, document: LangiumDocument<AstNode>, extraScopes: ScopeProvider[]) {
521521
if (node.superTypes.length > 0) {
522-
const providers = node.superTypes.map(
523-
(superType) => (name: string) => superType.ref?.fields.find((f) => f.name === name)
524-
);
525-
extraScopes = [...providers, ...extraScopes];
522+
const superTypeProviders: ScopeProvider[] = [];
523+
// build scope providers for super types recursively with breadth-first search
524+
const queue = node.superTypes.map((t) => t.ref!);
525+
while (queue.length > 0) {
526+
const superType = queue.shift()!;
527+
const provider = (name: string) => superType.fields.find((f) => f.name === name);
528+
superTypeProviders.push(provider);
529+
queue.push(...superType.superTypes.map((t) => t.ref!));
530+
}
531+
extraScopes = [...superTypeProviders, ...extraScopes];
526532
}
527-
528533
return this.resolveDefault(node, document, extraScopes);
529534
}
530535

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { loadSchema } from '@zenstackhq/testtools';
2+
3+
describe('Regression: issue 971', () => {
4+
it('regression', async () => {
5+
await loadSchema(
6+
`
7+
abstract model Level1 {
8+
id String @id @default(cuid())
9+
URL String?
10+
@@validate(URL != null, "URL must be provided") // works
11+
}
12+
abstract model Level2 extends Level1 {
13+
@@validate(URL != null, "URL must be provided") // works
14+
}
15+
abstract model Level3 extends Level2 {
16+
@@validate(URL != null, "URL must be provided") // doesn't work
17+
}
18+
model Foo extends Level3 {
19+
}
20+
`
21+
);
22+
});
23+
});

0 commit comments

Comments
 (0)