Repository navigation
Avoid unconditional constraint instantiation in Draft06/07/2019 dispatch - #926
DannyvdSluijs wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR optimizes Draft06/Draft07/Draft2019 keyword dispatch by avoiding unconditional constraint instantiation when the corresponding schema keyword/property is absent, while preserving existing behavior (matching the constraints’ own early-return guards).
Changes:
- Added per-draft keyword→schema-property maps for the few keywords whose dispatch name doesn’t match their guarding schema property (
ref,ifThenElse,content). - Updated
checkForKeyword()in Draft06/07/2019 dispatchers to skipFactory::createInstanceFor()unless the relevant schema property exists. - Added unit tests asserting that only present keywords trigger
createInstanceFor()calls.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/JsonSchema/Constraints/Drafts/Draft06/Draft06Constraint.php | Gate constraint instantiation on schema-property presence (incl. $ref mapping). |
| src/JsonSchema/Constraints/Drafts/Draft07/Draft07Constraint.php | Gate constraint instantiation on schema-property presence (incl. $ref, if, content* mappings). |
| src/JsonSchema/Constraints/Drafts/Draft2019/Draft2019Constraint.php | Gate constraint instantiation on schema-property presence (incl. $ref, if, content* mappings). |
| tests/Constraints/Drafts/Draft06/Draft06ConstraintTest.php | New tests verifying instantiation is skipped for absent keywords in Draft06. |
| tests/Constraints/Drafts/Draft07/Draft07ConstraintTest.php | New tests verifying instantiation is skipped for absent keywords (incl. special mappings) in Draft07. |
| tests/Constraints/Drafts/Draft2019/Draft2019ConstraintTest.php | New tests verifying instantiation is skipped for absent keywords (incl. special mappings) in Draft2019. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| public function testDependentSchemasAndDependentRequiredAreInstantiatedIndependently(): void | ||
| { | ||
| $constraint = new Draft2019Constraint(); | ||
| $calledKeywords = []; | ||
| $this->injectFactory($constraint, $this->createSpyFactory($calledKeywords)); | ||
|
|
||
| $schema = json_decode('{"dependentRequired": {"a": ["b"]}}'); | ||
| $value = json_decode('{"a": 1, "b": 2}'); | ||
| $constraint->check($value, $schema); | ||
|
|
||
| $this->assertSame(['dependentRequired'], $calledKeywords); | ||
| } |
b379304 to
4f9cdb0
Compare
4f9cdb0 to
0ca666a
Compare
0ca666a to
803e676
Compare
checkForKeyword() now skips createInstanceFor() when the corresponding schema property is absent, instead of always instantiating a constraint object only for it to immediately no-op via its own internal guard. Fixes #920. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
803e676 to
aead65c
Compare
|
If a keyword's name differs from its schema property and it isn't in I opened #993 against this branch with a test that keeps the map in sync with the constraints. For every keyword in each draft factory's The test relies on the convention that the first #993 also fixes the tests in this PR: the BC
Correction: custom constraint classes registered on the factory passed to the |
|
I profiled the strict mode with the GeoJSON schema of the jsonschema-benchmark (Draft-07), using the benchmark's harness for this library, and this PR helps a lot there. For the first 100 instances:
#1007 evaluates the branches of With this PR, the dispatch still calls It would replace the |
Summary
Draft06Constraint,Draft07Constraint, andDraft2019Constraintpreviously calledcheckForKeyword()for every registered keyword on every schema node, unconditionally instantiating a constraint object (viaFactory::createInstanceFor()) even when the keyword was absent from the schema — the constraint's owncheck()would just immediately return via its internalproperty_exists($schema, ...)guard.checkForKeyword()now skipscreateInstanceFor()entirely when the relevant schema property is absent, using a small per-class keyword→property map to handle the three keywords whose name doesn't match the schema property they guard on (ref→$ref,ifThenElse→if,content→contentMediaType/contentEncoding).Fixes #920.
Verification
composer test): identical failure set before and after (verified via git-stash A/B diff of failing test names) — zero regressions.composer phpstan(level 8): clean.composer style-check: clean.tests/Constraints/Drafts/Draft{06,07,2019}/) assertcreateInstanceForis skipped for absent keywords and invoked for present ones.Follow-up
A related, larger idea came up during review: gating could go further by also checking the value's type (not just schema-property presence), since ~76% of Draft2019 constraints already guard on value type internally. That's a bigger, potentially breaking change (touches ~98 constraint classes, would need an opt-in addition to
ConstraintInterfaceto stay backward compatible for consumers usingFactory::setConstraintClass()), so it's tracked separately in #925 rather than folded into this PR.Test plan
composer testcomposer phpstancomposer style-check