Skip to content

Commit bfd04a2

Browse files
authored
Add the API compatibility header in v8 (#1233)
* Add the API compatibility header in v8 * Fixed license header * Changed TEST_SUITE to platinum for github integration test * Added multiple Accept case for compatibility header * Fixed ML integration tests
1 parent ed7cd37 commit bfd04a2

14 files changed

+114
-78
lines changed

.ci/jobs/elastic+elasticsearch-php+8.2.yml

Lines changed: 0 additions & 14 deletions
This file was deleted.

.github/check-license-headers.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ function check_license_header {
1919
local f
2020
f=$1
2121

22-
if ! diff -w .github/license-header.txt <(head -$NLINES2 "$f" | tail -$NLINES1) >/dev/null; then
23-
echo "check-license-headers: error: '$f' does not have required license header, see 'diff -w .github/license-header.txt <(head -$NLINES2 $f | tail -$NLINES1)'"
24-
return 1
25-
else
22+
license=`cat .github/license-header.txt`
23+
file=`cat $f`
24+
if [[ "$file" == *"$license"* ]]; then
2625
return 0
26+
else
27+
echo "check-license-headers: error: '$f' does not have required license header"
28+
return 1
2729
fi
2830
}
2931

src/Client.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ final class Client
2929
{
3030
const CLIENT_NAME = 'es';
3131
const VERSION = '8.4.0-dev';
32-
32+
const API_COMPATIBILITY_HEADER = '%s/vnd.elasticsearch+%s; compatible-with=8';
33+
3334
use ClientEndpointsTrait;
3435
use EndpointTrait;
3536
use NamespaceTrait;

src/Response/Elasticsearch.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ public function asArray(): array
105105
$this->asArray = JsonSerializer::unserialize($this->asString());
106106
return $this->asArray;
107107
}
108-
if (strpos($contentType, 'application/x-ndjson') !== false) {
108+
if (strpos($contentType, 'application/x-ndjson') !== false ||
109+
strpos($contentType, 'application/vnd.elasticsearch+x-ndjson') !== false) {
109110
$this->asArray = NDJsonSerializer::unserialize($this->asString());
110111
return $this->asArray;
111112
}
@@ -132,12 +133,13 @@ public function asObject(): object
132133
return $this->asObject;
133134
}
134135
$contentType = $this->response->getHeaderLine('Content-Type');
135-
if (strpos($contentType, 'application/json') !== false||
136+
if (strpos($contentType, 'application/json') !== false ||
136137
strpos($contentType, 'application/vnd.elasticsearch+json') !== false) {
137138
$this->asObject = JsonSerializer::unserialize($this->asString(), ['type' => 'object']);
138139
return $this->asObject;
139140
}
140-
if (strpos($contentType, 'application/x-ndjson') !== false) {
141+
if (strpos($contentType, 'application/x-ndjson') !== false ||
142+
strpos($contentType, 'application/vnd.elasticsearch+x-ndjson') !== false) {
141143
$this->asObject = NDJsonSerializer::unserialize($this->asString(), ['type' => 'object']);
142144
return $this->asObject;
143145
}

src/Traits/EndpointTrait.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
namespace Elastic\Elasticsearch\Traits;
1616

17+
use Elastic\Elasticsearch\Client;
1718
use Elastic\Elasticsearch\Exception\ContentTypeException;
1819
use Elastic\Elasticsearch\Exception\MissingParameterException;
1920
use Elastic\Transport\Serializer\JsonSerializer;
@@ -106,10 +107,12 @@ protected function addQueryString(string $url, array $params, array $keys): stri
106107
*/
107108
protected function bodySerialize($body, string $contentType): string
108109
{
109-
if (strpos($contentType, 'application/x-ndjson') !== false) {
110+
if (strpos($contentType, 'application/x-ndjson') !== false ||
111+
strpos($contentType, 'application/vnd.elasticsearch+x-ndjson') !== false) {
110112
return NDJsonSerializer::serialize($body, ['remove_null' => false]);
111113
}
112-
if (strpos($contentType, 'application/json') !== false) {
114+
if (strpos($contentType, 'application/json') !== false ||
115+
strpos($contentType, 'application/vnd.elasticsearch+json') !== false) {
113116
return JsonSerializer::serialize($body, ['remove_null' => false]);
114117
}
115118
throw new ContentTypeException(sprintf(
@@ -141,13 +144,40 @@ protected function createRequest(string $method, string $url, array $headers, $b
141144
$content = is_string($body) ? $body : $this->bodySerialize($body, $headers['Content-Type']);
142145
$request = $request->withBody($streamFactory->createStream($content));
143146
}
147+
$headers = $this->buildCompatibilityHeaders($headers);
148+
144149
// Headers
145150
foreach ($headers as $name => $value) {
146151
$request = $request->withHeader($name, $value);
147152
}
148153
return $request;
149154
}
150155

156+
/**
157+
* Build the API compatibility headers
158+
* transfrom Content-Type and Accept adding vnd.elasticsearch+ and compatible-with
159+
*
160+
* @see https://github.com/elastic/elasticsearch-php/pull/1142
161+
*/
162+
protected function buildCompatibilityHeaders(array $headers): array
163+
{
164+
if (isset($headers['Content-Type'])) {
165+
if (preg_match('/application\/([^,]+)$/', $headers['Content-Type'], $matches)) {
166+
$headers['Content-Type'] = sprintf(Client::API_COMPATIBILITY_HEADER, 'application', $matches[1]);
167+
}
168+
}
169+
if (isset($headers['Accept'])) {
170+
$values = explode(',', $headers['Accept']);
171+
foreach ($values as &$value) {
172+
if (preg_match('/(application|text)\/([^,]+)/', $value, $matches)) {
173+
$value = sprintf(Client::API_COMPATIBILITY_HEADER, $matches[1], $matches[2]);
174+
}
175+
}
176+
$headers['Accept'] = implode(',', $values);
177+
}
178+
return $headers;
179+
}
180+
151181
/**
152182
* Check if the $required parameters are present in $params
153183
* @throws MissingParameterException

tests/Integration/MlTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
namespace Elastic\Elasticsearch\Tests\Integration;
1616

17+
use Elastic\Elasticsearch\Client;
1718
use Elastic\Elasticsearch\Tests\Utility;
1819
use PHPUnit\Framework\TestCase;
1920

@@ -88,7 +89,7 @@ public function testPostDataWithASingleJson(string $jobId)
8889

8990
$this->assertEquals(202, $response->getStatusCode());
9091
$request = $this->client->getTransport()->getLastRequest();
91-
$this->assertEquals(['application/json'], $request->getHeader('Content-Type'));
92+
$this->assertEquals([sprintf(Client::API_COMPATIBILITY_HEADER, 'application', 'json')], $request->getHeader('Content-Type'));
9293
}
9394

9495
/**
@@ -107,7 +108,7 @@ public function testPostDataWithMultipleJson(string $jobId)
107108

108109
$this->assertEquals(202, $response->getStatusCode());
109110
$request = $this->client->getTransport()->getLastRequest();
110-
$this->assertEquals(['application/x-ndjson'], $request->getHeader('Content-Type'));
111+
$this->assertEquals([sprintf(Client::API_COMPATIBILITY_HEADER, 'application', 'x-ndjson')], $request->getHeader('Content-Type'));
111112
}
112113

113114
/**

tests/Traits/EndpointTraitTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
namespace Elastic\Elasticsearch\Tests\Traits;
1616

17+
use Elastic\Elasticsearch\Client;
1718
use Elastic\Elasticsearch\Exception\ContentTypeException;
1819
use Elastic\Elasticsearch\Exception\MissingParameterException;
1920
use Elastic\Elasticsearch\Traits\EndpointTrait;
@@ -65,6 +66,36 @@ public function testBodySerializeUnknownContentTypeThrowsException()
6566
$this->bodySerialize(['foo' => 'bar'], 'Unknown-content-type');
6667
}
6768

69+
public function getCompatibilityHeaders()
70+
{
71+
return [
72+
[['Content-Type' => 'application/json'], ['Content-Type' => sprintf(Client::API_COMPATIBILITY_HEADER, 'application', 'json')]],
73+
[['Accept' => 'application/json'], ['Accept' => sprintf(Client::API_COMPATIBILITY_HEADER, 'application', 'json')]],
74+
[['Content-Type' => 'application/x-ndjson'], ['Content-Type' => sprintf(Client::API_COMPATIBILITY_HEADER, 'application', 'x-ndjson')]],
75+
[['Accept' => 'application/x-ndjson'], ['Accept' => sprintf(Client::API_COMPATIBILITY_HEADER, 'application', 'x-ndjson')]],
76+
[['Content-Type' => 'application/text'], ['Content-Type' => sprintf(Client::API_COMPATIBILITY_HEADER, 'application', 'text')]],
77+
[['Accept' => 'text/plain'], ['Accept' => sprintf(Client::API_COMPATIBILITY_HEADER, 'text', 'plain')]],
78+
// Multiple values
79+
[[
80+
'Accept' => 'text/plain,application/json'
81+
], [
82+
'Accept' => sprintf(
83+
"%s,%s",
84+
sprintf(Client::API_COMPATIBILITY_HEADER, 'text', 'plain'),
85+
sprintf(Client::API_COMPATIBILITY_HEADER, 'application', 'json')
86+
)
87+
]],
88+
];
89+
}
90+
91+
/**
92+
* @dataProvider getCompatibilityHeaders
93+
*/
94+
public function testBuildCompatibilityHeaders(array $input, array $expected)
95+
{
96+
$this->assertEquals($expected, $this->buildCompatibilityHeaders($input));
97+
}
98+
6899
public function getRequestParts(): array
69100
{
70101
return [
@@ -88,6 +119,7 @@ public function testCreateRequest(string $method, string $url, array $headers, $
88119
$host = parse_url($url, PHP_URL_HOST);
89120
$port = parse_url($url, PHP_URL_PORT);
90121
$this->assertEquals(empty($port) ? $host : $host . ':' . $port, $request->getHeader('Host')[0]);
122+
$headers = $this->buildCompatibilityHeaders($headers);
91123
foreach ($headers as $name => $value) {
92124
$header = $request->getHeader($name);
93125
$this->assertEquals($value, implode(',', $header));

util/ActionTest.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
<?php
22
/**
3-
* Elasticsearch PHP client
3+
* Elasticsearch PHP Client
44
*
5-
* @link https://github.com/elastic/elasticsearch-php/
5+
* @link https://github.com/elastic/elasticsearch-php
66
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
7-
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
8-
* @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
9-
*
7+
* @license https://opensource.org/licenses/MIT MIT License
8+
*
109
* Licensed to Elasticsearch B.V under one or more agreements.
11-
* Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
12-
* the GNU Lesser General Public License, Version 2.1, at your option.
10+
* Elasticsearch B.V licenses this file to you under the MIT License.
1311
* See the LICENSE file in the project root for more information.
1412
*/
1513

util/GenerateDocExamples.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
<?php
22
/**
3-
* Elasticsearch PHP client
3+
* Elasticsearch PHP Client
44
*
5-
* @link https://github.com/elastic/elasticsearch-php/
5+
* @link https://github.com/elastic/elasticsearch-php
66
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
7-
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
8-
* @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
9-
*
7+
* @license https://opensource.org/licenses/MIT MIT License
8+
*
109
* Licensed to Elasticsearch B.V under one or more agreements.
11-
* Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
12-
* the GNU Lesser General Public License, Version 2.1, at your option.
10+
* Elasticsearch B.V licenses this file to you under the MIT License.
1311
* See the LICENSE file in the project root for more information.
1412
*/
15-
1613
declare(strict_types = 1);
1714

1815
require_once dirname(__DIR__) . '/vendor/autoload.php';

util/RestSpecRunner.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
<?php
22
/**
3-
* Elasticsearch PHP client
3+
* Elasticsearch PHP Client
44
*
5-
* @link https://github.com/elastic/elasticsearch-php/
5+
* @link https://github.com/elastic/elasticsearch-php
66
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
7-
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
8-
* @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
9-
*
7+
* @license https://opensource.org/licenses/MIT MIT License
8+
*
109
* Licensed to Elasticsearch B.V under one or more agreements.
11-
* Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
12-
* the GNU Lesser General Public License, Version 2.1, at your option.
10+
* Elasticsearch B.V licenses this file to you under the MIT License.
1311
* See the LICENSE file in the project root for more information.
1412
*/
1513
declare(strict_types = 1);

util/YamlTests.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
<?php
22
/**
3-
* Elasticsearch PHP client
3+
* Elasticsearch PHP Client
44
*
5-
* @link https://github.com/elastic/elasticsearch-php/
5+
* @link https://github.com/elastic/elasticsearch-php
66
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
7-
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
8-
* @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
9-
*
7+
* @license https://opensource.org/licenses/MIT MIT License
8+
*
109
* Licensed to Elasticsearch B.V under one or more agreements.
11-
* Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
12-
* the GNU Lesser General Public License, Version 2.1, at your option.
10+
* Elasticsearch B.V licenses this file to you under the MIT License.
1311
* See the LICENSE file in the project root for more information.
1412
*/
15-
1613
declare(strict_types = 1);
1714

1815
namespace Elastic\Elasticsearch\Util;

util/build_tests.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
<?php
22
/**
3-
* Elasticsearch PHP client
3+
* Elasticsearch PHP Client
44
*
5-
* @link https://github.com/elastic/elasticsearch-php/
5+
* @link https://github.com/elastic/elasticsearch-php
66
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
7-
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
8-
* @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
9-
*
7+
* @license https://opensource.org/licenses/MIT MIT License
8+
*
109
* Licensed to Elasticsearch B.V under one or more agreements.
11-
* Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
12-
* the GNU Lesser General Public License, Version 2.1, at your option.
10+
* Elasticsearch B.V licenses this file to you under the MIT License.
1311
* See the LICENSE file in the project root for more information.
1412
*/
1513
declare(strict_types = 1);

util/docsConfig.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
<?php
22
/**
3-
* Elasticsearch PHP client
3+
* Elasticsearch PHP Client
44
*
5-
* @link https://github.com/elastic/elasticsearch-php/
5+
* @link https://github.com/elastic/elasticsearch-php
66
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
7-
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
8-
* @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
7+
* @license https://opensource.org/licenses/MIT MIT License
98
*
109
* Licensed to Elasticsearch B.V under one or more agreements.
11-
* Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
12-
* the GNU Lesser General Public License, Version 2.1, at your option.
10+
* Elasticsearch B.V licenses this file to you under the MIT License.
1311
* See the LICENSE file in the project root for more information.
1412
*/
15-
16-
1713
declare(strict_types = 1);
1814

1915
use Doctum\Doctum;

util/examples_to_parse.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
<?php
22
/**
3-
* Elasticsearch PHP client
3+
* Elasticsearch PHP Client
44
*
5-
* @link https://github.com/elastic/elasticsearch-php/
5+
* @link https://github.com/elastic/elasticsearch-php
66
* @copyright Copyright (c) Elasticsearch B.V (https://www.elastic.co)
7-
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
8-
* @license https://www.gnu.org/licenses/lgpl-2.1.html GNU Lesser General Public License, Version 2.1
9-
*
7+
* @license https://opensource.org/licenses/MIT MIT License
8+
*
109
* Licensed to Elasticsearch B.V under one or more agreements.
11-
* Elasticsearch B.V licenses this file to you under the Apache 2.0 License or
12-
* the GNU Lesser General Public License, Version 2.1, at your option.
10+
* Elasticsearch B.V licenses this file to you under the MIT License.
1311
* See the LICENSE file in the project root for more information.
1412
*/
1513

0 commit comments

Comments
 (0)