diff --git a/src/Schema/JsonRpc/Notification.php b/src/Schema/JsonRpc/Notification.php index bfd37f1f..3ec0883a 100644 --- a/src/Schema/JsonRpc/Notification.php +++ b/src/Schema/JsonRpc/Notification.php @@ -74,7 +74,7 @@ public function jsonSerialize(): array $array['params'] = $params; } - if (null !== $this->meta && !isset($params['meta'])) { + if (null !== $this->meta && !isset($params['_meta'])) { $array['params']['_meta'] = $this->meta; } diff --git a/src/Schema/JsonRpc/Request.php b/src/Schema/JsonRpc/Request.php index cb8ed836..afdf9e97 100644 --- a/src/Schema/JsonRpc/Request.php +++ b/src/Schema/JsonRpc/Request.php @@ -122,7 +122,7 @@ public function jsonSerialize(): array $array['params'] = $params; } - if (null !== $this->meta && !isset($params['meta'])) { + if (null !== $this->meta && !isset($params['_meta'])) { $array['params']['_meta'] = $this->meta; } diff --git a/tests/Unit/Schema/JsonRpc/NotificationTest.php b/tests/Unit/Schema/JsonRpc/NotificationTest.php index 0d7d7c48..b2580de8 100644 --- a/tests/Unit/Schema/JsonRpc/NotificationTest.php +++ b/tests/Unit/Schema/JsonRpc/NotificationTest.php @@ -53,4 +53,44 @@ protected function getParams(): ?array $this->assertSame($expectedMeta, $notification->jsonSerialize()); } + + public function testMetaDoesNotOverrideMetaFromParams(): void + { + $notificationImplementation = new class extends Notification { + public static function getMethod(): string + { + return 'notifications/dummy'; + } + + public static function fromParams(?array $params): self + { + return new self(); + } + + protected function getParams(): array + { + return [ + '_meta' => ['key' => 'from-params'], + ]; + } + }; + + $notification = $notificationImplementation::fromArray([ + 'jsonrpc' => '2.0', + 'method' => 'notifications/dummy', + 'params' => [ + '_meta' => ['key' => 'from-meta'], + ], + ]); + + $expected = [ + 'jsonrpc' => '2.0', + 'method' => 'notifications/dummy', + 'params' => [ + '_meta' => ['key' => 'from-params'], + ], + ]; + + $this->assertSame($expected, $notification->jsonSerialize()); + } } diff --git a/tests/Unit/Schema/JsonRpc/RequestTest.php b/tests/Unit/Schema/JsonRpc/RequestTest.php index f400a904..d23b6a9b 100644 --- a/tests/Unit/Schema/JsonRpc/RequestTest.php +++ b/tests/Unit/Schema/JsonRpc/RequestTest.php @@ -55,4 +55,41 @@ protected function getParams(): ?array $this->assertSame($expectedMeta, $notification->jsonSerialize()); } + + public function testMetaDoesNotOverrideMetaFromParams(): void + { + $requestImplementation = new class extends Request { + public static function getMethod(): string + { + return 'foo/bar'; + } + + public static function fromParams(?array $params): static + { + return new self(); + } + + protected function getParams(): array + { + return [ + '_meta' => ['key' => 'from-params'], + ]; + } + }; + + $request = $requestImplementation::fromParams(null) + ->withId('12345') + ->withMeta(['key' => 'from-meta']); + + $expected = [ + 'jsonrpc' => '2.0', + 'id' => '12345', + 'method' => 'foo/bar', + 'params' => [ + '_meta' => ['key' => 'from-params'], + ], + ]; + + $this->assertSame($expected, $request->jsonSerialize()); + } }