Skip to content

Fix nested empty object validation - #646

Closed
Adebayo120 wants to merge 1 commit into
modelcontextprotocol:mainfrom
Adebayo120:fix/schemavalidator-empty-object
Closed

Adebayo120 wants to merge 1 commit into
modelcontextprotocol:mainfrom
Adebayo120:fix/schemavalidator-empty-object

Conversation

@Adebayo120

Copy link
Copy Markdown
Contributor

Summary

  • Fix schema validation for nested empty JSON objects by preserving object context from the surrounding schema when json_decode(..., true) turns {} into an empty PHP array.
  • Add a regression test covering a nested object like {"parameters": {}} that previously failed with Expected object, but received array.

Reproduction

use Mcp\Capability\Discovery\SchemaValidator;

$data = json_decode('{"parameters":{}}', true, 512, JSON_THROW_ON_ERROR);
$schema = [
    'type' => 'object',
    'properties' => [
        'parameters' => ['type' => 'object'],
    ],
];

$validator = new SchemaValidator();
var_export($validator->validateAgainstJsonSchema($data, $schema));

Before the fix this returned an error complaining /parameters was an array rather than an object.

Validation

  • php vendor/bin/phpunit tests/Unit/Capability/Discovery/SchemaValidatorTest.php
  • php vendor/bin/phpstan analyse --no-progress --configuration=phpstan.dist.neon src/Capability/Discovery/SchemaValidator.php tests/Unit/Capability/Discovery/SchemaValidatorTest.php
  • php vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php --dry-run --diff src/Capability/Discovery/SchemaValidator.php tests/Unit/Capability/Discovery/SchemaValidatorTest.php

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Schema coercion can incorrectly accept arrays as object-valued tool output while rejecting valid nullable-object and additional-property inputs.

3 open issues
What changed in this PR

This PR updates schema-aware data preparation to handle nested empty JSON objects that become empty PHP arrays during associative decoding.

Changes:

  • Pass property and array-item schemas through recursive data conversion.
  • Convert nested empty arrays to objects when the schema explicitly requires an object.
  • Add a regression test for an empty nested parameters object.
File Description
tests/​Unit/​Capability/​Discovery/​SchemaValidatorTest.php Adds coverage for nested empty-object validation.
src/​Capability/​Discovery/​SchemaValidator.php Adds schema-aware recursive conversion and schema lookup helpers.

Review effort: Balanced

Comment on lines +130 to +131
if (!$isRoot && $this->expectsObjectType($schema) && \is_array($data) && [] === $data) {
return new \stdClass();
Comment on lines +165 to +173
if (\is_array($schema)) {
return isset($schema['type']) && 'object' === $schema['type'];
}

if (\is_object($schema)) {
return isset($schema->type) && 'object' === $schema->type;
}

return false;
Comment on lines +183 to +187
return $schema['items'] ?? null;
}

if (\is_object($schema) && isset($schema->properties) && \is_object($schema->properties)) {
return $schema->properties->{$property} ?? ($schema->items ?? null);
@chr-hertel chr-hertel added the Server Issues & PRs related to the Server component label Nov 3, 2026
@chr-hertel chr-hertel modified the milestone: 0.10.0 Nov 4, 2026
@chr-hertel

Copy link
Copy Markdown
Member

Replaced by #678 - thanks @Adebayo120!

@chr-hertel chr-hertel closed this Nov 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Server Issues & PRs related to the Server component

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants