Merge pull request #935 from utopia-php/fix/dat-2310-empty-object-fidelity

fix: preserve empty object attributes (DAT-2310)
This commit is contained in:
Jake Barnby authored and GitHub committed 2026-08-12 21:39:29 +12:00
commit b2b136a382
6 files changed
+174 -19

No files matched your search

Generated
+13 -13
View File
@@ -2036,16 +2036,16 @@
}, },
{ {
"name": "utopia-php/cache", "name": "utopia-php/cache",
"version": "4.0.1", "version": "4.0.2",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/utopia-php/cache.git", "url": "https://github.com/utopia-php/cache.git",
"reference": "6f8cb03a5cb76e95b95a05103cd14cf782b8060f" "reference": "92e02dab63606234b993b841ebf4c58845dd4620"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/utopia-php/cache/zipball/6f8cb03a5cb76e95b95a05103cd14cf782b8060f", "url": "https://api.github.com/repos/utopia-php/cache/zipball/92e02dab63606234b993b841ebf4c58845dd4620",
"reference": "6f8cb03a5cb76e95b95a05103cd14cf782b8060f", "reference": "92e02dab63606234b993b841ebf4c58845dd4620",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@@ -2089,9 +2089,9 @@
], ],
"support": { "support": {
"issues": "https://github.com/utopia-php/cache/issues", "issues": "https://github.com/utopia-php/cache/issues",
"source": "https://github.com/utopia-php/cache/tree/4.0.1" "source": "https://github.com/utopia-php/cache/tree/4.0.2"
}, },
"time": "2026-08-04T00:06:02+00:00" "time": "2026-08-12T07:48:59+00:00"
}, },
{ {
"name": "utopia-php/circuit-breaker", "name": "utopia-php/circuit-breaker",
@@ -2202,16 +2202,16 @@
}, },
{ {
"name": "utopia-php/mongo", "name": "utopia-php/mongo",
"version": "1.5.1", "version": "1.5.2",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/utopia-php/mongo.git", "url": "https://github.com/utopia-php/mongo.git",
"reference": "3ece830d1d72d2c9f68c656738e285629b5c72a9" "reference": "20f9a644a356599fdd6265ba48696fc42a1f1881"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/utopia-php/mongo/zipball/3ece830d1d72d2c9f68c656738e285629b5c72a9", "url": "https://api.github.com/repos/utopia-php/mongo/zipball/20f9a644a356599fdd6265ba48696fc42a1f1881",
"reference": "3ece830d1d72d2c9f68c656738e285629b5c72a9", "reference": "20f9a644a356599fdd6265ba48696fc42a1f1881",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@@ -2257,9 +2257,9 @@
], ],
"support": { "support": {
"issues": "https://github.com/utopia-php/mongo/issues", "issues": "https://github.com/utopia-php/mongo/issues",
"source": "https://github.com/utopia-php/mongo/tree/1.5.1" "source": "https://github.com/utopia-php/mongo/tree/1.5.2"
}, },
"time": "2026-08-02T00:46:11+00:00" "time": "2026-08-12T06:57:25+00:00"
}, },
{ {
"name": "utopia-php/pools", "name": "utopia-php/pools",
@@ -4648,5 +4648,5 @@
"ext-redis": "*" "ext-redis": "*"
}, },
"platform-dev": {}, "platform-dev": {},
"plugin-api-version": "2.6.0" "plugin-api-version": "2.9.0"
} }
+3 -1
View File
@@ -1401,7 +1401,9 @@ class Mongo extends Adapter
private function convertStdClassToArray(mixed $value): mixed private function convertStdClassToArray(mixed $value): mixed
{ {
if (is_object($value) && get_class($value) === stdClass::class) { if (is_object($value) && get_class($value) === stdClass::class) {
return array_map($this->convertStdClassToArray(...), get_object_vars($value)); $properties = get_object_vars($value);
return $properties === [] ? $value : array_map($this->convertStdClassToArray(...), $properties);
} }
if (is_array($value)) { if (is_array($value)) {
+53 -5
View File
@@ -669,7 +669,7 @@ class Database
}, },
/** /**
* @param string|null $value * @param string|null $value
* @return array|null * @return mixed
*/ */
function (?string $value) { function (?string $value) {
if (is_null($value)) { if (is_null($value)) {
@@ -690,7 +690,7 @@ class Database
* @return mixed * @return mixed
*/ */
function (mixed $value) { function (mixed $value) {
if (!\is_array($value)) { if (!\is_array($value) && !$value instanceof \stdClass) {
return $value; return $value;
} }
@@ -708,12 +708,60 @@ class Database
if (!is_string($value)) { if (!is_string($value)) {
return $value; return $value;
} }
$decoded = json_decode($value, true); $decoded = self::decodeObject($value);
return is_array($decoded) ? $decoded : $value;
return is_array($decoded) || $decoded instanceof \stdClass ? $decoded : $value;
} }
); );
} }
private static function decodeObject(string $value): mixed
{
if (preg_match('/\{\s*\}/', $value) === 0) {
return json_decode($value, true);
}
return self::toAssociative(json_decode($value));
}
private static function toAssociative(mixed $value): mixed
{
if ($value instanceof \stdClass) {
$properties = (array)$value;
return $properties === [] ? $value : array_map(self::toAssociative(...), $properties);
}
if (is_array($value)) {
return array_map(self::toAssociative(...), $value);
}
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 * Add listener to events
* Passing a null $callback will remove the listener * Passing a null $callback will remove the listener
@@ -6360,7 +6408,7 @@ class Database
$oldValue = $old->getAttribute($key); $oldValue = $old->getAttribute($key);
if ($value !== $oldValue) { if (!self::valuesEqual($value, $oldValue)) {
$shouldUpdate = true; $shouldUpdate = true;
break; break;
} }
@@ -27,6 +27,10 @@ class ObjectValidator extends Validator
return json_last_error() === JSON_ERROR_NONE; return json_last_error() === JSON_ERROR_NONE;
} }
if ($value instanceof \stdClass) {
return true;
}
// Allow empty or associative arrays (non-list) // Allow empty or associative arrays (non-list)
return empty($value) || (is_array($value) && !array_is_list($value)); return empty($value) || (is_array($value) && !array_is_list($value));
} }
@@ -5,6 +5,7 @@ namespace Tests\E2E\Adapter\Scopes;
use Exception; use Exception;
use Utopia\Database\Database; use Utopia\Database\Database;
use Utopia\Database\Document; use Utopia\Database\Document;
use Utopia\Database\Exception\Authorization as AuthorizationException;
use Utopia\Database\Exception\Duplicate as DuplicateException; use Utopia\Database\Exception\Duplicate as DuplicateException;
use Utopia\Database\Exception\Index as IndexException; use Utopia\Database\Exception\Index as IndexException;
use Utopia\Database\Exception\Query as QueryException; use Utopia\Database\Exception\Query as QueryException;
@@ -963,6 +964,105 @@ trait ObjectAttributeTests
$database->deleteCollection($collectionId); $database->deleteCollection($collectionId);
} }
public function testObjectAttributeEmptyObject(): void
{
/** @var Database $database */
$database = static::getDatabase();
if (!$database->getAdapter()->getSupportForObject()) {
$this->markTestSkipped('Adapter does not support object attributes');
}
$collectionId = ID::unique();
$database->createCollection($collectionId);
$this->createAttribute($database, $collectionId, 'meta', Database::VAR_OBJECT, 0, false);
$created = $database->createDocument($collectionId, new Document([
'$id' => 'emptyObject',
'$permissions' => [Permission::read(Role::any())],
'meta' => new \stdClass(),
]));
$this->assertSame('{}', json_encode($created->getAttribute('meta')));
$database->purgeCachedDocument($collectionId, 'emptyObject');
$read = $database->getDocument($collectionId, 'emptyObject');
$this->assertSame('{}', json_encode($read->getAttribute('meta')));
$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);
}
public function testObjectAttributeNestedEmptyObjects(): void
{
/** @var Database $database */
$database = static::getDatabase();
if (!$database->getAdapter()->getSupportForObject()) {
$this->markTestSkipped('Adapter does not support object attributes');
}
$collectionId = ID::unique();
$database->createCollection($collectionId);
$this->createAttribute($database, $collectionId, 'meta', Database::VAR_OBJECT, 0, false);
$created = $database->createDocument($collectionId, new Document([
'$id' => 'nestedEmptyObjects',
'$permissions' => [Permission::read(Role::any())],
'meta' => [
'inner' => new \stdClass(),
'arr' => [new \stdClass(), ['x' => 1]],
'emptyArray' => [],
],
]));
$createdMeta = $created->getAttribute('meta');
$this->assertSame('{}', json_encode($createdMeta['inner']));
$this->assertSame('{}', json_encode($createdMeta['arr'][0]));
$this->assertSame('{"x":1}', json_encode($createdMeta['arr'][1]));
$this->assertSame('[]', json_encode($createdMeta['emptyArray']));
$database->purgeCachedDocument($collectionId, 'nestedEmptyObjects');
$readMeta = $database->getDocument($collectionId, 'nestedEmptyObjects')->getAttribute('meta');
$this->assertSame('{}', json_encode($readMeta['inner']));
$this->assertSame('{}', json_encode($readMeta['arr'][0]));
$this->assertSame('{"x":1}', json_encode($readMeta['arr'][1]));
$this->assertSame('[]', json_encode($readMeta['emptyArray']));
$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);
}
public function testMetadataWithVector(): void public function testMetadataWithVector(): void
{ {
/** @var Database $database */ /** @var Database $database */
+1
View File
@@ -64,6 +64,7 @@ class ObjectTest extends TestCase
$validator = new ObjectValidator(); $validator = new ObjectValidator();
$this->assertTrue($validator->isValid([])); $this->assertTrue($validator->isValid([]));
$this->assertTrue($validator->isValid(new \stdClass()));
$this->assertFalse($validator->isValid('sldfjsdlfj')); $this->assertFalse($validator->isValid('sldfjsdlfj'));
} }