Skip to content

Commit 5b89765

Browse files
committed
bug #1145 [Chat] Fix MongoDB identifier context in MessageNormalizer denormalize (camilleislasse)
This PR was merged into the main branch. Discussion ---------- [Chat] Fix MongoDB identifier context in MessageNormalizer denormalize | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Docs? | no | Issues | - | License | MIT The `MessageNormalizer::denormalize()` method was ignoring the `identifier` context option, always using hardcoded `'id'` key to restore the UUID. This caused issues with MongoDB store which uses `_id` as the identifier (MongoDB's primary key convention). When saving messages, the store correctly passed `context: ['identifier' => '_id']` to `normalize()`, but `load()` called `denormalize()` without context, and even if it did, the method ignored it. **Changes:** - `MessageNormalizer::denormalize()` now uses `$context['identifier'] ?? 'id'` to read the UUID - `MongoDb\MessageStore::load()` now passes the `identifier` context to `denormalize()` - Fixed the MongoDB test to simulate real MongoDB behavior (data with `_id` key) - Added test for custom identifier roundtrip Commits ------- b985024 [Chat] Fix MongoDB identifier context in MessageNormalizer denormalize
2 parents 8ee03e5 + b985024 commit 5b89765

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

src/chat/src/Bridge/MongoDb/MessageStore.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ public function load(): MessageBag
7979
]);
8080

8181
return new MessageBag(...array_map(
82-
fn (array $message): MessageInterface => $this->serializer->denormalize($message, MessageInterface::class),
82+
fn (array $message): MessageInterface => $this->serializer->denormalize($message, MessageInterface::class, context: [
83+
'identifier' => '_id',
84+
]),
8385
$cursor->toArray(),
8486
));
8587
}

src/chat/src/MessageNormalizer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
7474
default => throw new LogicException(\sprintf('Unknown message type "%s".', $type)),
7575
};
7676

77+
$identifier = $context['identifier'] ?? 'id';
7778
/** @var AbstractUid&TimeBasedUidInterface&Uuid $existingUuid */
78-
$existingUuid = Uuid::fromString($data['id']);
79+
$existingUuid = Uuid::fromString($data[$identifier]);
7980

8081
$messageWithExistingUuid = $message->withId($existingUuid);
8182

src/chat/tests/Bridge/MongoDb/MessageStoreTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function testMessageStoreCanLoad()
8989

9090
$cursor = $this->createMock(CursorInterface::class);
9191
$cursor->expects($this->once())->method('toArray')->willReturn([
92-
$serializer->normalize(Message::ofUser('Hello world')),
92+
$serializer->normalize(Message::ofUser('Hello world'), context: ['identifier' => '_id']),
9393
]);
9494

9595
$collection = $this->createMock(Collection::class);

src/chat/tests/MessageNormalizerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,19 @@ public function testItCanDenormalize()
7676
$this->assertSame(Role::User, $message->getRole());
7777
$this->assertArrayHasKey('addedAt', $message->getMetadata()->all());
7878
}
79+
80+
public function testItCanDenormalizeWithCustomIdentifier()
81+
{
82+
$normalizer = new MessageNormalizer();
83+
$message = Message::ofUser('Hello World');
84+
85+
// Normalize with _id (like MongoDB)
86+
$payload = $normalizer->normalize($message, context: ['identifier' => '_id']);
87+
$this->assertArrayHasKey('_id', $payload);
88+
$this->assertArrayNotHasKey('id', $payload);
89+
90+
$denormalized = $normalizer->denormalize($payload, MessageInterface::class, context: ['identifier' => '_id']);
91+
92+
$this->assertSame($message->getId()->toRfc4122(), $denormalized->getId()->toRfc4122());
93+
}
7994
}

0 commit comments

Comments
 (0)