-
-
Notifications
You must be signed in to change notification settings - Fork 375
Report an unresolvable $ref instead of passing silently #944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
|
|
||
| namespace JsonSchema\Constraints\Drafts\Draft07; | ||
|
|
||
| use JsonSchema\ConstraintError; | ||
| use JsonSchema\Constraints\ConstraintInterface; | ||
| use JsonSchema\Entity\ErrorBagProxy; | ||
| use JsonSchema\Entity\JsonPointer; | ||
|
|
@@ -30,6 +31,10 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n | |
| try { | ||
| $refSchema = $this->factory->getSchemaStorage()->resolveRefSchema($schema); | ||
| } catch (\Exception $e) { | ||
| // A $ref that cannot be resolved is a broken schema, not an absent keyword: | ||
| // returning here would let the value pass as if the $ref had not been written. | ||
| $this->addError(ConstraintError::UNRESOLVABLE_REF(), $path, ['ref' => $schema->{'$ref'}]); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed, measured on all three drafts in strict mode: |
||
|
|
||
| return; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
|
|
||
| namespace JsonSchema\Constraints\Drafts\Draft2019; | ||
|
|
||
| use JsonSchema\ConstraintError; | ||
| use JsonSchema\Constraints\ConstraintInterface; | ||
| use JsonSchema\Entity\ErrorBagProxy; | ||
| use JsonSchema\Entity\JsonPointer; | ||
|
|
@@ -30,6 +31,10 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n | |
| try { | ||
| $refSchema = $this->factory->getSchemaStorage()->resolveRefSchema($schema); | ||
| } catch (\Exception $e) { | ||
| // A $ref that cannot be resolved is a broken schema, not an absent keyword: | ||
| // returning here would let the value pass as if the $ref had not been written. | ||
| $this->addError(ConstraintError::UNRESOLVABLE_REF(), $path, ['ref' => $schema->{'$ref'}]); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed, measured on all three drafts in strict mode: |
||
|
|
||
| return; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace JsonSchema\Tests\Constraints\Draft06; | ||
|
|
||
| use JsonSchema\Constraints\Constraint; | ||
| use JsonSchema\Tests\Constraints\VeryBaseTestCase; | ||
| use JsonSchema\Validator; | ||
|
|
||
| class RefConstraintTest extends VeryBaseTestCase | ||
| { | ||
| private const DIALECT = 'http://json-schema.org/draft-06/schema#'; | ||
|
|
||
| public function testAnUnresolvableRefIsReportedInsteadOfSilentlyPassing(): void | ||
| { | ||
| $validator = new Validator(); | ||
| $data = new \stdClass(); | ||
| $schema = json_decode('{"$schema": "' . self::DIALECT . '", "$ref": "#/$defs/nonExistent"}'); | ||
|
|
||
| $validator->validate($data, $schema, Constraint::CHECK_MODE_STRICT); | ||
|
|
||
| self::assertFalse($validator->isValid()); | ||
| $errors = $validator->getErrors(); | ||
| self::assertCount(1, $errors); | ||
| self::assertSame('unresolvableRef', $errors[0]['constraint']['name']); | ||
| } | ||
|
|
||
| public function testAResolvableRefStillValidates(): void | ||
| { | ||
| $validator = new Validator(); | ||
| $data = json_decode('{"child": 1}'); | ||
| $schema = json_decode( | ||
| '{"$schema": "' . self::DIALECT . '", "$defs": {"num": {"type": "integer"}},' | ||
| . ' "properties": {"child": {"$ref": "#/$defs/num"}}}' | ||
| ); | ||
|
|
||
| $validator->validate($data, $schema, Constraint::CHECK_MODE_STRICT); | ||
|
|
||
| self::assertTrue($validator->isValid(), (string) json_encode($validator->getErrors())); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace JsonSchema\Tests\Constraints\Draft07; | ||
|
|
||
| use JsonSchema\Constraints\Constraint; | ||
| use JsonSchema\Tests\Constraints\VeryBaseTestCase; | ||
| use JsonSchema\Validator; | ||
|
|
||
| class RefConstraintTest extends VeryBaseTestCase | ||
| { | ||
| private const DIALECT = 'http://json-schema.org/draft-07/schema#'; | ||
|
|
||
| public function testAnUnresolvableRefIsReportedInsteadOfSilentlyPassing(): void | ||
| { | ||
| $validator = new Validator(); | ||
| $data = new \stdClass(); | ||
| $schema = json_decode('{"$schema": "' . self::DIALECT . '", "$ref": "#/$defs/nonExistent"}'); | ||
|
|
||
| $validator->validate($data, $schema, Constraint::CHECK_MODE_STRICT); | ||
|
|
||
| self::assertFalse($validator->isValid()); | ||
| $errors = $validator->getErrors(); | ||
| self::assertCount(1, $errors); | ||
| self::assertSame('unresolvableRef', $errors[0]['constraint']['name']); | ||
| } | ||
|
|
||
| public function testAResolvableRefStillValidates(): void | ||
| { | ||
| $validator = new Validator(); | ||
| $data = json_decode('{"child": 1}'); | ||
| $schema = json_decode( | ||
| '{"$schema": "' . self::DIALECT . '", "$defs": {"num": {"type": "integer"}},' | ||
| . ' "properties": {"child": {"$ref": "#/$defs/num"}}}' | ||
| ); | ||
|
|
||
| $validator->validate($data, $schema, Constraint::CHECK_MODE_STRICT); | ||
|
|
||
| self::assertTrue($validator->isValid(), (string) json_encode($validator->getErrors())); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace JsonSchema\Tests\Constraints\Draft2019; | ||
|
|
||
| use JsonSchema\Constraints\Constraint; | ||
| use JsonSchema\Tests\Constraints\VeryBaseTestCase; | ||
| use JsonSchema\Validator; | ||
|
|
||
| class RefConstraintTest extends VeryBaseTestCase | ||
| { | ||
| private const DIALECT = 'https://json-schema.org/draft/2019-09/schema'; | ||
|
|
||
| public function testAnUnresolvableRefIsReportedInsteadOfSilentlyPassing(): void | ||
| { | ||
| $validator = new Validator(); | ||
| $data = new \stdClass(); | ||
| $schema = json_decode('{"$schema": "' . self::DIALECT . '", "$ref": "#/$defs/nonExistent"}'); | ||
|
|
||
| $validator->validate($data, $schema, Constraint::CHECK_MODE_STRICT); | ||
|
|
||
| self::assertFalse($validator->isValid()); | ||
| $errors = $validator->getErrors(); | ||
| self::assertCount(1, $errors); | ||
| self::assertSame('unresolvableRef', $errors[0]['constraint']['name']); | ||
| } | ||
|
|
||
| public function testAResolvableRefStillValidates(): void | ||
| { | ||
| $validator = new Validator(); | ||
| $data = json_decode('{"child": 1}'); | ||
| $schema = json_decode( | ||
| '{"$schema": "' . self::DIALECT . '", "$defs": {"num": {"type": "integer"}},' | ||
| . ' "properties": {"child": {"$ref": "#/$defs/num"}}}' | ||
| ); | ||
|
|
||
| $validator->validate($data, $schema, Constraint::CHECK_MODE_STRICT); | ||
|
|
||
| self::assertTrue($validator->isValid(), (string) json_encode($validator->getErrors())); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirmed, measured on all three drafts in strict mode:
anyOf [$ref, {}],oneOf [$ref, {}]andnot $refall come out valid with an unresolvable$ref. I laid out the options in the main thread, since the fix changes the approach the PR was approved with.