Skip to content

fix: accept sparse numeric keys in search filters - #8863

Merged
soyuka merged 2 commits into
api-platform:5.0from
GregoireGiraud:fix/partial-search-sparse-keys
Nov 24, 2026
Merged

soyuka merged 2 commits into
api-platform:5.0from
GregoireGiraud:fix/partial-search-sparse-keys

Conversation

@GregoireGiraud

@GregoireGiraud GregoireGiraud commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor
Q A
Branch? 5.0
Tickets Fixes #8864; related to #8415
License MIT
Doc PR n/a

What

Doctrine ORM's ExactFilter and PartialSearchFilter ignored arrays with non-contiguous integer keys. Those keys are valid multi-value input, not operator names. Both filters now accept them while still leaving string-keyed operator maps to filters such as ComparisonFilter and DateFilter.

flowchart LR
    A["Bookstore app needs books alpha, beta, gamma"] --> B["beta is already in its local catalog"]
    B --> C["array_diff removes beta but preserves keys 0 and 2"]
    C --> D["GET /books?code[0]=alpha&code[2]=gamma"]
    D --> E["Before: filter ignored; returns alpha, beta, gamma"]
    D --> F["After: filter applied; returns only alpha and gamma"]
Loading

Tests

HTTP regression tests seed alpha, beta, and gamma and assert that both ordinary lists and sparse indices return only alpha and beta. The existing test combining ExactFilter and ComparisonFilter still verifies that [lt] applies only the comparison.

Targeted local run: 82 passed, 8 skipped, 311 assertions. PHP CS Fixer reported no changes.

// associative arrays are operator-maps owned by ComparisonFilter/DateFilter, not equality
if (\is_array($values) && !array_is_list($values)) {
return;
// String keys denote operators owned by filters such as ComparisonFilter and DateFilter.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

could we clean with array_values($values) to transform it as a list everytime ?

Also, this fix should be done in ExactFilter too

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

and in Odm\Filter too

@GregoireGiraud GregoireGiraud Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We can use array_values() after checking the original keys, but not unconditionally. array_values(['lt' => 'beta']) produces ['beta'], so the search filter would also run on input meant for ComparisonFilter. PartialSearchFilter already reindexes when it collects scalar values.

I’ve added the same fix to ORM ExactFilter, where we explicitly reindex after checking the keys. The HTTP regression test passes, and the existing [lt] test still passes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, both ODM filters have the same guard. I’ll apply the same rule to ODM ExactFilter and PartialSearchFilter: accept sparse integer keys, but leave string-keyed operator maps alone. I’ll add MongoDB-backed regression coverage for both.

@GregoireGiraud GregoireGiraud changed the title fix(doctrine): accept sparse numeric keys in PartialSearchFilter fix(doctrine): accept sparse numeric keys in ORM search filters Nov 23, 2026
@GregoireGiraud

Copy link
Copy Markdown
Contributor Author

Hi @soyuka, could you review this PR when you have a chance? Thanks!

@soyuka
soyuka force-pushed the fix/partial-search-sparse-keys branch from 68eac40 to 26644f4 Compare November 24, 2026 14:40
@soyuka soyuka changed the title fix(doctrine): accept sparse numeric keys in ORM search filters fix: accept sparse numeric keys in search filters Nov 24, 2026
@soyuka
soyuka merged commit b26d5c2 into api-platform:5.0 Nov 24, 2026
43 of 117 checks passed
@soyuka

soyuka commented Nov 24, 2026

Copy link
Copy Markdown
Member

Thanks!

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.

3 participants