Skip to content

Commit 999c209

Browse files
abnegateclaude
andcommitted
(fix): convert associative arrays to Documents in relationship hooks
Wrap Document construction from associative arrays in try/catch to convert StructureException to RelationshipException. Arrays with invalid $id (null) throw StructureException at the Document constructor before relationship validation can run. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent a07ec0e commit 999c209

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/Database/Hook/Relationships.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Utopia\Database\Exception\Query as QueryException;
1212
use Utopia\Database\Exception\Relationship as RelationshipException;
1313
use Utopia\Database\Exception\Restricted as RestrictedException;
14+
use Utopia\Database\Exception\Structure as StructureException;
1415
use Utopia\Database\Helpers\Permission;
1516
use Utopia\Database\Helpers\Role;
1617
use Utopia\Database\Operator;
@@ -148,7 +149,11 @@ public function afterDocumentCreate(Document $collection, Document $document): D
148149

149150
try {
150151
if (\is_array($value) && ! \array_is_list($value)) {
151-
$value = new Document($value); // @phpstan-ignore argument.type
152+
try {
153+
$value = new Document($value); // @phpstan-ignore argument.type
154+
} catch (StructureException $e) {
155+
throw new RelationshipException('Invalid relationship value. ' . $e->getMessage());
156+
}
152157
$document->setAttribute($key, $value);
153158
}
154159

@@ -286,7 +291,11 @@ public function afterDocumentUpdate(Document $collection, Document $old, Documen
286291
$value = $document->getAttribute($key);
287292

288293
if (\is_array($value) && ! \array_is_list($value)) {
289-
$value = new Document($value); // @phpstan-ignore argument.type
294+
try {
295+
$value = new Document($value); // @phpstan-ignore argument.type
296+
} catch (StructureException $e) {
297+
throw new RelationshipException('Invalid relationship value. ' . $e->getMessage());
298+
}
290299
$document->setAttribute($key, $value);
291300
}
292301

0 commit comments

Comments
 (0)