From 47687bd4f767bd821a40f69151dfe278c709b5b2 Mon Sep 17 00:00:00 2001 From: Keath Pavlenko Date: Sat, 9 May 2026 12:23:13 -0700 Subject: [PATCH] fix: detect policy plugin by stable id --- .../orm/src/client/crud/operations/base.ts | 2 +- .../test/policy-plugin-detection.test.ts | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 tests/regression/test/policy-plugin-detection.test.ts diff --git a/packages/orm/src/client/crud/operations/base.ts b/packages/orm/src/client/crud/operations/base.ts index 1190be056..d96718765 100644 --- a/packages/orm/src/client/crud/operations/base.ts +++ b/packages/orm/src/client/crud/operations/base.ts @@ -221,7 +221,7 @@ export abstract class BaseOperationHandler { // TODO: this is not clean, needs a better solution protected get hasPolicyEnabled() { - return this.options.plugins?.some((plugin) => plugin.constructor.name === 'PolicyPlugin'); + return this.options.plugins?.some((plugin) => plugin.id === 'policy') ?? false; } protected requireModel(model: string) { diff --git a/tests/regression/test/policy-plugin-detection.test.ts b/tests/regression/test/policy-plugin-detection.test.ts new file mode 100644 index 000000000..48ce80b7c --- /dev/null +++ b/tests/regression/test/policy-plugin-detection.test.ts @@ -0,0 +1,29 @@ +import { PolicyPlugin } from '@zenstackhq/plugin-policy'; +import { createTestClient } from '@zenstackhq/testtools'; +import { describe, expect, it } from 'vitest'; + +describe('PolicyPlugin detection', () => { + it('uses plugin id when constructor names are bundled or minified', async () => { + const MinifiedPolicyPlugin = class a extends PolicyPlugin {}; + const plugin = new MinifiedPolicyPlugin(); + + expect(plugin.id).toBe('policy'); + expect(plugin.constructor.name).toBe('a'); + + const db = await createTestClient( + ` +model User { + id String @id + name String + + @@allow('all', true) +} + `, + { plugins: [plugin] }, + ); + + await db.user.create({ data: { id: 'u1', name: 'User 1' } }); + + await expect(db.user.delete({ where: { id: 'u1' } })).resolves.toEqual({ id: 'u1', name: 'User 1' }); + }); +});