Skip to content

Commit d13fe04

Browse files
committed
fix: validate array relation $id and improve test reliability
1 parent cb32dc4 commit d13fe04

5 files changed

Lines changed: 28 additions & 6 deletions

File tree

src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,9 @@ protected function validateRelationship(mixed $relation): void
261261
$relationId = $relation->getAttribute('$id');
262262
} elseif (\is_string($relation)) {
263263
$relationId = $relation;
264-
} elseif (!(\is_array($relation) && \array_values($relation) !== $relation)) {
264+
} elseif (\is_array($relation) && \array_values($relation) !== $relation) {
265+
$relationId = $relation['$id'] ?? null;
266+
} else {
265267
throw new Exception(Exception::RELATIONSHIP_VALUE_INVALID, 'Relationship value must be an object or document ID string, not ' . \gettype($relation));
266268
}
267269

src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,9 @@ public function action(string $databaseId, string $documentId, string $collectio
312312
$this->validateRelationship($relation);
313313

314314
if (\is_array($relation) && \array_values($relation) !== $relation) {
315-
$relation['$id'] = ID::unique();
315+
if (!isset($relation['$id'])) {
316+
$relation['$id'] = ID::unique();
317+
}
316318
$relation = new Document($relation);
317319
}
318320

src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,9 @@ public function action(string $databaseId, string $collectionId, string $documen
204204
$this->validateRelationship($relation);
205205

206206
if (\is_array($relation) && \array_values($relation) !== $relation) {
207-
$relation['$id'] = ID::unique();
207+
if (!isset($relation['$id'])) {
208+
$relation['$id'] = ID::unique();
209+
}
208210
$relation = new Document($relation);
209211
}
210212

src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,9 @@ public function action(string $databaseId, string $collectionId, string $documen
214214
$this->validateRelationship($relation);
215215

216216
if (\is_array($relation) && \array_values($relation) !== $relation) {
217-
$relation['$id'] = ID::unique();
217+
if (!isset($relation['$id'])) {
218+
$relation['$id'] = ID::unique();
219+
}
218220
$relation = new Document($relation);
219221
}
220222

tests/e2e/Services/Databases/TablesDB/DatabasesBase.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7680,7 +7680,7 @@ public function testInvalidRelationshipDocumentId(array $data): void
76807680
]);
76817681

76827682
// Create one-to-many relationship
7683-
$this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $parentTableId . '/columns/relationship', array_merge([
7683+
$relationship = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $parentTableId . '/columns/relationship', array_merge([
76847684
'content-type' => 'application/json',
76857685
'x-appwrite-project' => $this->getProject()['$id'],
76867686
'x-appwrite-key' => $this->getProject()['apiKey']
@@ -7690,8 +7690,22 @@ public function testInvalidRelationshipDocumentId(array $data): void
76907690
'twoWay' => false,
76917691
'key' => 'children',
76927692
]);
7693+
$this->assertEquals(202, $relationship['headers']['status-code']);
76937694

7694-
sleep(1);
7695+
// Wait for relationship column to be available
7696+
$maxAttempts = 10;
7697+
for ($i = 0; $i < $maxAttempts; $i++) {
7698+
$columns = $this->client->call(Client::METHOD_GET, '/tablesdb/' . $databaseId . '/tables/' . $parentTableId . '/columns', array_merge([
7699+
'content-type' => 'application/json',
7700+
'x-appwrite-project' => $this->getProject()['$id'],
7701+
'x-appwrite-key' => $this->getProject()['apiKey']
7702+
]));
7703+
$columnKeys = array_column($columns['body']['columns'], 'key');
7704+
if (in_array('children', $columnKeys)) {
7705+
break;
7706+
}
7707+
usleep(200000);
7708+
}
76957709

76967710
// ID too long (>36 chars) should fail
76977711
$response = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $parentTableId . '/rows', array_merge([

0 commit comments

Comments
 (0)