From c08a749e22087d2e37b81fc568bcaa51272aa999 Mon Sep 17 00:00:00 2001 From: Maytham Mahdi Date: Fri, 8 Aug 2025 08:12:36 +0300 Subject: [PATCH 1/5] Fix PostgreSQL JSON field search syntax error - Add check for existing ->> operator before processing JSON paths - Prevents malformed SQL generation that caused syntax errors - Fixes regression introduced in v3.3.31 - Resolves issue #17237 The fix ensures that PostgreSQL JSON field searches generate valid SQL by properly handling cases where the ->> operator is already present in the column expression. --- packages/support/src/helpers.php | 46 +++++++++++++++++--------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/packages/support/src/helpers.php b/packages/support/src/helpers.php index e0362a5f6a..164f958383 100644 --- a/packages/support/src/helpers.php +++ b/packages/support/src/helpers.php @@ -165,28 +165,32 @@ if (! function_exists('Filament\Support\generate_search_column_expression')) { $column = match ($driverName) { 'pgsql' => ( - str($column)->contains('->') - ? ( - // Handle `table.field` part with double quotes - str($column) - ->before('->') - ->explode('.') - ->map(fn (string $part): string => (string) str($part)->wrap('"')) - ->implode('.') - ) . collect(str($column)->after('->')->explode('->')) // Handle JSON path parts - ->map(function ($segment, $index) use ($column): string { - $totalParts = substr_count($column, '->'); + str($column)->contains('->>') + ? $column + : ( + str($column)->contains('->') + ? ( + // Handle `table.field` part with double quotes + str($column) + ->before('->') + ->explode('.') + ->map(fn (string $part): string => (string) str($part)->wrap('"')) + ->implode('.') + ).collect(str($column)->after('->')->explode('->')) // Handle JSON path parts + ->map(function ($segment, $index) use ($column): string { + $totalParts = substr_count($column, '->'); - return ($index === ($totalParts - 1)) - ? "->>'{$segment}'" - : "->'{$segment}'"; - }) - ->implode('') - : str($column) - ->explode('.') - ->map(fn (string $part): string => (string) str($part)->wrap('"')) - ->implode('.') - ) . '::text', + return ($index === ($totalParts - 1)) + ? "->>'{$segment}'" + : "->'{$segment}'"; + }) + ->implode('') + : str($column) + ->explode('.') + ->map(fn (string $part): string => (string) str($part)->wrap('"')) + ->implode('.') + ) + ).'::text', default => $column, }; From f11c00a282a6f464f0ad0dd8807bfe6472e9f520 Mon Sep 17 00:00:00 2001 From: Maytham Mahdi Date: Fri, 8 Aug 2025 08:13:51 +0300 Subject: [PATCH 2/5] Apply code style fixes to PostgreSQL JSON search fix --- packages/support/src/helpers.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/support/src/helpers.php b/packages/support/src/helpers.php index 164f958383..07422f0e0e 100644 --- a/packages/support/src/helpers.php +++ b/packages/support/src/helpers.php @@ -176,7 +176,7 @@ if (! function_exists('Filament\Support\generate_search_column_expression')) { ->explode('.') ->map(fn (string $part): string => (string) str($part)->wrap('"')) ->implode('.') - ).collect(str($column)->after('->')->explode('->')) // Handle JSON path parts + ) . collect(str($column)->after('->')->explode('->')) // Handle JSON path parts ->map(function ($segment, $index) use ($column): string { $totalParts = substr_count($column, '->'); @@ -190,7 +190,7 @@ if (! function_exists('Filament\Support\generate_search_column_expression')) { ->map(fn (string $part): string => (string) str($part)->wrap('"')) ->implode('.') ) - ).'::text', + ) . '::text', default => $column, }; From 6ed167d69d9f24f324b467c69239f801a1f80846 Mon Sep 17 00:00:00 2001 From: Maytham Mahdi Date: Fri, 8 Aug 2025 08:58:41 +0300 Subject: [PATCH 3/5] Add test cases for PostgreSQL columns with existing ->> operator - Test basic columns with ->> operator (e.g., name->>'ar') - Test table-prefixed columns with ->> operator (e.g., products.name->>'en') - Test relationship columns with ->> operator (e.g., categories.name->>'ar') - Test nested relationship columns with ->> operator (e.g., categories.parent.name->>'en') These tests ensure our fix properly handles cases where the ->> operator is already present in the column expression, preventing double-processing and malformed SQL generation. --- tests/src/Support/helpersTest.php | 64 +++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/tests/src/Support/helpersTest.php b/tests/src/Support/helpersTest.php index fb9f27ba7e..048812e85d 100644 --- a/tests/src/Support/helpersTest.php +++ b/tests/src/Support/helpersTest.php @@ -112,3 +112,67 @@ it('will generate a column expression for Postgres with colons in the table name ['blog:posts.title', 'lower("blog:posts"."title"::text)'], ['blog:posts:comments.author.name', 'lower("blog:posts:comments"."author"."name"::text)'], ]); + +it('will handle PostgreSQL columns that already contain ->> operator', function () { + $column = 'name->>\'ar\''; + $isSearchForcedCaseInsensitive = true; + + $databaseConnection = Mockery::mock(Connection::class); + $databaseConnection->shouldReceive('getDriverName')->andReturn('pgsql'); + $databaseConnection->shouldReceive('getConfig')->with('search_collation')->andReturn(null); + + $grammar = new PostgresGrammar($databaseConnection); + + $expression = generate_search_column_expression($column, $isSearchForcedCaseInsensitive, $databaseConnection); + + expect($expression->getValue($grammar)) + ->toBe("lower(name->>'ar'::text)"); +}); + +it('will handle PostgreSQL columns with table prefix that already contain ->> operator', function () { + $column = 'products.name->>\'en\''; + $isSearchForcedCaseInsensitive = true; + + $databaseConnection = Mockery::mock(Connection::class); + $databaseConnection->shouldReceive('getDriverName')->andReturn('pgsql'); + $databaseConnection->shouldReceive('getConfig')->with('search_collation')->andReturn(null); + + $grammar = new PostgresGrammar($databaseConnection); + + $expression = generate_search_column_expression($column, $isSearchForcedCaseInsensitive, $databaseConnection); + + expect($expression->getValue($grammar)) + ->toBe("lower(products.name->>'en'::text)"); +}); + +it('will handle PostgreSQL columns from relationships that already contain ->> operator', function () { + $column = 'categories.name->>\'ar\''; + $isSearchForcedCaseInsensitive = true; + + $databaseConnection = Mockery::mock(Connection::class); + $databaseConnection->shouldReceive('getDriverName')->andReturn('pgsql'); + $databaseConnection->shouldReceive('getConfig')->with('search_collation')->andReturn(null); + + $grammar = new PostgresGrammar($databaseConnection); + + $expression = generate_search_column_expression($column, $isSearchForcedCaseInsensitive, $databaseConnection); + + expect($expression->getValue($grammar)) + ->toBe("lower(categories.name->>'ar'::text)"); +}); + +it('will handle PostgreSQL columns from nested relationships that already contain ->> operator', function () { + $column = 'categories.parent.name->>\'en\''; + $isSearchForcedCaseInsensitive = true; + + $databaseConnection = Mockery::mock(Connection::class); + $databaseConnection->shouldReceive('getDriverName')->andReturn('pgsql'); + $databaseConnection->shouldReceive('getConfig')->with('search_collation')->andReturn(null); + + $grammar = new PostgresGrammar($databaseConnection); + + $expression = generate_search_column_expression($column, $isSearchForcedCaseInsensitive, $databaseConnection); + + expect($expression->getValue($grammar)) + ->toBe("lower(categories.parent.name->>'en'::text)"); +}); From 282d9a23496fd0627d824d91a67f51faebccdded Mon Sep 17 00:00:00 2001 From: Maytham Mahdi Date: Sun, 10 Aug 2025 15:03:57 +0300 Subject: [PATCH 4/5] Refactor PostgreSQL JSON search column expression handling - Simplified logic for processing columns with existing ->> operator. - Updated tests to reflect changes in column expression generation, ensuring proper handling of explicit ->> operator and nested JSON paths. - Improved clarity in test descriptions for better understanding of functionality. --- packages/support/src/helpers.php | 18 +++++++++++----- tests/src/Support/helpersTest.php | 34 ++++++++----------------------- 2 files changed, 22 insertions(+), 30 deletions(-) diff --git a/packages/support/src/helpers.php b/packages/support/src/helpers.php index 07422f0e0e..d50a539e22 100644 --- a/packages/support/src/helpers.php +++ b/packages/support/src/helpers.php @@ -165,10 +165,7 @@ if (! function_exists('Filament\Support\generate_search_column_expression')) { $column = match ($driverName) { 'pgsql' => ( - str($column)->contains('->>') - ? $column - : ( - str($column)->contains('->') + str($column)->contains('->') ? ( // Handle `table.field` part with double quotes str($column) @@ -178,6 +175,18 @@ if (! function_exists('Filament\Support\generate_search_column_expression')) { ->implode('.') ) . collect(str($column)->after('->')->explode('->')) // Handle JSON path parts ->map(function ($segment, $index) use ($column): string { + // if segment already contains '>something' (from ->> operator), preserve it + $isExplicitOperatorPrefixed = str($segment)->startsWith('>'); + $segment = $isExplicitOperatorPrefixed ? (string) str($segment)->after('>') : $segment; + + // Remove single quotes from segment if present to avoid redundant quoting + $isWrappedWithSingleQuotes = str($segment)->startsWith("'") && str($segment)->endsWith("'"); + $segment = $isWrappedWithSingleQuotes ? (string) str($segment)->trim("'") : $segment; + + if ($isExplicitOperatorPrefixed) { + return "->>'{$segment}'"; + } + $totalParts = substr_count($column, '->'); return ($index === ($totalParts - 1)) @@ -189,7 +198,6 @@ if (! function_exists('Filament\Support\generate_search_column_expression')) { ->explode('.') ->map(fn (string $part): string => (string) str($part)->wrap('"')) ->implode('.') - ) ) . '::text', default => $column, }; diff --git a/tests/src/Support/helpersTest.php b/tests/src/Support/helpersTest.php index 048812e85d..e26449a189 100644 --- a/tests/src/Support/helpersTest.php +++ b/tests/src/Support/helpersTest.php @@ -113,8 +113,8 @@ it('will generate a column expression for Postgres with colons in the table name ['blog:posts:comments.author.name', 'lower("blog:posts:comments"."author"."name"::text)'], ]); -it('will handle PostgreSQL columns that already contain ->> operator', function () { - $column = 'name->>\'ar\''; +it('will generate a JSON search column expression for Postgres with explicit ->> operator', function () { + $column = 'data->>name'; $isSearchForcedCaseInsensitive = true; $databaseConnection = Mockery::mock(Connection::class); @@ -126,11 +126,11 @@ it('will handle PostgreSQL columns that already contain ->> operator', function $expression = generate_search_column_expression($column, $isSearchForcedCaseInsensitive, $databaseConnection); expect($expression->getValue($grammar)) - ->toBe("lower(name->>'ar'::text)"); + ->toBe("lower(\"data\"->>'name'::text)"); }); -it('will handle PostgreSQL columns with table prefix that already contain ->> operator', function () { - $column = 'products.name->>\'en\''; +it('will generate a nested JSON search column expression for Postgres with explicit ->> operator on the last segment', function () { + $column = 'data->name->>ar'; $isSearchForcedCaseInsensitive = true; $databaseConnection = Mockery::mock(Connection::class); @@ -142,11 +142,11 @@ it('will handle PostgreSQL columns with table prefix that already contain ->> op $expression = generate_search_column_expression($column, $isSearchForcedCaseInsensitive, $databaseConnection); expect($expression->getValue($grammar)) - ->toBe("lower(products.name->>'en'::text)"); + ->toBe("lower(\"data\"->'name'->>'ar'::text)"); }); -it('will handle PostgreSQL columns from relationships that already contain ->> operator', function () { - $column = 'categories.name->>\'ar\''; +it('will generate a JSON search column expression for Postgres with explicit ->> operator and simple key', function () { + $column = 'name->>\'en\''; $isSearchForcedCaseInsensitive = true; $databaseConnection = Mockery::mock(Connection::class); @@ -158,21 +158,5 @@ it('will handle PostgreSQL columns from relationships that already contain ->> o $expression = generate_search_column_expression($column, $isSearchForcedCaseInsensitive, $databaseConnection); expect($expression->getValue($grammar)) - ->toBe("lower(categories.name->>'ar'::text)"); -}); - -it('will handle PostgreSQL columns from nested relationships that already contain ->> operator', function () { - $column = 'categories.parent.name->>\'en\''; - $isSearchForcedCaseInsensitive = true; - - $databaseConnection = Mockery::mock(Connection::class); - $databaseConnection->shouldReceive('getDriverName')->andReturn('pgsql'); - $databaseConnection->shouldReceive('getConfig')->with('search_collation')->andReturn(null); - - $grammar = new PostgresGrammar($databaseConnection); - - $expression = generate_search_column_expression($column, $isSearchForcedCaseInsensitive, $databaseConnection); - - expect($expression->getValue($grammar)) - ->toBe("lower(categories.parent.name->>'en'::text)"); + ->toBe("lower(\"name\"->>'en'::text)"); }); From 1f80864d129558a1a5178d4545e68c59da78ff17 Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Sun, 10 Aug 2025 20:26:20 +0100 Subject: [PATCH 5/5] Update packages/support/src/helpers.php --- packages/support/src/helpers.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/support/src/helpers.php b/packages/support/src/helpers.php index d50a539e22..05eeb77d59 100644 --- a/packages/support/src/helpers.php +++ b/packages/support/src/helpers.php @@ -175,7 +175,7 @@ if (! function_exists('Filament\Support\generate_search_column_expression')) { ->implode('.') ) . collect(str($column)->after('->')->explode('->')) // Handle JSON path parts ->map(function ($segment, $index) use ($column): string { - // if segment already contains '>something' (from ->> operator), preserve it + // If segment already starts with `>` (from `->>` operator), preserve it $isExplicitOperatorPrefixed = str($segment)->startsWith('>'); $segment = $isExplicitOperatorPrefixed ? (string) str($segment)->after('>') : $segment;