mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
(fix): preserve empty document objects
This commit is contained in:
1 parent
ac63900560
commit
c2aa880ae0
7 files changed
+202
-22
No files matched your search
+39
@@ -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.
|
||||
|
||||
+2
-4
@@ -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());
|
||||
|
||||
+1
@@ -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);
|
||||
}
|
||||
|
||||
+4
-10
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -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());
|
||||
|
||||
+2
-6
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user