From 351fc4151d1a811ca46f3092e661dcf4bfbbdabd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Ondr=C3=A1=C4=8Dek?= Date: Sun, 25 Oct 2026 13:03:32 +0100 Subject: [PATCH 1/3] test: Keep KEYWORD_SCHEMA_PROPERTIES in sync with the keyword constraints MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The draft constraints only dispatch a keyword when one of the schema properties mapped to it is present. A keyword whose schema property differs from its name and is missing from KEYWORD_SCHEMA_PROPERTIES is silently never checked. The test compares, for every keyword in each draft factory, the schema properties in the first condition of the constraint's check() with the properties the dispatcher checks. Signed-off-by: Roman Ondráček --- .../Drafts/KeywordSchemaPropertiesTest.php | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 tests/Constraints/Drafts/KeywordSchemaPropertiesTest.php diff --git a/tests/Constraints/Drafts/KeywordSchemaPropertiesTest.php b/tests/Constraints/Drafts/KeywordSchemaPropertiesTest.php new file mode 100644 index 000000000..598225c73 --- /dev/null +++ b/tests/Constraints/Drafts/KeywordSchemaPropertiesTest.php @@ -0,0 +1,100 @@ +getGuardedSchemaProperties($keywordConstraintClass); + if ($guardedProperties === []) { + $this->markTestSkipped(sprintf('%s does not guard on any schema property', $keywordConstraintClass)); + } + + $keywordSchemaProperties = (new \ReflectionClassConstant($draftConstraintClass, 'KEYWORD_SCHEMA_PROPERTIES'))->getValue(); + $dispatchedProperties = $keywordSchemaProperties[$keyword] ?? [$keyword]; + + sort($guardedProperties); + sort($dispatchedProperties); + self::assertSame( + $guardedProperties, + $dispatchedProperties, + sprintf('Add the "%s" keyword to %s::KEYWORD_SCHEMA_PROPERTIES', $keyword, $draftConstraintClass) + ); + } + + public function keywordProvider(): \Generator + { + $drafts = [ + Draft06\Draft06Constraint::class => Draft06\Factory::class, + Draft07\Draft07Constraint::class => Draft07\Factory::class, + Draft2019\Draft2019Constraint::class => Draft2019\Factory::class, + ]; + + foreach ($drafts as $draftConstraintClass => $factoryClass) { + $constraintMap = new \ReflectionProperty($factoryClass, 'constraintMap'); + if (PHP_VERSION_ID < 80100) { + $constraintMap->setAccessible(true); + } + + foreach ($constraintMap->getValue(new $factoryClass()) as $keyword => $keywordConstraintClass) { + if ($keyword === 'schema') { + continue; + } + + yield sprintf('%s: %s', (new \ReflectionClass($draftConstraintClass))->getShortName(), $keyword) => [ + $draftConstraintClass, + $keyword, + $keywordConstraintClass, + ]; + } + } + } + + /** + * The schema properties named in the first condition of the constraint's check(), which is + * where every constraint returns early when its keyword is absent. + * + * @param class-string $constraintClass + * + * @return list + */ + private function getGuardedSchemaProperties(string $constraintClass): array + { + $check = new \ReflectionMethod($constraintClass, 'check'); + $lines = file((string) $check->getFileName()); + if ($lines === false) { + self::fail(sprintf('Unable to read the source of %s', $constraintClass)); + } + $startLine = (int) $check->getStartLine(); + $body = implode('', array_slice($lines, $startLine, (int) $check->getEndLine() - $startLine)); + + if (!preg_match('/^\s*if \((.*)\) \{$/m', $body, $condition)) { + return []; + } + + preg_match_all('/property_exists\(\$schema, \'([^\']+)\'\)/', $condition[1], $properties); + + return $properties[1]; + } +} From 44692c5f0bd6fc12e92706b2de1f6fb031d515aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Ondr=C3=A1=C4=8Dek?= Date: Sun, 25 Oct 2026 13:03:40 +0100 Subject: [PATCH 2/3] refactor: Move KEYWORD_SCHEMA_PROPERTIES to the top of the class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Roman Ondráček --- .../Constraints/Drafts/Draft06/Draft06Constraint.php | 8 ++++---- .../Constraints/Drafts/Draft07/Draft07Constraint.php | 12 ++++++------ .../Drafts/Draft2019/Draft2019Constraint.php | 12 ++++++------ 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/JsonSchema/Constraints/Drafts/Draft06/Draft06Constraint.php b/src/JsonSchema/Constraints/Drafts/Draft06/Draft06Constraint.php index 79e0ee23f..fc962749f 100644 --- a/src/JsonSchema/Constraints/Drafts/Draft06/Draft06Constraint.php +++ b/src/JsonSchema/Constraints/Drafts/Draft06/Draft06Constraint.php @@ -12,6 +12,10 @@ class Draft06Constraint extends Constraint { + private const KEYWORD_SCHEMA_PROPERTIES = [ + 'ref' => ['$ref'], + ]; + public function __construct(?\JsonSchema\Constraints\Factory $factory = null) { parent::__construct(new Factory( @@ -66,10 +70,6 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n $this->checkForKeyword('pattern', $value, $schema, $path, $i); } - private const KEYWORD_SCHEMA_PROPERTIES = [ - 'ref' => ['$ref'], - ]; - /** * @param mixed $value * @param mixed $schema diff --git a/src/JsonSchema/Constraints/Drafts/Draft07/Draft07Constraint.php b/src/JsonSchema/Constraints/Drafts/Draft07/Draft07Constraint.php index d1cac103f..dcc9e1187 100644 --- a/src/JsonSchema/Constraints/Drafts/Draft07/Draft07Constraint.php +++ b/src/JsonSchema/Constraints/Drafts/Draft07/Draft07Constraint.php @@ -12,6 +12,12 @@ class Draft07Constraint extends Constraint { + private const KEYWORD_SCHEMA_PROPERTIES = [ + 'ref' => ['$ref'], + 'ifThenElse' => ['if'], + 'content' => ['contentMediaType', 'contentEncoding'], + ]; + public function __construct(?\JsonSchema\Constraints\Factory $factory = null) { parent::__construct(new Factory( @@ -68,12 +74,6 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n $this->checkForKeyword('content', $value, $schema, $path, $i); } - private const KEYWORD_SCHEMA_PROPERTIES = [ - 'ref' => ['$ref'], - 'ifThenElse' => ['if'], - 'content' => ['contentMediaType', 'contentEncoding'], - ]; - /** * @param mixed $value * @param mixed $schema diff --git a/src/JsonSchema/Constraints/Drafts/Draft2019/Draft2019Constraint.php b/src/JsonSchema/Constraints/Drafts/Draft2019/Draft2019Constraint.php index 2167387e3..8ebcd725a 100644 --- a/src/JsonSchema/Constraints/Drafts/Draft2019/Draft2019Constraint.php +++ b/src/JsonSchema/Constraints/Drafts/Draft2019/Draft2019Constraint.php @@ -12,6 +12,12 @@ class Draft2019Constraint extends Constraint { + private const KEYWORD_SCHEMA_PROPERTIES = [ + 'ref' => ['$ref'], + 'ifThenElse' => ['if'], + 'content' => ['contentMediaType', 'contentEncoding'], + ]; + public function __construct(?\JsonSchema\Constraints\Factory $factory = null) { parent::__construct(new Factory( @@ -69,12 +75,6 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n $this->checkForKeyword('content', $value, $schema, $path, $i); } - private const KEYWORD_SCHEMA_PROPERTIES = [ - 'ref' => ['$ref'], - 'ifThenElse' => ['if'], - 'content' => ['contentMediaType', 'contentEncoding'], - ]; - /** * @param mixed $value * @param mixed $schema From 922ac22ce92188c9bcbb62cb94a8903af66c4734 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Ondr=C3=A1=C4=8Dek?= Date: Sun, 25 Oct 2026 13:03:39 +0100 Subject: [PATCH 3/3] test: Fix the dispatcher tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - The test named after dependentSchemas and dependentRequired only used dependentRequired, so dependentSchemas was not covered. Both are now covered separately. - The draft constraints always run with their own draft factory, and may call methods only that factory has, such as getDynamicScope() needed for $recursiveRef in Draft 2019-09. A mock of the base factory lacks them, so the tests mock the draft factory. Signed-off-by: Roman Ondráček --- .../Drafts/Draft06/Draft06ConstraintTest.php | 2 +- .../Drafts/Draft07/Draft07ConstraintTest.php | 2 +- .../Draft2019/Draft2019ConstraintTest.php | 17 +++++++++++++---- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/tests/Constraints/Drafts/Draft06/Draft06ConstraintTest.php b/tests/Constraints/Drafts/Draft06/Draft06ConstraintTest.php index ed6d98427..8996db15f 100644 --- a/tests/Constraints/Drafts/Draft06/Draft06ConstraintTest.php +++ b/tests/Constraints/Drafts/Draft06/Draft06ConstraintTest.php @@ -7,7 +7,7 @@ use JsonSchema\Constraints\BaseConstraint; use JsonSchema\Constraints\ConstraintInterface; use JsonSchema\Constraints\Drafts\Draft06\Draft06Constraint; -use JsonSchema\Constraints\Factory; +use JsonSchema\Constraints\Drafts\Draft06\Factory; use PHPUnit\Framework\TestCase; class Draft06ConstraintTest extends TestCase diff --git a/tests/Constraints/Drafts/Draft07/Draft07ConstraintTest.php b/tests/Constraints/Drafts/Draft07/Draft07ConstraintTest.php index a2ccb048c..d68f909b1 100644 --- a/tests/Constraints/Drafts/Draft07/Draft07ConstraintTest.php +++ b/tests/Constraints/Drafts/Draft07/Draft07ConstraintTest.php @@ -7,7 +7,7 @@ use JsonSchema\Constraints\BaseConstraint; use JsonSchema\Constraints\ConstraintInterface; use JsonSchema\Constraints\Drafts\Draft07\Draft07Constraint; -use JsonSchema\Constraints\Factory; +use JsonSchema\Constraints\Drafts\Draft07\Factory; use PHPUnit\Framework\TestCase; class Draft07ConstraintTest extends TestCase diff --git a/tests/Constraints/Drafts/Draft2019/Draft2019ConstraintTest.php b/tests/Constraints/Drafts/Draft2019/Draft2019ConstraintTest.php index 4d11573ef..d8f699701 100644 --- a/tests/Constraints/Drafts/Draft2019/Draft2019ConstraintTest.php +++ b/tests/Constraints/Drafts/Draft2019/Draft2019ConstraintTest.php @@ -7,7 +7,7 @@ use JsonSchema\Constraints\BaseConstraint; use JsonSchema\Constraints\ConstraintInterface; use JsonSchema\Constraints\Drafts\Draft2019\Draft2019Constraint; -use JsonSchema\Constraints\Factory; +use JsonSchema\Constraints\Drafts\Draft2019\Factory; use PHPUnit\Framework\TestCase; class Draft2019ConstraintTest extends TestCase @@ -87,17 +87,26 @@ public static function contentPropertyProvider(): \Generator yield 'contentEncoding' => ['contentEncoding']; } - public function testDependentSchemasAndDependentRequiredAreInstantiatedIndependently(): void + /** + * @dataProvider dependentKeywordProvider + */ + public function testDependentSchemasAndDependentRequiredAreInstantiatedIndependently(string $schemaJson, string $expectedKeyword): void { $constraint = new Draft2019Constraint(); $calledKeywords = []; $this->injectFactory($constraint, $this->createSpyFactory($calledKeywords)); - $schema = json_decode('{"dependentRequired": {"a": ["b"]}}'); + $schema = json_decode($schemaJson); $value = json_decode('{"a": 1, "b": 2}'); $constraint->check($value, $schema); - $this->assertSame(['dependentRequired'], $calledKeywords); + $this->assertSame([$expectedKeyword], $calledKeywords); + } + + public function dependentKeywordProvider(): \Generator + { + yield 'dependentSchemas' => ['{"dependentSchemas": {"a": {"required": ["b"]}}}', 'dependentSchemas']; + yield 'dependentRequired' => ['{"dependentRequired": {"a": ["b"]}}', 'dependentRequired']; } private function createSpyFactory(array &$calledKeywords): Factory