Skip to content

Commit

Permalink
fix: relation fields are included even if they are set false in sel…
Browse files Browse the repository at this point in the history
…ect clause (#1429)
  • Loading branch information
ymc9 committed May 10, 2024
1 parent cb50826 commit 6a71742
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/runtime/src/enhancements/policy/policy-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class ExpressionValidator implements AstValidator<Expression> {
// 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
42 changes: 42 additions & 0 deletions tests/regression/tests/issue-1427.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit 6a71742

Please sign in to comment.