Skip to content

A free open source IT asset/license management system

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

snipe5976e93de281
diff --git a/app/Models/Traits/Searchable.php b/app/Models/Traits/Searchable.php
index 27ac7748b0..44feae79b9 100644
--- a/app/Models/Traits/Searchable.php
+++ b/app/Models/Traits/Searchable.php
@@ -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'];
diff --git a/tests/Feature/Search/SearchableTraitTest.php b/tests/Feature/Search/SearchableTraitTest.php
index 2f35f58a8d..274901e56a 100644
--- a/tests/Feature/Search/SearchableTraitTest.php
+++ b/tests/Feature/Search/SearchableTraitTest.php
@@ -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
*/