Fixed RB-4136 - array to string conversion when people throw random crap at the API

This commit is contained in:
snipe
2026-05-20 18:04:43 +01:00
parent ea820ce99a
commit db4fcff1f3
2 changed files with 22 additions and 1 deletions
+1 -1
View File
@@ -53,7 +53,7 @@ trait Searchable
*/
public function scopeTextSearch($query, $search)
{
$preparedSearch = $this->prepareSearchInput((string) $search);
$preparedSearch = $this->prepareSearchInput(is_array($search) ? implode(' ', $search) : (string) $search);
$terms = $preparedSearch['terms'];
$filters = $preparedSearch['filters'];
$filterOperator = $preparedSearch['filter_operator'];
@@ -572,6 +572,27 @@ class SearchableTraitTest extends TestCase
->assertJson(fn (AssertableJson $json) => $json->has('rows', 3)->etc());
}
/**
* Regression: passing search as an array (?search[]=foo) must not throw
* "Array to string conversion" — values should be joined and searched normally.
*/
public function test_array_search_param_does_not_throw()
{
Asset::factory()->create(['name' => 'ArraySearchMacBook']);
Asset::factory()->create(['name' => 'ArraySearchDell']);
// search[]=ArraySearchMacBook simulates ?search[]=ArraySearchMacBook in the URL
$this->actingAsForApi(User::factory()->viewAssets()->create())
->getJson(route('api.assets.index', ['search' => ['ArraySearchMacBook']]))
->assertOk()
->assertJson(fn (AssertableJson $json) => $json->has('rows', 1)->etc());
// Multiple array values must not throw — exact match count depends on join semantics
$this->actingAsForApi(User::factory()->viewAssets()->create())
->getJson(route('api.assets.index', ['search' => ['ArraySearchMacBook', 'ArraySearchDell']]))
->assertOk();
}
/**
* Test no results when search matches nothing
*/