Skip to content

fix(orm): enum field filter inside typed JSON compared as raw JSON - #2776

Open
ymc9 wants to merge 1 commit into
devfrom
fix/issue-2755
Open

fix(orm): enum field filter inside typed JSON compared as raw JSON#2776
ymc9 wants to merge 1 commit into
devfrom
fix/issue-2755

Conversation

@ymc9

@ymc9 ymc9 commented Jul 30, 2026

Copy link
Copy Markdown
Member

Fixes #2755

Problem

Filtering by an enum-typed field inside a typed JSON field failed:

db.bKP.findMany({ where: { proposal: { bkpType: 'CAPSTONE' } } });

On PostgreSQL this threw invalid input syntax for type json (the JSON-extracted value was compared directly against the bare enum literal), and on SQLite it silently matched nothing (quoted JSON text "CAPSTONE" never equals CAPSTONE).

Fix

In buildTypeJsonNonArrayFilter, the cast-to-text + quote-trim treatment applied to String fields is extended to enum fields, since enum values are stored as JSON strings. The comparison then flows through buildEnumFilter on a plain-text receiver, covering equals, not, in, and notIn.

Testing

  • New regression test tests/regression/test/issue-2755.test.ts covering direct equality, not, and in filters on both SQLite and PostgreSQL (failed with the issue's exact error before the fix).
  • Existing e2e suites json-filter.test.ts and typed-json-fields.test.ts pass on both SQLite and PostgreSQL.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved JSON filtering for enum values.
    • Fixed equality, negation, and inclusion filters for enums stored inside JSON fields across SQLite and PostgreSQL.

Enum values are stored as JSON strings, but filtering by an enum-typed
field of a typed JSON field compared the JSON-extracted value directly
against the bare enum literal, failing on Postgres with "invalid input
syntax for type json" and silently matching nothing on SQLite. Apply
the same cast-to-text + quote-trim treatment used for String fields.

Fixes #2755

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The ORM now trims surrounding quotes from enum values in typed JSON filters before primitive filter construction. A regression test covers equality, negation, and inclusion queries across SQLite and PostgreSQL.

Changes

Typed JSON enum filtering

Layer / File(s) Summary
Enum filter value normalization
packages/orm/src/client/crud/dialects/base-dialect.ts
Typed JSON enum values are quote-trimmed before being passed to buildPrimitiveFilter.
Regression coverage for filter operators
tests/regression/test/issue-2755.test.ts
Tests equality, not, and in filtering for embedded enum values on SQLite and PostgreSQL.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: evgenovalov

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately reflects the enum-filter fix in typed JSON fields.
Linked Issues check ✅ Passed The code and regression test address #2755 by making enum filters work inside typed JSON fields.
Out of Scope Changes check ✅ Passed The changes appear scoped to the reported enum-in-JSON filter bug and its regression coverage.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/issue-2755

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/regression/test/issue-2755.test.ts`:
- Around line 50-54: Add a regression assertion in the bKP.findMany tests for
the filter-builder’s notIn branch, using proposal.bkpType.notIn with
['CAPSTONE'] and asserting that only the record with id '2' is returned.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: dcf83742-e96b-4fc8-ba02-97cdbcccd6e4

📥 Commits

Reviewing files that changed from the base of the PR and between b0e2465 and b5e5c26.

📒 Files selected for processing (2)
  • packages/orm/src/client/crud/dialects/base-dialect.ts
  • tests/regression/test/issue-2755.test.ts

Comment on lines +50 to +54
await expect(
db.bKP.findMany({
where: { proposal: { bkpType: { in: ['CAPSTONE', 'OTHER'] } } },
}),
).resolves.toEqual([expect.objectContaining({ id: '1' }), expect.objectContaining({ id: '2' })]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Add an explicit notIn regression case.

The PR claims support for notIn, but this suite only covers equals, not, and in. notIn has its own filter-builder branch, so add an assertion that notIn: ['CAPSTONE'] returns only record 2. The PR objective explicitly includes notIn.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/regression/test/issue-2755.test.ts` around lines 50 - 54, Add a
regression assertion in the bKP.findMany tests for the filter-builder’s notIn
branch, using proposal.bkpType.notIn with ['CAPSTONE'] and asserting that only
the record with id '2' is returned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error on filter by JSON field with enum values

1 participant