-
-
Notifications
You must be signed in to change notification settings - Fork 150
fix(policy): do not treat Json kind keys as Kysely nodes #2809
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
Open
BetterAndBetterII
wants to merge
1
commit into
zenstackhq:dev
Choose a base branch
from
BetterAndBetterII:fix/json-kind-create-policy
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import { createPolicyTestClient } from '@zenstackhq/testtools'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| // https://github.com/zenstackhq/zenstack/issues/2791 | ||
| // Policy pre-create checks unwrap insert values and used to treat any object with a | ||
| // `kind` key as a Kysely operation node. A Json payload like `{ kind: "artwork" }` | ||
| // then failed with `Invariant failed: expecting a ValueNode`. | ||
| const schema = ` | ||
| model User { | ||
| id Int @id @default(autoincrement()) | ||
| } | ||
|
|
||
| model Item { | ||
| id Int @id @default(autoincrement()) | ||
| payload Json | ||
|
|
||
| // non-constant: constant @@allow('all', true) skips pre-create value unwrapping | ||
| @@allow('all', auth() == null) | ||
| } | ||
| `; | ||
|
|
||
| describe('Regression for issue 2791', () => { | ||
| it('creates a Json value that has a top-level kind key', async () => { | ||
| const db = await createPolicyTestClient(schema, { provider: 'postgresql' }); | ||
|
|
||
| await expect(db.item.create({ data: { payload: { kind: 'artwork' } } })).resolves.toMatchObject({ | ||
| payload: { kind: 'artwork' }, | ||
| }); | ||
| }); | ||
|
|
||
| it('creates many Json values that have a top-level kind key', async () => { | ||
| const db = await createPolicyTestClient(schema, { provider: 'postgresql' }); | ||
|
|
||
| await expect( | ||
| db.item.createMany({ data: [{ payload: { kind: 'artwork' } }, { payload: { kind: 'photo' } }] }), | ||
| ).resolves.toMatchObject({ count: 2 }); | ||
|
|
||
| await expect(db.item.findMany()).resolves.toEqual( | ||
| expect.arrayContaining([ | ||
| expect.objectContaining({ payload: { kind: 'artwork' } }), | ||
| expect.objectContaining({ payload: { kind: 'photo' } }), | ||
| ]), | ||
| ); | ||
| }); | ||
|
|
||
| it('still accepts the same Json payload via update', async () => { | ||
| const db = await createPolicyTestClient(schema, { provider: 'postgresql' }); | ||
|
|
||
| const item = await db.item.create({ data: { payload: {} } }); | ||
| await expect( | ||
| db.item.update({ where: { id: item.id }, data: { payload: { kind: 'artwork' } } }), | ||
| ).resolves.toMatchObject({ payload: { kind: 'artwork' } }); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
🧩 Analysis chain
🏁 Script executed:
Repository: zenstackhq/zenstack
Length of output: 19017
🏁 Script executed:
Repository: zenstackhq/zenstack
Length of output: 47627
🏁 Script executed:
Repository: zenstackhq/zenstack
Length of output: 17084
🌐 Web query:
Kysely source parseValueExpression ValueNode.create insert values operation node💡 Result:
In Kysely, the
parseValueExpressionfunction andValueNode.createmethod are core components of the library's internal query parsing architecture, responsible for converting user-provided values into operation nodes that can be compiled into SQL [1][2][3]. TheparseValueExpressionfunction, located insrc/parser/value-parser.ts, acts as a dispatcher [1][4]. It examines the input expression: if the input is an expression or factory (such as a subquery, raw SQL, or a builder function), it callsparseExpressionto handle it; otherwise, it treats the input as a literal value and invokesValueNode.create(exp)to wrap it into aValueNode[1][2][3]. Regarding INSERT operations, theInsertQueryBuilderuses an internal utility, typicallyparseInsertExpression, to process the object passed to the.values()method [5]. This utility parses the input object to separate column names from values [5]. The values themselves are often passed through parsers that utilizeparseValueExpression(orparseValueExpressionOrListfor arrays) to ensure that each provided value—whether it is a simple primitive, a column reference, or a complex expression—is correctly converted into the correspondingOperationNoderequired by theInsertQueryNode[1][2][5]. TheValueNodeitself is a low-level operation node that represents a parameter or literal value in the generated SQL [1][6]. WhileValueNode.createis used for general values,ValueNode.createImmediateis specifically used for values that should be treated as immediate, safe literals rather than parameters, often used in specific internal operations like binary comparison operators [1][6]. TheInsertQueryNodeinterface includes avaluesproperty of typeOperationNode, which holds the structured representation of the data to be inserted once parsing is complete [7][8].Citations:
🏁 Script executed:
Repository: zenstackhq/zenstack
Length of output: 10198
🏁 Script executed:
Repository: zenstackhq/zenstack
Length of output: 622
🏁 Script executed:
Repository: zenstackhq/zenstack
Length of output: 622
🏁 Script executed:
Repository: zenstackhq/zenstack
Length of output: 50375
🏁 Script executed:
Repository: zenstackhq/zenstack
Length of output: 19322
🏁 Script executed:
Repository: zenstackhq/zenstack
Length of output: 9459
🏁 Script executed:
Repository: zenstackhq/zenstack
Length of output: 13315
🏁 Script executed:
Repository: zenstackhq/zenstack
Length of output: 24869
🌐 Web query:
Kysely 0.29.0 PrimitiveValueListNode create parseInsertExpression source values raw unknown💡 Result:
In Kysely, PrimitiveValueListNode is an internal operation node designed as a performance optimization [1][2]. While standard query values are represented by ValueNode objects, PrimitiveValueListNode stores an array of primitive values directly [1][2]. This avoids the overhead of creating individual ValueNode objects for each item in large lists, such as those used in a WHERE IN clause [1][2]. Regarding your technical query about its usage in parseInsertExpression: 1. Relationship to parseInsertExpression: The parseInsertExpression function (located in src/parser/insert-values-parser.ts) is used to parse insert statements [3][4]. It utilizes an internal helper function, parseRowValues, which inspects the provided object values [3]. If the values contain only primitive types and no expressions or undefined values, parseRowValues uses PrimitiveValueListNode.create to package the data [3]. If complex expressions or undefined values (which require default values) are detected, it falls back to the standard ValueListNode [3]. 2. Structure: The PrimitiveValueListNode interface is defined as follows [1][2]: interface PrimitiveValueListNode extends OperationNode { readonly kind: 'PrimitiveValueListNode'; readonly values: ReadonlyArray; } 3. Factory: It provides a factory object with a create method: PrimitiveValueListNode.create(values: ReadonlyArray): Readonly [1] This optimization is used internally by Kysely and is not intended for direct user manipulation [1][2]. The behavior described is consistent across recent versions, including 0.29.0 [1][5].
Citations:
🏁 Script executed:
Repository: zenstackhq/zenstack
Length of output: 684
🏁 Script executed:
Repository: zenstackhq/zenstack
Length of output: 684
Preserve raw
PrimitiveValueListNodevalues before classifying operation nodes.PrimitiveValueListNode.valuescontains raw values. Therefore, JSON such as{ kind: 'ValueNode', value: 'artwork' }or{ kind: 'DefaultInsertValueNode' }matches this condition and is reduced to'artwork'ornull. Preserve list provenance beforeunwrapCreateValueRow; do not usekindalone. Add regression cases for both values.🤖 Prompt for AI Agents