Merge pull request #125 from utopia-php/fix/cache-empty-object-fidelity

fix(cache): preserve empty nested JSON objects (DAT-2310)
This commit is contained in:
Jake Barnby authored and GitHub committed 2026-08-12 19:48:59 +12:00
1 parent 6f8cb03a5c
commit 92e02dab63
11 files changed
+139 -6

No files matched your search

+2 -2
View File
@@ -42,7 +42,7 @@ class Hazelcast implements Adapter, Retryable
{
$cache = $this->execute(fn(): mixed => $this->memcached->get($key));
if (\is_string($cache)) {
$cache = json_decode($cache, true);
$cache = Json::decode($cache);
}
if (! \is_array($cache)) {
@@ -82,7 +82,7 @@ class Hazelcast implements Adapter, Retryable
{
$cache = $this->execute(fn(): mixed => $this->memcached->get($key));
if (\is_string($cache)) {
$cache = json_decode($cache, true);
$cache = Json::decode($cache);
}
if (! \is_array($cache)) {
+39
View File
@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace Utopia\Cache\Adapter;
use stdClass;
final class Json
{
public static function decode(string $value, int $flags = 0): mixed
{
if (preg_match('/\{\s*\}/', $value) === 0) {
return json_decode($value, true, flags: $flags);
}
return self::toAssociative(json_decode($value, flags: $flags));
}
private static function toAssociative(mixed $value): mixed
{
if ($value instanceof stdClass) {
$properties = get_object_vars($value);
if ($properties === []) {
return $value;
}
$value = $properties;
}
if (\is_array($value)) {
foreach ($value as $key => $item) {
$value[$key] = self::toAssociative($item);
}
}
return $value;
}
}
+3 -2
View File
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Utopia\Cache\Adapter\Redis;
use JsonException;
use Utopia\Cache\Adapter\Json;
/**
* Cache envelope codec shared by the Redis-family adapters.
@@ -34,7 +35,7 @@ final class Envelope
*/
public static function decode(string $value, int $ttl, int $now): mixed
{
$cache = json_decode($value, true);
$cache = Json::decode($value);
if (! \is_array($cache) || ! isset($cache['time'], $cache['data']) || ! \is_int($cache['time'])) {
return false;
}
@@ -53,7 +54,7 @@ final class Envelope
public static function touch(string $value, int $newTime): string|false
{
try {
$cache = json_decode($value, true, flags: JSON_THROW_ON_ERROR);
$cache = Json::decode($value, JSON_THROW_ON_ERROR);
} catch (JsonException) {
return false;
}
+2 -2
View File
@@ -57,7 +57,7 @@ class RedisCluster implements Adapter, Retryable
return false;
}
$cache = json_decode($redis_string, true);
$cache = Json::decode($redis_string);
// A purged key keeps its field until re-cached, holding a value that
// is not an envelope.
@@ -123,7 +123,7 @@ class RedisCluster implements Adapter, Retryable
try {
/** @var array{time: int, data: mixed} $cache */
$cache = json_decode($redis_string, true, flags: JSON_THROW_ON_ERROR);
$cache = Json::decode($redis_string, JSON_THROW_ON_ERROR);
$cache['time'] = time();
$value = json_encode($cache, flags: JSON_THROW_ON_ERROR);
} catch (Throwable) {
+3
View File
@@ -9,10 +9,13 @@ use MemcachedException;
use Utopia\Cache\Adapter\Hazelcast as HazelcastAdapter;
use Utopia\Cache\Cache;
use Utopia\Tests\Base;
use Utopia\Tests\Scope\EmptyObjectFidelity;
use Utopia\Tests\Services;
final class HazelcastTest extends Base
{
use EmptyObjectFidelity;
public static function setUpBeforeClass(): void
{
$memcached = new Memcached();
+26
View File
@@ -181,6 +181,32 @@ final class MultiplexingTest extends TestCase
});
}
public function testEmptyObjectFidelity(): void
{
$this->runCo(function (): void {
$cache = $this->makeCache();
$data = [
'empty' => new \stdClass(),
'nested' => ['empty' => new \stdClass()],
'list' => [new \stdClass(), ['x' => 1]],
'emptyArray' => [],
];
$this->assertSame($data, $cache->save('empty-object-fidelity', $data, 'empty-object-fidelity'));
$this->assertSame(
'{"empty":{},"nested":{"empty":{}},"list":[{},{"x":1}],"emptyArray":[]}',
json_encode($cache->load('empty-object-fidelity', 60, 'empty-object-fidelity')),
);
$this->assertTrue($cache->touch('empty-object-fidelity', 'empty-object-fidelity'));
$this->assertSame(
'{"empty":{},"nested":{"empty":{}},"list":[{},{"x":1}],"emptyArray":[]}',
json_encode($cache->load('empty-object-fidelity', 60, 'empty-object-fidelity')),
);
$cache->purge('empty-object-fidelity');
});
}
public function testLargeJsonPayload(): void
{
$this->runCo(function (): void {
+3
View File
@@ -10,10 +10,13 @@ use RedisClusterException;
use Utopia\Cache\Adapter\RedisCluster as RedisAdapter;
use Utopia\Cache\Cache;
use Utopia\Tests\Base;
use Utopia\Tests\Scope\EmptyObjectFidelity;
use Utopia\Tests\Services;
final class RedisClusterTest extends Base
{
use EmptyObjectFidelity;
private const float TIMEOUT = 1.5;
protected static RedisCluster $redis;
+3
View File
@@ -10,10 +10,13 @@ use RedisException;
use Utopia\Cache\Adapter\Redis as RedisAdapter;
use Utopia\Cache\Cache;
use Utopia\Tests\Base;
use Utopia\Tests\Scope\EmptyObjectFidelity;
use Utopia\Tests\Services;
final class RedisTest extends Base
{
use EmptyObjectFidelity;
public static function setUpBeforeClass(): void
{
$redis = new Redis();
+3
View File
@@ -10,10 +10,13 @@ use Utopia\Cache\Adapter\Redis as RedisAdapter;
use Utopia\Cache\Adapter\Sharding;
use Utopia\Cache\Cache;
use Utopia\Tests\Base;
use Utopia\Tests\Scope\EmptyObjectFidelity;
use Utopia\Tests\Services;
final class ShardingTest extends Base
{
use EmptyObjectFidelity;
public static function setUpBeforeClass(): void
{
$shards = [];
+32
View File
@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Utopia\Tests\Scope;
trait EmptyObjectFidelity
{
public function testEmptyObjectFidelity(): void
{
$key = 'empty-object-fidelity';
$data = [
'empty' => new \stdClass(),
'nested' => ['empty' => new \stdClass()],
'list' => [new \stdClass(), ['x' => 1]],
'emptyArray' => [],
];
$this->assertSame($data, self::$cache->save($key, $data, $key));
$this->assertSame(
'{"empty":{},"nested":{"empty":{}},"list":[{},{"x":1}],"emptyArray":[]}',
json_encode(self::$cache->load($key, 60, $key)),
);
$this->assertTrue(self::$cache->touch($key, $key));
$this->assertSame(
'{"empty":{},"nested":{"empty":{}},"list":[{},{"x":1}],"emptyArray":[]}',
json_encode(self::$cache->load($key, 60, $key)),
);
self::$cache->purge($key);
}
}
+23
View File
@@ -76,6 +76,29 @@ final class RedisEnvelopeTest extends TestCase
$this->assertSame($data, Envelope::decode($encoded, 60, 130));
}
public function testDecodePreservesEmptyObjects(): void
{
$data = [
'empty' => new \stdClass(),
'nested' => ['empty' => new \stdClass()],
'list' => [new \stdClass(), ['x' => 1]],
'emptyArray' => [],
];
$encoded = Envelope::encode($data, 100);
$this->assertSame(
'{"empty":{},"nested":{"empty":{}},"list":[{},{"x":1}],"emptyArray":[]}',
json_encode(Envelope::decode($encoded, 60, 130)),
);
$touched = Envelope::touch($encoded, 120);
$this->assertIsString($touched);
$this->assertSame(
'{"empty":{},"nested":{"empty":{}},"list":[{},{"x":1}],"emptyArray":[]}',
json_encode(Envelope::decode($touched, 60, 130)),
);
}
public function testTouchRewritesTime(): void
{
$encoded = Envelope::encode('value', 100);