Skip to content

fix: stricter binary operation operand type compatibility check #846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { isDataModelFieldReference } from '@zenstackhq/sdk';
import { ValidationAcceptor } from 'langium';
import { isAuthInvocation, isCollectionPredicate } from '../../utils/ast-utils';
import { AstValidator } from '../types';
import { typeAssignable } from './utils';

/**
* Validates expressions.
Expand Down Expand Up @@ -124,6 +125,28 @@ export default class ExpressionValidator implements AstValidator<Expression> {
break;
}

if (
(expr.left.$resolvedType?.nullable && isNullExpr(expr.right)) ||
(expr.right.$resolvedType?.nullable && isNullExpr(expr.left))
) {
// comparing nullable field with null
return;
}

if (
typeof expr.left.$resolvedType?.decl === 'string' &&
typeof expr.right.$resolvedType?.decl === 'string'
) {
// scalar types assignability
if (
!typeAssignable(expr.left.$resolvedType.decl, expr.right.$resolvedType.decl) &&
!typeAssignable(expr.right.$resolvedType.decl, expr.left.$resolvedType.decl)
) {
accept('error', 'incompatible operand types', { node: expr });
}
return;
}

// disallow comparing model type with scalar type or comparison between
// incompatible model types
const leftType = expr.left.$resolvedType?.decl;
Expand Down
8 changes: 7 additions & 1 deletion packages/schema/src/language-server/zmodel-linker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import {
} from 'langium';
import { match } from 'ts-pattern';
import { CancellationToken } from 'vscode-jsonrpc';
import { getAllDeclarationsFromImports } from '../utils/ast-utils';
import { getAllDeclarationsFromImports, isAuthInvocation } from '../utils/ast-utils';
import { mapBuiltinTypeToExpressionType } from './validator/utils';

interface DefaultReference extends Reference {
Expand Down Expand Up @@ -337,6 +337,12 @@ export class ZModelLinker extends DefaultLinker {
this.linkReference(node, 'member', document, [provider], true);
if (node.member.ref) {
this.resolveToDeclaredType(node, node.member.ref.type);

if (node.$resolvedType && isAuthInvocation(node.operand)) {
// member access on auth() function is nullable
// because user may not have provided all fields
node.$resolvedType.nullable = true;
}
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions tests/integration/tests/regression/issue-804.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { loadModelWithError } from '@zenstackhq/testtools';

describe('Regression: issue 804', () => {
it('regression', async () => {
expect(
await loadModelWithError(
`
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

model User {
id Int @id @default(autoincrement())
email Int
posts Post[]
}

model Post {
id Int @id @default(autoincrement())
author User? @relation(fields: [authorId], references: [id])
authorId Int
published Boolean

@@allow('all', auth().posts?[published] == 'TRUE')
}
`
)
).toContain('incompatible operand types');
});
});