Merge pull request #13183 from appwrite/fix/dat-2310-empty-object-fidelity

fix(databases): preserve empty nested document objects (DAT-2310)
This commit is contained in:
Jake Barnby authored and GitHub committed 2026-08-13 00:06:20 +12:00
commit 8e04766bf7
8 files changed
+227 -47

No files matched your search

Generated
+25 -25
View File
@@ -3272,16 +3272,16 @@
},
{
"name": "utopia-php/cache",
"version": "4.0.1",
"version": "4.0.2",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/cache.git",
"reference": "6f8cb03a5cb76e95b95a05103cd14cf782b8060f"
"reference": "92e02dab63606234b993b841ebf4c58845dd4620"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/cache/zipball/6f8cb03a5cb76e95b95a05103cd14cf782b8060f",
"reference": "6f8cb03a5cb76e95b95a05103cd14cf782b8060f",
"url": "https://api.github.com/repos/utopia-php/cache/zipball/92e02dab63606234b993b841ebf4c58845dd4620",
"reference": "92e02dab63606234b993b841ebf4c58845dd4620",
"shasum": ""
},
"require": {
@@ -3325,9 +3325,9 @@
],
"support": {
"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",
@@ -3638,16 +3638,16 @@
},
{
"name": "utopia-php/database",
"version": "7.1.3",
"version": "7.1.4",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/database.git",
"reference": "ce4e14e11cf8f9e6897015d5a7f8bb44a4b74949"
"reference": "b2b136a3827ccee2ea4eb83626348db4992286e1"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/database/zipball/ce4e14e11cf8f9e6897015d5a7f8bb44a4b74949",
"reference": "ce4e14e11cf8f9e6897015d5a7f8bb44a4b74949",
"url": "https://api.github.com/repos/utopia-php/database/zipball/b2b136a3827ccee2ea4eb83626348db4992286e1",
"reference": "b2b136a3827ccee2ea4eb83626348db4992286e1",
"shasum": ""
},
"require": {
@@ -3692,9 +3692,9 @@
],
"support": {
"issues": "https://github.com/utopia-php/database/issues",
"source": "https://github.com/utopia-php/database/tree/7.1.3"
"source": "https://github.com/utopia-php/database/tree/7.1.4"
},
"time": "2026-08-10T12:17:57+00:00"
"time": "2026-08-12T09:39:29+00:00"
},
{
"name": "utopia-php/detector",
@@ -4058,16 +4058,16 @@
},
{
"name": "utopia-php/http",
"version": "2.0.0-rc21",
"version": "2.0.0-rc22",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/http.git",
"reference": "9d8d374926dd2d3c35808deb72199ab943355d64"
"reference": "cc791f56bfa1114d8e5c721f68eb4ad581140883"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/http/zipball/9d8d374926dd2d3c35808deb72199ab943355d64",
"reference": "9d8d374926dd2d3c35808deb72199ab943355d64",
"url": "https://api.github.com/repos/utopia-php/http/zipball/cc791f56bfa1114d8e5c721f68eb4ad581140883",
"reference": "cc791f56bfa1114d8e5c721f68eb4ad581140883",
"shasum": ""
},
"require": {
@@ -4105,9 +4105,9 @@
],
"support": {
"issues": "https://github.com/utopia-php/http/issues",
"source": "https://github.com/utopia-php/http/tree/2.0.0-rc21"
"source": "https://github.com/utopia-php/http/tree/2.0.0-rc22"
},
"time": "2026-08-10T04:51:03+00:00"
"time": "2026-08-12T06:51:48+00:00"
},
{
"name": "utopia-php/image",
@@ -4416,16 +4416,16 @@
},
{
"name": "utopia-php/mongo",
"version": "1.5.1",
"version": "1.5.2",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/mongo.git",
"reference": "3ece830d1d72d2c9f68c656738e285629b5c72a9"
"reference": "20f9a644a356599fdd6265ba48696fc42a1f1881"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/mongo/zipball/3ece830d1d72d2c9f68c656738e285629b5c72a9",
"reference": "3ece830d1d72d2c9f68c656738e285629b5c72a9",
"url": "https://api.github.com/repos/utopia-php/mongo/zipball/20f9a644a356599fdd6265ba48696fc42a1f1881",
"reference": "20f9a644a356599fdd6265ba48696fc42a1f1881",
"shasum": ""
},
"require": {
@@ -4471,9 +4471,9 @@
],
"support": {
"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/platform",
@@ -8439,5 +8439,5 @@
"platform-dev": {
"ext-fileinfo": "*"
},
"plugin-api-version": "2.6.0"
"plugin-api-version": "2.9.0"
}
@@ -257,6 +257,45 @@ abstract class Action extends DatabasesAction
return $resource . 'Id';
}
/**
* Convert a request object to the associative shape Document expects while
* retaining empty objects at any nested depth.
*
* @return array<string, mixed>
*/
protected function normalizeData(string|array|\stdClass $data): array
{
if (\is_string($data)) {
$data = \json_decode($data);
if (!$data instanceof \stdClass && !\is_array($data)) {
return [];
}
}
if ($data instanceof \stdClass) {
$data = (array) $data;
}
return \array_map($this->normalizeValue(...), $data);
}
private function normalizeValue(mixed $value): mixed
{
if ($value instanceof \stdClass) {
$properties = (array) $value;
return $properties === []
? $value
: \array_map($this->normalizeValue(...), $properties);
}
if (\is_array($value)) {
return \array_map($this->normalizeValue(...), $value);
}
return $value;
}
/**
* Remove configured removable attributes from a document.
* Used for relationship path handling to remove API-specific attributes.
@@ -93,11 +93,9 @@ class Update extends Action
->callback($this->action(...));
}
public function action(string $databaseId, string $collectionId, string|array $data, array $queries, ?string $transactionId, UtopiaResponse $response, Database $dbForProject, callable $getDatabasesDB, Context $usage, Event $queueForEvents, Event $queueForRealtime, FunctionPublisher $publisherForFunctions, Event $queueForWebhooks, array $plan, EventProcessor $eventProcessor): void
public function action(string $databaseId, string $collectionId, string|array|\stdClass $data, array $queries, ?string $transactionId, UtopiaResponse $response, Database $dbForProject, callable $getDatabasesDB, Context $usage, Event $queueForEvents, Event $queueForRealtime, FunctionPublisher $publisherForFunctions, Event $queueForWebhooks, array $plan, EventProcessor $eventProcessor): void
{
$data = \is_string($data)
? \json_decode($data, true)
: $data;
$data = $this->normalizeData($data);
if (empty($data)) {
throw new Exception($this->getMissingPayloadException());
@@ -113,6 +113,7 @@ class Upsert extends Action
}
foreach ($documents as $key => $document) {
$document = $this->normalizeData($document);
if ($transactionId === null) {
$document = $this->parseOperators($document, $collection);
}
@@ -147,20 +147,14 @@ class Create extends Action
->callback($this->action(...));
}
public function action(string $databaseId, string $documentId, string $collectionId, string|array $data, ?array $permissions, ?array $documents, ?string $transactionId, UtopiaResponse $response, Database $dbForProject, callable $getDatabasesDB, User $user, Event $queueForEvents, Context $usage, Event $queueForRealtime, FunctionPublisher $publisherForFunctions, Event $queueForWebhooks, array $plan, Authorization $authorization, EventProcessor $eventProcessor): void
public function action(string $databaseId, string $documentId, string $collectionId, string|array|\stdClass $data, ?array $permissions, ?array $documents, ?string $transactionId, UtopiaResponse $response, Database $dbForProject, callable $getDatabasesDB, User $user, Event $queueForEvents, Context $usage, Event $queueForRealtime, FunctionPublisher $publisherForFunctions, Event $queueForWebhooks, array $plan, Authorization $authorization, EventProcessor $eventProcessor): void
{
// The JSON object validators guarantee object shape for both parameters, so
// decoding is all that is left. Encoded scalars and lists are rejected during
// validation, before the handler adds system fields or touches the database.
$data = \is_string($data)
? \json_decode($data, true)
: $data;
$data = $this->normalizeData($data);
if ($documents !== null) {
foreach ($documents as $key => $document) {
if (\is_string($document)) {
$documents[$key] = \json_decode($document, true);
}
/** @var string|array|\stdClass $document */
$documents[$key] = $this->normalizeData($document);
}
}
@@ -94,9 +94,9 @@ class Update extends Action
->callback($this->action(...));
}
public function action(string $databaseId, string $collectionId, string $documentId, string|array $data, ?array $permissions, ?string $transactionId, ?\DateTime $requestTimestamp, UtopiaResponse $response, Database $dbForProject, callable $getDatabasesDB, Event $queueForEvents, Context $usage, TransactionState $transactionState, array $plan, Authorization $authorization, User $user): void
public function action(string $databaseId, string $collectionId, string $documentId, string|array|\stdClass $data, ?array $permissions, ?string $transactionId, ?\DateTime $requestTimestamp, UtopiaResponse $response, Database $dbForProject, callable $getDatabasesDB, Event $queueForEvents, Context $usage, TransactionState $transactionState, array $plan, Authorization $authorization, User $user): void
{
$data = (\is_string($data)) ? \json_decode($data, true) : $data; // Cast to JSON array
$data = $this->normalizeData($data);
if (empty($data) && \is_null($permissions)) {
throw new Exception($this->getMissingPayloadException());
@@ -97,18 +97,14 @@ class Upsert extends Action
->callback($this->action(...));
}
public function action(string $databaseId, string $collectionId, string $documentId, string|array $data, ?array $permissions, ?string $transactionId, ?\DateTime $requestTimestamp, UtopiaResponse $response, User $user, Database $dbForProject, callable $getDatabasesDB, Event $queueForEvents, Context $usage, TransactionState $transactionState, array $plan, Authorization $authorization): void
public function action(string $databaseId, string $collectionId, string $documentId, string|array|\stdClass $data, ?array $permissions, ?string $transactionId, ?\DateTime $requestTimestamp, UtopiaResponse $response, User $user, Database $dbForProject, callable $getDatabasesDB, Event $queueForEvents, Context $usage, TransactionState $transactionState, array $plan, Authorization $authorization): void
{
$data = (\is_string($data)) ? \json_decode($data, true) : $data; // Cast to JSON array
$data = $this->normalizeData($data);
if (empty($data) && \is_null($permissions)) {
throw new Exception($this->getMissingPayloadException());
}
if (\array_is_list($data) && \count($data) > 1) { // Allow 1 associated array
throw new Exception($this->getMissingPayloadException());
}
$isAPIKey = $user->isKey($authorization->getRoles());
$isPrivilegedUser = $user->isPrivileged($authorization->getRoles());
@@ -0,0 +1,152 @@
<?php
declare(strict_types=1);
namespace Tests\E2E\Services\Databases\DocumentsDB;
use Tests\E2E\Client;
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideServer;
use Utopia\Database\Helpers\ID;
final class DocumentsDBObjectFidelityTest extends Scope
{
use ProjectCustom;
use SideServer;
public function testEmptyObjectFidelity(): void
{
$headers = [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
];
$databaseId = ID::unique();
$collectionId = ID::unique();
$database = $this->client->call(Client::METHOD_POST, '/documentsdb', $headers, [
'databaseId' => $databaseId,
'name' => 'Object Fidelity',
]);
$this->assertSame(201, $database['headers']['status-code']);
try {
$collection = $this->client->call(Client::METHOD_POST, "/documentsdb/{$databaseId}/collections", $headers, [
'collectionId' => $collectionId,
'name' => 'Objects',
]);
$this->assertSame(201, $collection['headers']['status-code']);
$empty = $this->client->call(Client::METHOD_POST, "/documentsdb/{$databaseId}/collections/{$collectionId}/documents", $headers, [
'documentId' => 'empty',
'data' => new \stdClass(),
]);
$this->assertSame(201, $empty['headers']['status-code']);
$created = $this->client->call(
Client::METHOD_POST,
"/documentsdb/{$databaseId}/collections/{$collectionId}/documents",
$headers,
[
'documentId' => 'created',
'data' => ['shape' => $this->shape()],
],
false,
);
$this->assertSame(201, $created['headers']['status-code']);
$this->assertShape($created['body']);
for ($read = 0; $read < 2; $read++) {
$fetched = $this->client->call(
Client::METHOD_GET,
"/documentsdb/{$databaseId}/collections/{$collectionId}/documents/created",
$headers,
decode: false,
);
$this->assertSame(200, $fetched['headers']['status-code']);
$this->assertShape($fetched['body']);
}
$updated = $this->client->call(
Client::METHOD_PATCH,
"/documentsdb/{$databaseId}/collections/{$collectionId}/documents/created",
$headers,
['data' => \json_encode(['shape' => $this->shape()], JSON_THROW_ON_ERROR)],
false,
);
$this->assertSame(200, $updated['headers']['status-code']);
$this->assertShape($updated['body']);
$upserted = $this->client->call(
Client::METHOD_PUT,
"/documentsdb/{$databaseId}/collections/{$collectionId}/documents/upserted",
$headers,
['data' => ['shape' => $this->shape()]],
false,
);
$this->assertContains($upserted['headers']['status-code'], [200, 201]);
$this->assertShape($upserted['body']);
$bulkCreated = $this->client->call(
Client::METHOD_POST,
"/documentsdb/{$databaseId}/collections/{$collectionId}/documents",
$headers,
['documents' => [\json_encode(['$id' => 'bulk-created', 'shape' => $this->shape()], JSON_THROW_ON_ERROR)]],
false,
);
$this->assertSame(201, $bulkCreated['headers']['status-code']);
$this->assertShape($bulkCreated['body'], true);
$bulkUpdated = $this->client->call(
Client::METHOD_PATCH,
"/documentsdb/{$databaseId}/collections/{$collectionId}/documents",
$headers,
['data' => \json_encode(['shape' => $this->shape()], JSON_THROW_ON_ERROR)],
false,
);
$this->assertSame(200, $bulkUpdated['headers']['status-code']);
$this->assertShape($bulkUpdated['body'], true);
$bulkUpserted = $this->client->call(
Client::METHOD_PUT,
"/documentsdb/{$databaseId}/collections/{$collectionId}/documents",
$headers,
['documents' => [['$id' => 'bulk-upserted', 'shape' => $this->shape()]]],
false,
);
$this->assertContains($bulkUpserted['headers']['status-code'], [200, 201]);
$this->assertShape($bulkUpserted['body'], true);
} finally {
$this->client->call(Client::METHOD_DELETE, "/documentsdb/{$databaseId}", $headers);
}
}
/**
* @return array<string, mixed>
*/
private function shape(): array
{
return [
'empty' => new \stdClass(),
'nested' => ['empty' => new \stdClass()],
'list' => [new \stdClass(), ['x' => 1]],
'emptyArray' => [],
];
}
private function assertShape(string $body, bool $bulk = false): void
{
$decoded = \json_decode($body, flags: JSON_THROW_ON_ERROR);
if ($bulk) {
$decoded = $decoded->documents[0];
}
$shape = $decoded->shape;
$this->assertInstanceOf(\stdClass::class, $shape->empty);
$this->assertInstanceOf(\stdClass::class, $shape->nested->empty);
$this->assertInstanceOf(\stdClass::class, $shape->list[0]);
$this->assertSame(1, $shape->list[1]->x);
$this->assertSame([], $shape->emptyArray);
}
}