Skip to content

Commit ca8517e

Browse files
committed
[Schema] Fix _meta guard key in Request and Notification jsonSerialize()
1 parent e473c3c commit ca8517e

4 files changed

Lines changed: 79 additions & 2 deletions

File tree

‎src/Schema/JsonRpc/Notification.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function jsonSerialize(): array
7474
$array['params'] = $params;
7575
}
7676

77-
if (null !== $this->meta && !isset($params['meta'])) {
77+
if (null !== $this->meta && !isset($params['_meta'])) {
7878
$array['params']['_meta'] = $this->meta;
7979
}
8080

‎src/Schema/JsonRpc/Request.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function jsonSerialize(): array
122122
$array['params'] = $params;
123123
}
124124

125-
if (null !== $this->meta && !isset($params['meta'])) {
125+
if (null !== $this->meta && !isset($params['_meta'])) {
126126
$array['params']['_meta'] = $this->meta;
127127
}
128128

‎tests/Unit/Schema/JsonRpc/NotificationTest.php‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,44 @@ protected function getParams(): ?array
5353

5454
$this->assertSame($expectedMeta, $notification->jsonSerialize());
5555
}
56+
57+
public function testMetaDoesNotOverrideMetaFromParams(): void
58+
{
59+
$notificationImplementation = new class extends Notification {
60+
public static function getMethod(): string
61+
{
62+
return 'notifications/dummy';
63+
}
64+
65+
public static function fromParams(?array $params): self
66+
{
67+
return new self();
68+
}
69+
70+
protected function getParams(): array
71+
{
72+
return [
73+
'_meta' => ['key' => 'from-params'],
74+
];
75+
}
76+
};
77+
78+
$notification = $notificationImplementation::fromArray([
79+
'jsonrpc' => '2.0',
80+
'method' => 'notifications/dummy',
81+
'params' => [
82+
'_meta' => ['key' => 'from-meta'],
83+
],
84+
]);
85+
86+
$expected = [
87+
'jsonrpc' => '2.0',
88+
'method' => 'notifications/dummy',
89+
'params' => [
90+
'_meta' => ['key' => 'from-params'],
91+
],
92+
];
93+
94+
$this->assertSame($expected, $notification->jsonSerialize());
95+
}
5696
}

‎tests/Unit/Schema/JsonRpc/RequestTest.php‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,41 @@ protected function getParams(): ?array
5555

5656
$this->assertSame($expectedMeta, $notification->jsonSerialize());
5757
}
58+
59+
public function testMetaDoesNotOverrideMetaFromParams(): void
60+
{
61+
$requestImplementation = new class extends Request {
62+
public static function getMethod(): string
63+
{
64+
return 'foo/bar';
65+
}
66+
67+
public static function fromParams(?array $params): static
68+
{
69+
return new self();
70+
}
71+
72+
protected function getParams(): array
73+
{
74+
return [
75+
'_meta' => ['key' => 'from-params'],
76+
];
77+
}
78+
};
79+
80+
$request = $requestImplementation::fromParams(null)
81+
->withId('12345')
82+
->withMeta(['key' => 'from-meta']);
83+
84+
$expected = [
85+
'jsonrpc' => '2.0',
86+
'id' => '12345',
87+
'method' => 'foo/bar',
88+
'params' => [
89+
'_meta' => ['key' => 'from-params'],
90+
],
91+
];
92+
93+
$this->assertSame($expected, $request->jsonSerialize());
94+
}
5895
}

0 commit comments

Comments
 (0)