Repository navigation
fix(find-options): allow array values in JsonContains - #12420
Conversation
JsonContains constrained its generic to Record<...>, which forced users to pass arrays through `as any` even though Postgres `@>` accepts both object and array containment. Relax the constraint to also accept readonly arrays so jsonb columns that store arrays can be queried without the cast. The existing test at test/github-issues/9381/issue-9381.test.ts:115 already exercised the array case via JsonContains([5, 6, 7, 8] as any); the cast is no longer required and has been removed. A new functional test under test/functional/find-options/json-contains-operator covers the array-of-objects scenario from the original report. Closes typeorm#11800
fd40de4 to
73fc3f8
Compare
Code Review by Qodo
1.
|
JsonContains now accepts arrays as well as objects, but the find-options docs only demonstrated object containment. Add a short example showing array containment so the docs match the supported usage. Refs typeorm#11800
The existing functional test only exercised array-of-objects containment, and the older issue typeorm#9381 case stores primitive arrays through a JSON transformer that wraps the value, so neither path covered raw jsonb columns holding a primitive array. Add a nullable jsonb numbers column on the test entity and a new case that stores [5, 6, 7, 8] / [1, 2] and asserts JsonContains([5, 6]) matches only the first row while JsonContains([9]) matches none. Refs typeorm#11800
commit: |
Allowing arrays in JsonContains exposed a binding gap: the WHERE
parameter is bound through createParameter without JSON serialization,
so pg-node converts a JS array to a Postgres array literal "{...}"
which then fails to parse on the right side of `col ::jsonb @> $1`
with `invalid input syntax for type json`. Object containment worked
only because pg-node stringifies plain objects on its own.
Stringify the value in getWherePredicateCondition when the operator
is jsonContains and the value is a non-null object or array, leaving
strings untouched so a pre-serialized JSON payload is not double
encoded.
Refs typeorm#11800
JsonContains is documented as supported on PostgreSQL and CockroachDB, and the comparable ArrayContains/ArrayContainedBy/ArrayOverlap suites already cover both drivers. Add cockroachdb to enabledDrivers so jsonb @> regressions on CockroachDB are caught by the same suite. Refs typeorm#11800
gioboa
left a comment
There was a problem hiding this comment.
Thanks for your help @kyungseopk1m
|
@kyungseopk1m all yours — thanks for picking this up. I didn't end up getting to it, and your approach matches what I had in mind. |
(cherry picked from commit f5cc456)
(cherry picked from commit f5cc456)
Summary
Fixes #11800.
JsonContainsconstrainedTtoRecord<...>, which forced users to pass arrays throughas anyeven though Postgres@>accepts both object and array containment. Relax the constraint to also accept readonly arrays so jsonb columns that store arrays can be queried without the cast.Tests
test/github-issues/9381/issue-9381.test.ts:115: removed the now-unnecessaryas anycast onJsonContains([5, 6, 7, 8]). The case existed precisely to exercise array containment at runtime but had to be type-cast to compile.test/functional/find-options/json-contains-operator/: new functional test mirroring the reporter's array-of-objects scenario.CC @PreAgile since you mentioned wanting to take this in #11800. I did not find an open PR for this issue; happy to coordinate or defer if there is in-flight work I missed.