Skip to content

Commit a9db77e

Browse files
norkunasthijskuilmanStrift
authored
Cast federation payload explicilty to an object (#757)
* Cast federation payload explicilty to an object * Add unit test for multisearch federation array to object casting --------- Co-authored-by: Thijs Kuilman <thijs.kuilman@gmail.com> Co-authored-by: Laurent Cazanove <lau.cazanove@gmail.com>
1 parent 5cd4676 commit a9db77e

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/Endpoints/Delegates/HandlesMultiSearch.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function multiSearch(array $queries = [], ?MultiSearchFederation $federat
2525

2626
$payload = ['queries' => $body];
2727
if (null !== $federation) {
28-
$payload['federation'] = $federation->toArray();
28+
$payload['federation'] = (object) $federation->toArray();
2929
}
3030

3131
return $this->http->post('/multi-search', $payload);

tests/Endpoints/MultiSearchTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
namespace Tests\Endpoints;
66

7+
use Meilisearch\Client;
78
use Meilisearch\Contracts\FederationOptions;
89
use Meilisearch\Contracts\MultiSearchFederation;
910
use Meilisearch\Contracts\SearchQuery;
1011
use Meilisearch\Endpoints\Indexes;
12+
use Symfony\Component\HttpClient\MockHttpClient;
13+
use Symfony\Component\HttpClient\Psr18Client;
14+
use Symfony\Component\HttpClient\Response\MockResponse;
1115
use Tests\TestCase;
1216

1317
final class MultiSearchTest extends TestCase
@@ -159,4 +163,26 @@ public function testMultiSearchWithDistinctAttribute(): void
159163
self::assertCount(1, $response['results'][1]['hits']);
160164
self::assertSame('fantasy', $response['results'][1]['hits'][0]['genre']);
161165
}
166+
167+
public function testMultiSearchFederationCastingToObject(): void
168+
{
169+
$httpClient = new MockHttpClient(static function (string $method, string $url, array $options): MockResponse {
170+
self::assertSame('POST', $method);
171+
self::assertSame('http://meilisearch/multi-search', $url);
172+
self::assertSame('{"queries":[{"indexUid":"first"},{"indexUid":"second"}],"federation":{}}', $options['body']);
173+
174+
return new MockResponse(
175+
json_encode(['results' => []], \JSON_THROW_ON_ERROR),
176+
['response_headers' => ['content-type' => 'application/json']],
177+
);
178+
});
179+
180+
$client = new Client('http://meilisearch', 'apikey', new Psr18Client($httpClient));
181+
$client->multiSearch([
182+
(new SearchQuery())->setIndexUid('first'),
183+
(new SearchQuery())->setIndexUid('second'),
184+
],
185+
new MultiSearchFederation()
186+
);
187+
}
162188
}

0 commit comments

Comments
 (0)