Avoid unconditional constraint instantiation in Draft06/07/2019 dispatch - #926
Open
DannyvdSluijs wants to merge 1 commit into
Open
Avoid unconditional constraint instantiation in Draft06/07/2019 dispatch#926DannyvdSluijs wants to merge 1 commit into
DannyvdSluijs wants to merge 1 commit into
Conversation
Contributor
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.
Comment on lines
+90
to
+101
| 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); | ||
| } |
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>
DannyvdSluijs
force-pushed
the
fix/920-avoid-unconditional-constraint-instantiation
branch
from
July 24, 2026 09:57
b379304 to
4f9cdb0
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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