From a90435df325f43d91c240eb86831f7643892d032 Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Fri, 10 May 2024 22:34:12 +0800 Subject: [PATCH 1/2] fix: relation fields are included even if they are set `false` in select clause --- .../src/enhancements/policy/policy-utils.ts | 4 ++ .../validator/expression-validator.ts | 2 +- tests/regression/tests/issue-1427.test.ts | 42 +++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 tests/regression/tests/issue-1427.test.ts diff --git a/packages/runtime/src/enhancements/policy/policy-utils.ts b/packages/runtime/src/enhancements/policy/policy-utils.ts index 2cddaae5e..d2cea30fa 100644 --- a/packages/runtime/src/enhancements/policy/policy-utils.ts +++ b/packages/runtime/src/enhancements/policy/policy-utils.ts @@ -653,6 +653,10 @@ export class PolicyUtil extends QueryUtils { const hoistedConditions: any[] = []; for (const field of getModelFields(injectTarget)) { + if (injectTarget[field] === false) { + continue; + } + const fieldInfo = resolveField(this.modelMeta, model, field); if (!fieldInfo || !fieldInfo.isDataModel) { // only care about relation fields diff --git a/packages/schema/src/language-server/validator/expression-validator.ts b/packages/schema/src/language-server/validator/expression-validator.ts index cb42e4cb1..d65e304dc 100644 --- a/packages/schema/src/language-server/validator/expression-validator.ts +++ b/packages/schema/src/language-server/validator/expression-validator.ts @@ -30,7 +30,7 @@ export default class ExpressionValidator implements AstValidator { // check was done at link time accept( 'error', - 'auth() cannot be resolved because no model marked wth "@@auth()" or named "User" is found', + 'auth() cannot be resolved because no model marked with "@@auth()" or named "User" is found', { node: expr } ); } else { diff --git a/tests/regression/tests/issue-1427.test.ts b/tests/regression/tests/issue-1427.test.ts new file mode 100644 index 000000000..0d7c7c07e --- /dev/null +++ b/tests/regression/tests/issue-1427.test.ts @@ -0,0 +1,42 @@ +import { loadSchema } from '@zenstackhq/testtools'; + +describe('issue 1427', () => { + it('regression', async () => { + const { prisma, enhance } = await loadSchema( + ` + model User { + id String @id @default(cuid()) + name String + profile Profile? + @@allow('all', true) + } + + model Profile { + id String @id @default(cuid()) + user User @relation(fields: [userId], references: [id]) + userId String @unique + @@allow('all', true) + } + ` + ); + + await prisma.user.create({ + data: { + name: 'John', + profile: { + create: {}, + }, + }, + }); + + const db = enhance(); + const found = await db.user.findFirst({ + select: { + id: true, + name: true, + profile: false, + }, + }); + expect(found.profile).toBeUndefined(); + }); +}); From d8c499e227f40d7304178261bf15fd47dcee3b5b Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Fri, 10 May 2024 22:49:49 +0800 Subject: [PATCH 2/2] fix test --- .../schema/tests/schema/validation/attribute-validation.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/schema/tests/schema/validation/attribute-validation.test.ts b/packages/schema/tests/schema/validation/attribute-validation.test.ts index aca9e2674..380836e21 100644 --- a/packages/schema/tests/schema/validation/attribute-validation.test.ts +++ b/packages/schema/tests/schema/validation/attribute-validation.test.ts @@ -1051,7 +1051,7 @@ describe('Attribute tests', () => { @@allow('all', auth() != null) } `) - ).toContain(`auth() cannot be resolved because no model marked wth "@@auth()" or named "User" is found`); + ).toContain(`auth() cannot be resolved because no model marked with "@@auth()" or named "User" is found`); await loadModel(` ${prelude}