Skip to content

Commit b28d5a9

Browse files
committed
[Server] Set empty object defaults after schema merges and keep parse errors
1 parent fde80e0 commit b28d5a9

4 files changed

Lines changed: 67 additions & 8 deletions

File tree

‎src/Capability/Discovery/SchemaGenerator.php‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,25 @@ private function buildParameterSchema(array $paramInfo, ?array $methodLevelParam
247247
// Run after all merges so that when a Schema attribute reshapes the parameter
248248
// (e.g. to `object`), the array `items` invariant is only enforced on what is
249249
// genuinely still an array.
250-
return $this->ensureArrayItems($mergedSchema);
250+
return $this->ensureObjectDefault($this->ensureArrayItems($mergedSchema));
251+
}
252+
253+
/**
254+
* An empty PHP array encodes as `[]`, which a client could not send back to an object-only parameter.
255+
*
256+
* @param array<string, mixed> $schema
257+
*
258+
* @return array<string, mixed>
259+
*/
260+
private function ensureObjectDefault(array $schema): array
261+
{
262+
$types = (array) ($schema['type'] ?? []);
263+
264+
if ([] === ($schema['default'] ?? null) && \in_array('object', $types, true) && !\in_array('array', $types, true)) {
265+
$schema['default'] = new \stdClass();
266+
}
267+
268+
return $schema;
251269
}
252270

253271
/**
@@ -308,12 +326,6 @@ private function buildInferredParameterSchema(array $paramInfo): array
308326
// Add default value only if parameter actually has a default
309327
if ($paramInfo['has_default']) {
310328
$paramSchema['default'] = $paramInfo['default_value'];
311-
312-
// An empty PHP array encodes as `[]`, which a client could not send back to an object-only parameter.
313-
$types = (array) ($paramSchema['type'] ?? []);
314-
if ([] === $paramSchema['default'] && \in_array('object', $types, true) && !\in_array('array', $types, true)) {
315-
$paramSchema['default'] = new \stdClass();
316-
}
317329
}
318330

319331
// Handle enums

‎src/JsonRpc/MessageFactory.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,13 @@ public function create(string $input): array
167167

168168
/**
169169
* Valid JSON, but a PHP object cannot hold a property whose name starts with NUL.
170+
*
171+
* @throws \JsonException when the input is malformed after that name
170172
*/
171173
private static function invalidMemberName(string $input): InvalidInputMessageException
172174
{
173175
$exception = new InvalidInputMessageException('A JSON-RPC message must not have a member name starting with a NUL byte.');
174-
$id = json_decode($input, true)['id'] ?? null;
176+
$id = json_decode($input, true, flags: \JSON_THROW_ON_ERROR)['id'] ?? null;
175177

176178
if (\is_string($id) || \is_int($id)) {
177179
$exception->setRequestId($id);

‎tests/Unit/Capability/Discovery/SchemaGeneratorTest.php‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Mcp\Tests\Unit\Capability\Discovery;
1313

14+
use Mcp\Capability\Attribute\Schema;
1415
use Mcp\Capability\Discovery\DocBlockParser;
1516
use Mcp\Capability\Discovery\SchemaGenerator;
1617
use Mcp\Capability\Discovery\SchemaValidator;
@@ -708,6 +709,43 @@ public function handle(array $map = [], ?array $nullableMap = [], array $mapOrLi
708709
$this->assertSame('[]', json_encode($properties['list']['default']));
709710
}
710711

712+
public function testEmptyDefaultFollowsTheFinalType(): void
713+
{
714+
$handler = new class {
715+
/**
716+
* @param array<string, int> $toArray
717+
* @param array<string, int> $toDefinition
718+
*/
719+
public function handle(
720+
#[Schema(type: 'array')]
721+
array $toArray = [],
722+
#[Schema(definition: ['type' => 'array'])]
723+
array $toDefinition = [],
724+
#[Schema(type: 'object')]
725+
array $toObject = [],
726+
): void {
727+
}
728+
729+
/**
730+
* @param array<string, int> $map
731+
*/
732+
#[Schema(properties: ['map' => ['type' => 'array']])]
733+
public function method(array $map = []): void
734+
{
735+
}
736+
};
737+
738+
$properties = $this->schemaGenerator->generate(new \ReflectionMethod($handler, 'handle'))['properties'];
739+
740+
$this->assertSame('[]', json_encode($properties['toArray']['default']));
741+
$this->assertSame('[]', json_encode($properties['toDefinition']['default']));
742+
$this->assertSame('{}', json_encode($properties['toObject']['default']));
743+
744+
$properties = $this->schemaGenerator->generate(new \ReflectionMethod($handler, 'method'))['properties'];
745+
746+
$this->assertSame('[]', json_encode($properties['map']['default']));
747+
}
748+
711749
public function testListOfGenericArraysIsAList(): void
712750
{
713751
$handler = new class {

‎tests/Unit/JsonRpc/MessageFactoryTest.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ public function testOtherParamsAreDecodedToArrays(): void
6666
$this->assertInstanceOf(InitializeRequest::class, $messages[0]);
6767
}
6868

69+
public function testMalformedJsonAfterAMemberNameStartingWithNulIsAParseError(): void
70+
{
71+
$this->expectException(\JsonException::class);
72+
73+
$this->factory->create('{"id":1,"\u0000x":1,}');
74+
}
75+
6976
public function testAnObjectWithNumericKeysIsNotABatch(): void
7077
{
7178
$messages = $this->factory->create('{"0":{"jsonrpc":"2.0","id":1,"method":"ping"}}');

0 commit comments

Comments
 (0)