From 74d9af59947d1f3c6998fb23715bf2d8f1cf4e0b Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 12 Aug 2026 16:58:51 +1200 Subject: [PATCH] (test): verify empty object dependency chain --- .github/workflows/tests.yml | 3 +++ Dockerfile | 19 ++++++++++++- src/Database/Database.php | 25 ++++++++++++++++- .../Adapter/Scopes/ObjectAttributeTests.php | 27 ++++++++++++++++++- 4 files changed, 71 insertions(+), 3 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 318304d9d3..bf11f07a95 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -25,6 +25,9 @@ jobs: uses: docker/build-push-action@1104d471370f9806843c095c1db02b5a90c5f8b6 # v3.3.1 with: context: . + build-args: | + UTOPIA_CACHE_REFERENCE=d7c0806f9bbdafee794849398f65e8ece322711d + UTOPIA_MONGO_REFERENCE=daea8f8213c33f52e8b66d0f5366428bf2f5a4b8 push: false tags: ${{ env.IMAGE }} load: true diff --git a/Dockerfile b/Dockerfile index d43c2a167d..2c0c65e3dc 100755 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,8 @@ FROM composer:2.8 AS composer +ARG UTOPIA_CACHE_REFERENCE='' +ARG UTOPIA_MONGO_REFERENCE='' + WORKDIR /usr/local/src/ COPY composer.lock /usr/local/src/ @@ -10,7 +13,21 @@ RUN composer install \ --optimize-autoloader \ --no-plugins \ --no-scripts \ - --prefer-dist + --prefer-dist \ + && if [ -n "$UTOPIA_CACHE_REFERENCE" ] || [ -n "$UTOPIA_MONGO_REFERENCE" ]; then \ + test -n "$UTOPIA_CACHE_REFERENCE" \ + && test -n "$UTOPIA_MONGO_REFERENCE" \ + && git init /tmp/utopia-cache \ + && git -C /tmp/utopia-cache remote add origin https://github.com/utopia-php/monorepo.git \ + && git -C /tmp/utopia-cache fetch --depth 1 origin "$UTOPIA_CACHE_REFERENCE" \ + && git -C /tmp/utopia-cache checkout --detach FETCH_HEAD \ + && cp -R /tmp/utopia-cache/packages/cache/src/. vendor/utopia-php/cache/src/ \ + && git init /tmp/utopia-mongo \ + && git -C /tmp/utopia-mongo remote add origin https://github.com/utopia-php/mongo.git \ + && git -C /tmp/utopia-mongo fetch --depth 1 origin "$UTOPIA_MONGO_REFERENCE" \ + && git -C /tmp/utopia-mongo checkout --detach FETCH_HEAD \ + && cp -R /tmp/utopia-mongo/src/. vendor/utopia-php/mongo/src/; \ + fi FROM php:8.5.8-cli-alpine AS compile diff --git a/src/Database/Database.php b/src/Database/Database.php index a82425de80..760c1aeafe 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -739,6 +739,29 @@ class Database return $value; } + private static function valuesEqual(mixed $value, mixed $old): bool + { + if ($value instanceof \stdClass && $old instanceof \stdClass) { + return self::valuesEqual((array)$value, (array)$old); + } + + if (is_array($value) && is_array($old)) { + if (array_keys($value) !== array_keys($old)) { + return false; + } + + foreach ($value as $key => $item) { + if (!self::valuesEqual($item, $old[$key])) { + return false; + } + } + + return true; + } + + return $value === $old; + } + /** * Add listener to events * Passing a null $callback will remove the listener @@ -6385,7 +6408,7 @@ class Database $oldValue = $old->getAttribute($key); - if ($value !== $oldValue) { + if (!self::valuesEqual($value, $oldValue)) { $shouldUpdate = true; break; } diff --git a/tests/e2e/Adapter/Scopes/ObjectAttributeTests.php b/tests/e2e/Adapter/Scopes/ObjectAttributeTests.php index 095bf796bf..7c3ea359ad 100644 --- a/tests/e2e/Adapter/Scopes/ObjectAttributeTests.php +++ b/tests/e2e/Adapter/Scopes/ObjectAttributeTests.php @@ -5,6 +5,7 @@ namespace Tests\E2E\Adapter\Scopes; use Exception; use Utopia\Database\Database; use Utopia\Database\Document; +use Utopia\Database\Exception\Authorization as AuthorizationException; use Utopia\Database\Exception\Duplicate as DuplicateException; use Utopia\Database\Exception\Index as IndexException; use Utopia\Database\Exception\Query as QueryException; @@ -991,6 +992,21 @@ trait ObjectAttributeTests $cached = $database->getDocument($collectionId, 'emptyObject'); $this->assertSame('{}', json_encode($cached->getAttribute('meta'))); + $updated = $database->updateDocument($collectionId, 'emptyObject', new Document([ + 'meta' => new \stdClass(), + ])); + $this->assertSame($cached->getUpdatedAt(), $updated->getUpdatedAt()); + $this->assertSame('{}', json_encode($updated->getAttribute('meta'))); + + try { + $database->updateDocument($collectionId, 'emptyObject', new Document([ + 'meta' => [], + ])); + $this->fail('Changing an empty object to an empty array must require update permission'); + } catch (AuthorizationException) { + $this->addToAssertionCount(1); + } + $database->deleteCollection($collectionId); } @@ -1029,12 +1045,21 @@ trait ObjectAttributeTests $this->assertSame('{"x":1}', json_encode($readMeta['arr'][1])); $this->assertSame('[]', json_encode($readMeta['emptyArray'])); - $cachedMeta = $database->getDocument($collectionId, 'nestedEmptyObjects')->getAttribute('meta'); + $cached = $database->getDocument($collectionId, 'nestedEmptyObjects'); + $cachedMeta = $cached->getAttribute('meta'); $this->assertSame('{}', json_encode($cachedMeta['inner'])); $this->assertSame('{}', json_encode($cachedMeta['arr'][0])); $this->assertSame('{"x":1}', json_encode($cachedMeta['arr'][1])); $this->assertSame('[]', json_encode($cachedMeta['emptyArray'])); + $updatedMeta = $cachedMeta; + $updatedMeta['inner'] = new \stdClass(); + $updatedMeta['arr'][0] = new \stdClass(); + $updated = $database->updateDocument($collectionId, 'nestedEmptyObjects', new Document([ + 'meta' => $updatedMeta, + ])); + $this->assertSame($cached->getUpdatedAt(), $updated->getUpdatedAt()); + $database->deleteCollection($collectionId); }