From 92e02dab63606234b993b841ebf4c58845dd4620 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 12 Aug 2026 19:48:59 +1200 Subject: [PATCH] Merge pull request #125 from utopia-php/fix/cache-empty-object-fidelity fix(cache): preserve empty nested JSON objects (DAT-2310) --- src/Cache/Adapter/Hazelcast.php | 4 +-- src/Cache/Adapter/Json.php | 39 +++++++++++++++++++++++ src/Cache/Adapter/Redis/Envelope.php | 5 +-- src/Cache/Adapter/RedisCluster.php | 4 +-- tests/Cache/E2E/HazelcastTest.php | 3 ++ tests/Cache/E2E/MultiplexingTest.php | 26 +++++++++++++++ tests/Cache/E2E/RedisClusterTest.php | 3 ++ tests/Cache/E2E/RedisTest.php | 3 ++ tests/Cache/E2E/ShardingTest.php | 3 ++ tests/Cache/Scope/EmptyObjectFidelity.php | 32 +++++++++++++++++++ tests/Cache/Unit/RedisEnvelopeTest.php | 23 +++++++++++++ 11 files changed, 139 insertions(+), 6 deletions(-) create mode 100644 src/Cache/Adapter/Json.php create mode 100644 tests/Cache/Scope/EmptyObjectFidelity.php diff --git a/src/Cache/Adapter/Hazelcast.php b/src/Cache/Adapter/Hazelcast.php index 5349b1cf72..693409f069 100644 --- a/src/Cache/Adapter/Hazelcast.php +++ b/src/Cache/Adapter/Hazelcast.php @@ -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)) { diff --git a/src/Cache/Adapter/Json.php b/src/Cache/Adapter/Json.php new file mode 100644 index 0000000000..c49ef6557d --- /dev/null +++ b/src/Cache/Adapter/Json.php @@ -0,0 +1,39 @@ + $item) { + $value[$key] = self::toAssociative($item); + } + } + + return $value; + } +} diff --git a/src/Cache/Adapter/Redis/Envelope.php b/src/Cache/Adapter/Redis/Envelope.php index d0466fe456..c2fd143f53 100644 --- a/src/Cache/Adapter/Redis/Envelope.php +++ b/src/Cache/Adapter/Redis/Envelope.php @@ -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; } diff --git a/src/Cache/Adapter/RedisCluster.php b/src/Cache/Adapter/RedisCluster.php index 8918e9df97..05b7c4ec9e 100644 --- a/src/Cache/Adapter/RedisCluster.php +++ b/src/Cache/Adapter/RedisCluster.php @@ -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) { diff --git a/tests/Cache/E2E/HazelcastTest.php b/tests/Cache/E2E/HazelcastTest.php index de19982979..3dc8b6ca57 100644 --- a/tests/Cache/E2E/HazelcastTest.php +++ b/tests/Cache/E2E/HazelcastTest.php @@ -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(); diff --git a/tests/Cache/E2E/MultiplexingTest.php b/tests/Cache/E2E/MultiplexingTest.php index 3188b2ac0a..a15e058d43 100644 --- a/tests/Cache/E2E/MultiplexingTest.php +++ b/tests/Cache/E2E/MultiplexingTest.php @@ -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 { diff --git a/tests/Cache/E2E/RedisClusterTest.php b/tests/Cache/E2E/RedisClusterTest.php index a9aa289b4d..4b91ce54a9 100644 --- a/tests/Cache/E2E/RedisClusterTest.php +++ b/tests/Cache/E2E/RedisClusterTest.php @@ -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; diff --git a/tests/Cache/E2E/RedisTest.php b/tests/Cache/E2E/RedisTest.php index f2d4644526..a71ebac143 100644 --- a/tests/Cache/E2E/RedisTest.php +++ b/tests/Cache/E2E/RedisTest.php @@ -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(); diff --git a/tests/Cache/E2E/ShardingTest.php b/tests/Cache/E2E/ShardingTest.php index a8e0a773d3..93dc9ff634 100644 --- a/tests/Cache/E2E/ShardingTest.php +++ b/tests/Cache/E2E/ShardingTest.php @@ -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 = []; diff --git a/tests/Cache/Scope/EmptyObjectFidelity.php b/tests/Cache/Scope/EmptyObjectFidelity.php new file mode 100644 index 0000000000..7841bc5833 --- /dev/null +++ b/tests/Cache/Scope/EmptyObjectFidelity.php @@ -0,0 +1,32 @@ + 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); + } +} diff --git a/tests/Cache/Unit/RedisEnvelopeTest.php b/tests/Cache/Unit/RedisEnvelopeTest.php index 1445512d18..5ef9210d65 100644 --- a/tests/Cache/Unit/RedisEnvelopeTest.php +++ b/tests/Cache/Unit/RedisEnvelopeTest.php @@ -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);