Skip to content

Commit d549453

Browse files
committed
Add support for API Compatibility Header (#1142)
1 parent 1e9397b commit d549453

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

src/Elasticsearch/ClientBuilder.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,11 +635,20 @@ public function build(): Client
635635
if (! isset($this->connectionParams['client']['headers'])) {
636636
$this->connectionParams['client']['headers'] = [];
637637
}
638+
$apiVersioning = getenv('ELASTIC_CLIENT_APIVERSIONING');
638639
if (! isset($this->connectionParams['client']['headers']['Content-Type'])) {
639-
$this->connectionParams['client']['headers']['Content-Type'] = ['application/json'];
640+
if ($apiVersioning === 'true' || $apiVersioning === '1') {
641+
$this->connectionParams['client']['headers']['Content-Type'] = ['application/vnd.elasticsearch+json;compatible-with=7'];
642+
} else {
643+
$this->connectionParams['client']['headers']['Content-Type'] = ['application/json'];
644+
}
640645
}
641646
if (! isset($this->connectionParams['client']['headers']['Accept'])) {
642-
$this->connectionParams['client']['headers']['Accept'] = ['application/json'];
647+
if ($apiVersioning === 'true' || $apiVersioning === '1') {
648+
$this->connectionParams['client']['headers']['Accept'] = ['application/vnd.elasticsearch+json;compatible-with=7'];
649+
} else {
650+
$this->connectionParams['client']['headers']['Accept'] = ['application/json'];
651+
}
643652
}
644653

645654
$this->connectionFactory = new ConnectionFactory($this->handler, $this->connectionParams, $this->serializer, $this->logger, $this->tracer);

tests/Elasticsearch/Tests/ClientBuilderTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,52 @@ public function testElasticClientMetaHeaderIsNotSentWhenDisabled()
296296
$this->assertFalse(isset($request['request']['headers']['x-elastic-client-meta']));
297297
}
298298
}
299+
300+
public function getCompatibilityHeaders()
301+
{
302+
return [
303+
['true', true],
304+
['1', true],
305+
['false', false],
306+
['0', false]
307+
];
308+
}
309+
310+
/**
311+
* @dataProvider getCompatibilityHeaders
312+
*/
313+
public function testCompatibilityHeader($env, $compatibility)
314+
{
315+
putenv("ELASTIC_CLIENT_APIVERSIONING=$env");
316+
317+
$client = ClientBuilder::create()
318+
->build();
319+
320+
try {
321+
$result = $client->info();
322+
} catch (ElasticsearchException $e) {
323+
$request = $client->transport->getLastConnection()->getLastRequestInfo();
324+
if ($compatibility) {
325+
$this->assertContains('application/vnd.elasticsearch+json;compatible-with=7', $request['request']['headers']['Content-Type']);
326+
$this->assertContains('application/vnd.elasticsearch+json;compatible-with=7', $request['request']['headers']['Accept']);
327+
} else {
328+
$this->assertNotContains('application/vnd.elasticsearch+json;compatible-with=7', $request['request']['headers']['Content-Type']);
329+
$this->assertNotContains('application/vnd.elasticsearch+json;compatible-with=7', $request['request']['headers']['Accept']);
330+
}
331+
}
332+
}
333+
334+
public function testCompatibilityHeaderDefaultIsOff()
335+
{
336+
$client = ClientBuilder::create()
337+
->build();
338+
339+
try {
340+
$result = $client->info();
341+
} catch (ElasticsearchException $e) {
342+
$request = $client->transport->getLastConnection()->getLastRequestInfo();
343+
$this->assertNotContains('application/vnd.elasticsearch+json;compatible-with=7', $request['request']['headers']['Content-Type']);
344+
$this->assertNotContains('application/vnd.elasticsearch+json;compatible-with=7', $request['request']['headers']['Accept']);
345+
}
346+
}
299347
}

0 commit comments

Comments
 (0)