Skip to content

Fix: Encode strings as JSON in JSONQueryString where clauses (fixes #1118) - #1438

Open
shadialhasan wants to merge 1 commit into
piccolo-orm:masterfrom
shadialhasan:fix-encode-strings-json-where
Open

shadialhasan wants to merge 1 commit into
piccolo-orm:masterfrom
shadialhasan:fix-encode-strings-json-where

Conversation

@shadialhasan

Copy link
Copy Markdown

Summary of Changes

Fixes #1118.

In piccolo/query/operators/json.py, JSONQueryString.clean_value() previously exempted all str instances from JSON encoding (if not isinstance(value, (str, QueryString)): value = dump_json(value)). As identified in #1118, when filtering JSON/JSONB columns using operators such as GetChildElement (arrow) or GetElementFromPath (from_path), plain string values (e.g. 'hello world') were passed as unquoted strings instead of valid JSON strings (e.g. '"hello world"'), leading to type mismatch issues in database JSON comparisons.

This PR updates JSONQueryString.clean_value() to:

  1. Preserve QueryString instances unchanged.
  2. Attempt to parse string values with load_json(). If the string is already valid JSON (e.g. '{"message": "hello world"}', '["a", "b"]', or '"already quoted"'), it is preserved as-is.
  3. If load_json() fails on a plain string (e.g. 'hello world', 'foo'), it encodes it with dump_json().
  4. Non-string Python values (dictionaries, lists, integers, booleans, etc.) continue to be serialized via dump_json().

Tests Added

  • Added unit tests in TestJSONQueryString (tests/query/operators/test_json.py) validating:
    • Plain string values are properly encoded as JSON ('hello world' -> '"hello world"').
    • Pre-encoded / valid JSON strings (objects, arrays, quoted strings) remain intact.
    • Other Python primitives and containers are serialized to JSON.
    • QueryString expressions are preserved.
    • Equality (==, eq) and inequality (!=, ne) operator compilation to SQL with parameterized query arguments.

Eng. MHD. Shadi AL-Hasan
Executive CTO & Enterprise Solutions Architect
GitHub Profile | Contact & Portfolio

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.

Encode strings as JSON in where clauses

1 participant