From c2aa880ae065e63ae80c9bd20a578a371a19c076 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Wed, 12 Aug 2026 16:40:46 +1200 Subject: [PATCH] (fix): preserve empty document objects --- .../Collections/Documents/Action.php | 39 +++++ .../Collections/Documents/Bulk/Update.php | 6 +- .../Collections/Documents/Bulk/Upsert.php | 1 + .../Collections/Documents/Create.php | 14 +- .../Collections/Documents/Update.php | 4 +- .../Collections/Documents/Upsert.php | 8 +- .../DocumentsDBObjectFidelityTest.php | 152 ++++++++++++++++++ 7 files changed, 202 insertions(+), 22 deletions(-) create mode 100644 tests/e2e/Services/Databases/DocumentsDB/DocumentsDBObjectFidelityTest.php diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php index 48ab9b298f..54d5f06db8 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php @@ -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 + */ + 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. diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Update.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Update.php index 789fef8fcb..62a3e9777a 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Update.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Update.php @@ -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()); diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Upsert.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Upsert.php index 0530535729..9c0eb232b6 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Upsert.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Upsert.php @@ -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); } diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php index 5c761fa55c..4ab3bd407a 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php @@ -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); } } diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php index 9fb1952428..66ffb504e9 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php @@ -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()); diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php index 6bedae4a31..d7cc46449d 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php @@ -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()); diff --git a/tests/e2e/Services/Databases/DocumentsDB/DocumentsDBObjectFidelityTest.php b/tests/e2e/Services/Databases/DocumentsDB/DocumentsDBObjectFidelityTest.php new file mode 100644 index 0000000000..ade7a86471 --- /dev/null +++ b/tests/e2e/Services/Databases/DocumentsDB/DocumentsDBObjectFidelityTest.php @@ -0,0 +1,152 @@ + '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 + */ + 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); + } +}