diff --git a/composer.lock b/composer.lock index f960ddbd42..1841e42ac4 100644 --- a/composer.lock +++ b/composer.lock @@ -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" } 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); + } +}