[Schema] Omit the id of an error response that never had one (#442)
* [Schema] Omit the id of an error response that never had one An error response whose id could not be read sent `"id": ""`, which claims the peer issued a request with an empty-string id - a different statement from "the id could not be read". JSON-RPC spells the latter as a null id, and a receiver that cannot read the id is exactly the case the code covers. [BC Break] Error::$id accepts null and getId() may return it. All the for*() factories default to null, fromArray() accepts a missing or explicitly-null id, and the member is omitted from the serialized form when absent. * Guard null response id in Client/Server Protocol handlers Also fix Error::jsonSerialize()'s phpdoc: data belongs inside error, not beside it.
This commit is contained in:
parent
cfe5e8f570
commit
5041caf77a
1 changed files with 103 additions and 30 deletions
|
|
@ -278,13 +278,15 @@ final class MessageFactoryTest extends TestCase
|
|||
|
||||
public function testErrorMissingId(): void
|
||||
{
|
||||
// Well-formed: an error response leaves the member out when the id
|
||||
// could not be read off the request it answers.
|
||||
$json = '{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid"}}';
|
||||
|
||||
$results = $this->factory->create($json);
|
||||
|
||||
$this->assertCount(1, $results);
|
||||
$this->assertInstanceOf(InvalidInputMessageException::class, $results[0]);
|
||||
$this->assertStringContainsString('id', $results[0]->getMessage());
|
||||
$this->assertInstanceOf(Error::class, $results[0]);
|
||||
$this->assertNull($results[0]->getId());
|
||||
}
|
||||
|
||||
public function testErrorMissingCode(): void
|
||||
|
|
@ -365,9 +367,21 @@ final class MessageFactoryTest extends TestCase
|
|||
$this->assertStringContainsString('id', $results[0]->getMessage());
|
||||
}
|
||||
|
||||
public function testErrorWithNullId(): void
|
||||
{
|
||||
// JSON-RPC 2.0 spells the same thing as an explicit null.
|
||||
$json = '{"jsonrpc": "2.0", "id": null, "error": {"code": -32600, "message": "Invalid"}}';
|
||||
|
||||
$results = $this->factory->create($json);
|
||||
|
||||
$this->assertCount(1, $results);
|
||||
$this->assertInstanceOf(Error::class, $results[0]);
|
||||
$this->assertNull($results[0]->getId());
|
||||
}
|
||||
|
||||
public function testErrorWithInvalidIdType(): void
|
||||
{
|
||||
$json = '{"jsonrpc": "2.0", "id": null, "error": {"code": -32600, "message": "Invalid"}}';
|
||||
$json = '{"jsonrpc": "2.0", "id": {"not": "an id"}, "error": {"code": -32600, "message": "Invalid"}}';
|
||||
|
||||
$results = $this->factory->create($json);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue