mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
(fix): preserve empty object attributes
This commit is contained in:
1 parent
ce4e14e11c
commit
c96d5de857
5 files changed
+112
-5
No files matched your search
@@ -1401,7 +1401,9 @@ class Mongo extends Adapter
|
||||
private function convertStdClassToArray(mixed $value): mixed
|
||||
{
|
||||
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)) {
|
||||
|
||||
@@ -669,7 +669,7 @@ class Database
|
||||
},
|
||||
/**
|
||||
* @param string|null $value
|
||||
* @return array|null
|
||||
* @return mixed
|
||||
*/
|
||||
function (?string $value) {
|
||||
if (is_null($value)) {
|
||||
@@ -690,7 +690,7 @@ class Database
|
||||
* @return mixed
|
||||
*/
|
||||
function (mixed $value) {
|
||||
if (!\is_array($value)) {
|
||||
if (!\is_array($value) && !$value instanceof \stdClass) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
@@ -708,12 +708,37 @@ class Database
|
||||
if (!is_string($value)) {
|
||||
return $value;
|
||||
}
|
||||
$decoded = json_decode($value, true);
|
||||
return is_array($decoded) ? $decoded : $value;
|
||||
$decoded = self::decodeObject($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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add listener to events
|
||||
* Passing a null $callback will remove the listener
|
||||
|
||||
@@ -27,6 +27,10 @@ class ObjectValidator extends Validator
|
||||
return json_last_error() === JSON_ERROR_NONE;
|
||||
}
|
||||
|
||||
if ($value instanceof \stdClass) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Allow empty or associative arrays (non-list)
|
||||
return empty($value) || (is_array($value) && !array_is_list($value));
|
||||
}
|
||||
|
||||
@@ -963,6 +963,81 @@ trait ObjectAttributeTests
|
||||
$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')));
|
||||
|
||||
$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']));
|
||||
|
||||
$cachedMeta = $database->getDocument($collectionId, 'nestedEmptyObjects')->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']));
|
||||
|
||||
$database->deleteCollection($collectionId);
|
||||
}
|
||||
|
||||
public function testMetadataWithVector(): void
|
||||
{
|
||||
/** @var Database $database */
|
||||
|
||||
@@ -64,6 +64,7 @@ class ObjectTest extends TestCase
|
||||
$validator = new ObjectValidator();
|
||||
|
||||
$this->assertTrue($validator->isValid([]));
|
||||
$this->assertTrue($validator->isValid(new \stdClass()));
|
||||
|
||||
$this->assertFalse($validator->isValid('sldfjsdlfj'));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user