Skip to content

Fix nested empty object validation - #646

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

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>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant