Skip to content

Commit

Permalink
fix: undefined field access when selecting with _count (zenstackhq#403)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored May 7, 2023
1 parent 66269ce commit d90d7c8
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zenstack-monorepo",
"version": "1.0.0-alpha.114",
"version": "1.0.0-alpha.115",
"description": "",
"scripts": {
"build": "pnpm -r build",
Expand Down
2 changes: 1 addition & 1 deletion packages/language/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/language",
"version": "1.0.0-alpha.114",
"version": "1.0.0-alpha.115",
"displayName": "ZenStack modeling language compiler",
"description": "ZenStack modeling language compiler",
"homepage": "https://zenstack.dev",
Expand Down
2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/next",
"version": "1.0.0-alpha.114",
"version": "1.0.0-alpha.115",
"displayName": "ZenStack Next.js integration",
"description": "ZenStack Next.js integration",
"homepage": "https://zenstack.dev",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/openapi/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/openapi",
"displayName": "ZenStack Plugin and Runtime for OpenAPI",
"version": "1.0.0-alpha.114",
"version": "1.0.0-alpha.115",
"description": "ZenStack plugin and runtime supporting OpenAPI",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/react/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/react",
"displayName": "ZenStack plugin and runtime for ReactJS",
"version": "1.0.0-alpha.114",
"version": "1.0.0-alpha.115",
"description": "ZenStack plugin and runtime for ReactJS",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/trpc/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/trpc",
"displayName": "ZenStack plugin for tRPC",
"version": "1.0.0-alpha.114",
"version": "1.0.0-alpha.115",
"description": "ZenStack plugin for tRPC",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/runtime",
"displayName": "ZenStack Runtime Library",
"version": "1.0.0-alpha.114",
"version": "1.0.0-alpha.115",
"description": "Runtime of ZenStack for both client-side and server-side environments.",
"repository": {
"type": "git",
Expand Down
26 changes: 14 additions & 12 deletions packages/runtime/src/enhancements/policy/policy-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,20 +345,22 @@ export class PolicyUtil {
}

const fieldInfo = resolveField(this.modelMeta, model, field);
if (fieldInfo && fieldInfo.isDataModel && !fieldInfo.isArray) {
// to-one relation data cannot be trimmed by injected guards, we have to
// post-check them
const ids = this.getEntityIds(fieldInfo.type, fieldData);

if (Object.keys(ids).length !== 0) {
// DEBUG
// this.logger.info(`Validating read of to-one relation: ${fieldInfo.type}#${formatObject(ids)}`);
await this.checkPolicyForFilter(fieldInfo.type, ids, operation, this.db);
if (fieldInfo) {
if (fieldInfo.isDataModel && !fieldInfo.isArray) {
// to-one relation data cannot be trimmed by injected guards, we have to
// post-check them
const ids = this.getEntityIds(fieldInfo.type, fieldData);

if (Object.keys(ids).length !== 0) {
// DEBUG
// this.logger.info(`Validating read of to-one relation: ${fieldInfo.type}#${formatObject(ids)}`);
await this.checkPolicyForFilter(fieldInfo.type, ids, operation, this.db);
}
}
}

// recurse
await this.postProcessForRead(fieldData, fieldInfo.type, injectTarget[field], operation);
// recurse
await this.postProcessForRead(fieldData, fieldInfo.type, injectTarget[field], operation);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publisher": "zenstack",
"displayName": "ZenStack Language Tools",
"description": "A toolkit for building secure CRUD apps with Next.js + Typescript",
"version": "1.0.0-alpha.114",
"version": "1.0.0-alpha.115",
"author": {
"name": "ZenStack Team"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/sdk",
"version": "1.0.0-alpha.114",
"version": "1.0.0-alpha.115",
"description": "ZenStack plugin development SDK",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/server",
"version": "1.0.0-alpha.114",
"version": "1.0.0-alpha.115",
"displayName": "ZenStack Server-side Adapters",
"description": "ZenStack server-side adapters",
"homepage": "https://zenstack.dev",
Expand Down
5 changes: 3 additions & 2 deletions packages/server/src/openapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ export async function handleRequest({
},
};
} else {
logError(logger, (err as Error).message);
const _err = err as Error;
logError(logger, _err.message + (_err.stack ? '\n' + _err.stack : ''));
return {
status: 400,
body: {
Expand All @@ -238,7 +239,7 @@ function unmarshal(value: string) {

function logError(logger: LoggerConfig | undefined | null, message: string, code?: string) {
if (logger === undefined) {
console.error(`@zenstackhq/openapi: error ${code ? '[' + code + ']' : ''}, ${message}`);
console.error(`@zenstackhq/server: error ${code ? '[' + code + ']' : ''}, ${message}`);
} else if (logger?.error) {
logger.error(message, code);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/testtools/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/testtools",
"version": "1.0.0-alpha.114",
"version": "1.0.0-alpha.115",
"description": "ZenStack Test Tools",
"main": "index.js",
"publishConfig": {
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test-run/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions tests/integration/tests/regression/issues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,39 @@ describe('GitHub issues regression', () => {
`
);
});

it('select with _count', async () => {
const { prisma, withPolicy } = await loadSchema(
`
model User {
id String @id @unique @default(uuid())
posts Post[]
@@allow('all', true)
}
model Post {
id String @id @default(uuid())
title String
published Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId String
@@allow('all', true)
}
`
);

await prisma.user.create({
data: {
posts: {
create: [{ title: 'Post 1' }, { title: 'Post 2' }],
},
},
});

const db = withPolicy();
const r = await db.user.findFirst({ select: { _count: { select: { posts: true } } } });
expect(r).toMatchObject({ _count: { posts: 2 } });
});
});

0 comments on commit d90d7c8

Please sign in to comment.