From bf1dde068f658de150715a046e8c20687953649f Mon Sep 17 00:00:00 2001 From: Soner Sayakci Date: Mon, 10 Feb 2025 08:58:13 +0100 Subject: [PATCH] fix: reusage of parameters Signed-off-by: Soner Sayakci --- CHANGELOG.md | 1 + src/OpenSearch/EndpointFactory.php | 11 +--- .../Endpoints/MlNamespaceIntegrationTest.php | 6 +++ tests/Namespaces/SearchTest.php | 50 +++++++++++++++++++ 4 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 tests/Namespaces/SearchTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index fccf9c3dc..90b3a6960 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Added - Added Guzzle and Symfony client factories ([#287](https://github.com/opensearch-project/opensearch-php/pull/287)) ### Changed +- Changed EndpointFactory to return new objects on each call to fix issues with parameter reusage ([#292](https://github.com/opensearch-project/opensearch-php/pull/292)) ### Deprecated ### Removed ### Fixed diff --git a/src/OpenSearch/EndpointFactory.php b/src/OpenSearch/EndpointFactory.php index 61abbe253..1b47e5917 100644 --- a/src/OpenSearch/EndpointFactory.php +++ b/src/OpenSearch/EndpointFactory.php @@ -12,11 +12,6 @@ */ class EndpointFactory implements EndpointFactoryInterface { - /** - * @var array - */ - private array $endpoints = []; - private ?SerializerInterface $serializer; public function __construct(?SerializerInterface $serializer = null) @@ -29,11 +24,7 @@ public function __construct(?SerializerInterface $serializer = null) */ public function getEndpoint(string $class): AbstractEndpoint { - if (!isset($this->endpoints[$class])) { - $this->endpoints[$class] = $this->createEndpoint($class); - } - - return $this->endpoints[$class]; + return $this->createEndpoint($class); } private function getSerializer(): SerializerInterface diff --git a/tests/Endpoints/MlNamespaceIntegrationTest.php b/tests/Endpoints/MlNamespaceIntegrationTest.php index 26c0a1088..3f4950860 100644 --- a/tests/Endpoints/MlNamespaceIntegrationTest.php +++ b/tests/Endpoints/MlNamespaceIntegrationTest.php @@ -41,6 +41,12 @@ public function testRegisterModelGroup() ], ]); $this->assertNotEmpty($modelGroupResponse['model_group_id']); + + if (Utility::isOpenSearchVersionAtLeast($client, '2.11.0')) { + $client->ml()->deleteModelGroup([ + 'id' => $modelGroupResponse['model_group_id'], + ]); + } } public function testgetModels() diff --git a/tests/Namespaces/SearchTest.php b/tests/Namespaces/SearchTest.php new file mode 100644 index 000000000..cd033b0fe --- /dev/null +++ b/tests/Namespaces/SearchTest.php @@ -0,0 +1,50 @@ +createMock(TransportInterface::class); + + $calledUrls = []; + + $transport->method('sendRequest') + ->willReturnCallback(function ($method, $uri, $params, $body) use (&$calledUrls) { + $calledUrls[] = $uri; + return []; + }); + + $client = new Client($transport, new EndpointFactory()); + + $client->search([ + 'index' => 'test', + 'body' => [ + 'query' => [ + 'match_all' => new \stdClass(), + ], + ], + ]); + + $client->search([ + 'body' => [ + 'query' => [ + 'match_all' => new \stdClass(), + ], + ], + ]); + + static::assertSame([ + '/test/_search', + '/_search', + ], $calledUrls); + } +}