Skip to content

Commit

Permalink
fix: auth() field from a base model is not properly resolved (#1192)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Mar 30, 2024
1 parent 1b2f48e commit 0f558cb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
15 changes: 2 additions & 13 deletions packages/schema/src/language-server/zmodel-linker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,26 +97,15 @@ export class ZModelLinker extends DefaultLinker {
container: AstNode,
property: string,
document: LangiumDocument,
extraScopes: ScopeProvider[],
onlyFromExtraScopes = false
extraScopes: ScopeProvider[]
) {
if (this.resolveFromScopeProviders(container, property, document, extraScopes)) {
return;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const reference: DefaultReference = (container as any)[property];

if (onlyFromExtraScopes) {
// if reference is not resolved from explicit scope providers and automatic linking is not allowed,
// we should explicitly create a linking error
reference._ref = this.createLinkingError({ reference, container, property });

// Add the reference to the document's array of references
document.references.push(reference);
} else {
this.doLink({ reference, container, property }, document);
}
this.doLink({ reference, container, property }, document);
}

//#endregion
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/src/language-server/zmodel-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export class ZModelScopeProvider extends DefaultScopeProvider {
if (model) {
const authModel = getAuthModel(getDataModels(model, true));
if (authModel) {
return this.createScopeForNodes(authModel.fields, globalScope);
return this.createScopeForModel(authModel, globalScope);
}
}
return EMPTY_SCOPE;
Expand Down
27 changes: 27 additions & 0 deletions tests/integration/tests/regression/issue-1179.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { loadModel } from '@zenstackhq/testtools';

describe('issue 1179', () => {
it('regression', async () => {
await loadModel(
`
abstract model Base {
id String @id @default(uuid())
}
model User extends Base {
email String
posts Post[]
@@allow('all', auth() == this)
}
model Post {
id String @id @default(uuid())
user User @relation(fields: [userId], references: [id])
userId String
@@allow('all', auth().id == userId)
}
`
);
});
});

0 comments on commit 0f558cb

Please sign in to comment.