Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4409a30

Browse files
committed
Merge branch '4.4' into 5.1
* 4.4: [FrameworkBundle] adopt src/.preload.php [Cache] Fix key encoding issue in Memcached adapter [HttpClient] Fix Array to string conversion notice when parsing JSON error body with non-scalar detail property
2 parents 3c5a4ed + cfec631 commit 4409a30

File tree

4 files changed

+51
-14
lines changed

4 files changed

+51
-14
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
179179
$preloadFile = $fs->makePathRelative(\dirname($containerFile, 2), $kernelDir);
180180
$preloadFile .= substr_replace(basename($containerFile), '.preload', -4, 0);
181181
$preloadFile = var_export('/'.$preloadFile, true);
182-
@file_put_contents($kernelDir.'/preload.php', <<<EOPHP
182+
@file_put_contents($kernelDir.'/.preload.php', <<<EOPHP
183183
<?php
184184
185185
if (file_exists(__DIR__.$preloadFile)) {

src/Symfony/Component/Cache/Tests/Adapter/MemcachedAdapterTest.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ public static function setUpBeforeClass(): void
4242
}
4343
}
4444

45-
public function createCachePool(int $defaultLifetime = 0): CacheItemPoolInterface
45+
public function createCachePool(int $defaultLifetime = 0, string $testMethod = null, string $namespace = null): CacheItemPoolInterface
4646
{
4747
$client = $defaultLifetime ? AbstractAdapter::createConnection('memcached://'.getenv('MEMCACHED_HOST')) : self::$client;
4848

49-
return new MemcachedAdapter($client, str_replace('\\', '.', __CLASS__), $defaultLifetime);
49+
return new MemcachedAdapter($client, $namespace ?? str_replace('\\', '.', __CLASS__), $defaultLifetime);
5050
}
5151

5252
public function testOptions()
@@ -248,4 +248,36 @@ public function testMultiServerDsn()
248248
];
249249
$this->assertSame($expected, $client->getServerList());
250250
}
251+
252+
public function testKeyEncoding()
253+
{
254+
$reservedMemcachedCharacters = " \n\r\t\v\f\0";
255+
256+
$namespace = $reservedMemcachedCharacters.random_int(0, \PHP_INT_MAX);
257+
$pool = $this->createCachePool(0, null, $namespace);
258+
259+
/**
260+
* Choose a key that is below {@see \Symfony\Component\Cache\Adapter\MemcachedAdapter::$maxIdLength} so that
261+
* {@see \Symfony\Component\Cache\Traits\AbstractTrait::getId()} does not shorten the key but choose special
262+
* characters that would be encoded and therefore increase the key length over the Memcached limit.
263+
*/
264+
// 250 is Memcached’s max key length, 7 bytes for prefix seed
265+
$key = str_repeat('%', 250 - 7 - \strlen($reservedMemcachedCharacters) - \strlen($namespace)).$reservedMemcachedCharacters;
266+
267+
self::assertFalse($pool->hasItem($key));
268+
269+
$item = $pool->getItem($key);
270+
self::assertFalse($item->isHit());
271+
self::assertSame($key, $item->getKey());
272+
273+
self::assertTrue($pool->save($item->set('foobar')));
274+
275+
self::assertTrue($pool->hasItem($key));
276+
$item = $pool->getItem($key);
277+
self::assertTrue($item->isHit());
278+
self::assertSame($key, $item->getKey());
279+
280+
self::assertTrue($pool->deleteItem($key));
281+
self::assertFalse($pool->hasItem($key));
282+
}
251283
}

src/Symfony/Component/HttpClient/Exception/HttpExceptionTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public function __construct(ResponseInterface $response)
6060
// see http://www.hydra-cg.com/spec/latest/core/#description-of-http-status-codes-and-errors
6161
$separator = isset($body['hydra:title'], $body['hydra:description']) ? "\n\n" : '';
6262
$message = ($body['hydra:title'] ?? '').$separator.($body['hydra:description'] ?? '');
63-
} elseif (isset($body['title']) || isset($body['detail'])) {
63+
} elseif ((isset($body['title']) || isset($body['detail']))
64+
&& (is_scalar($body['title'] ?? '') && is_scalar($body['detail'] ?? ''))) {
6465
// see RFC 7807 and https://jsonapi.org/format/#error-objects
6566
$separator = isset($body['title'], $body['detail']) ? "\n\n" : '';
6667
$message = ($body['title'] ?? '').$separator.($body['detail'] ?? '');

src/Symfony/Component/HttpClient/Tests/Exception/HttpExceptionTraitTest.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,24 @@ class HttpExceptionTraitTest extends TestCase
2222
{
2323
public function provideParseError(): iterable
2424
{
25-
yield ['application/ld+json', '{"hydra:title": "An error occurred", "hydra:description": "Some details"}'];
26-
yield ['application/problem+json', '{"title": "An error occurred", "detail": "Some details"}'];
27-
yield ['application/vnd.api+json', '{"title": "An error occurred", "detail": "Some details"}'];
25+
$errorWithoutMessage = 'HTTP/1.1 400 Bad Request returned for "http://example.com".';
26+
27+
$errorWithMessage = <<<ERROR
28+
An error occurred
29+
30+
Some details
31+
ERROR;
32+
33+
yield ['application/ld+json', '{"hydra:title": "An error occurred", "hydra:description": "Some details"}', $errorWithMessage];
34+
yield ['application/problem+json', '{"title": "An error occurred", "detail": "Some details"}', $errorWithMessage];
35+
yield ['application/vnd.api+json', '{"title": "An error occurred", "detail": "Some details"}', $errorWithMessage];
36+
yield ['application/json', '{"title": "An error occurred", "detail": {"field_name": ["Some details"]}}', $errorWithoutMessage];
2837
}
2938

3039
/**
3140
* @dataProvider provideParseError
3241
*/
33-
public function testParseError(string $mimeType, string $json): void
42+
public function testParseError(string $mimeType, string $json, string $expectedMessage): void
3443
{
3544
$response = $this->createMock(ResponseInterface::class);
3645
$response
@@ -47,12 +56,7 @@ public function testParseError(string $mimeType, string $json): void
4756

4857
$e = new TestException($response);
4958
$this->assertSame(400, $e->getCode());
50-
$this->assertSame(<<<ERROR
51-
An error occurred
52-
53-
Some details
54-
ERROR
55-
, $e->getMessage());
59+
$this->assertSame($expectedMessage, $e->getMessage());
5660
}
5761
}
5862

0 commit comments

Comments
 (0)