Skip to content

Commit b1ee0e8

Browse files
eraydbighappyface
authored andcommitted
[BUGFIX] Add provided schema under a dummy / internal URI (fixes #376) (#378)
* Add provided schema under a dummy / internal URI (fixes #376) In order to resolve internal $ref references within a user-provided schema, SchemaStorage needs to know about the schema. As user-supplied schemas do not have an associated URI, use a dummy / internal one instead. * Remove dangling use * Change URI to class constant on SchemaStorage
1 parent 5de03d4 commit b1ee0e8

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

src/JsonSchema/SchemaStorage.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
class SchemaStorage implements SchemaStorageInterface
1212
{
13+
const INTERNAL_PROVIDED_SCHEMA_URI = 'internal://provided-schema';
14+
1315
protected $uriRetriever;
1416
protected $uriResolver;
1517
protected $schemas = array();
@@ -43,7 +45,10 @@ public function getUriResolver()
4345
*/
4446
public function addSchema($id, $schema = null)
4547
{
46-
if (is_null($schema)) {
48+
if (is_null($schema) && $id !== self::INTERNAL_PROVIDED_SCHEMA_URI) {
49+
// if the schema was user-provided to Validator and is still null, then assume this is
50+
// what the user intended, as there's no way for us to retrieve anything else. User-supplied
51+
// schemas do not have an associated URI when passed via Validator::validate().
4752
$schema = $this->uriRetriever->retrieve($id);
4853
}
4954
$objectIterator = new ObjectIterator($schema);

src/JsonSchema/Validator.php

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use JsonSchema\Constraints\BaseConstraint;
1313
use JsonSchema\Constraints\Constraint;
1414
use JsonSchema\Exception\InvalidConfigException;
15+
use JsonSchema\SchemaStorage;
1516

1617
/**
1718
* A JsonSchema Constraint
@@ -41,6 +42,9 @@ public function validate(&$value, $schema = null, $checkMode = null)
4142
$this->factory->setConfig($checkMode);
4243
}
4344

45+
// add provided schema to SchemaStorage with internal URI to allow internal $ref resolution
46+
$this->factory->getSchemaStorage()->addSchema(SchemaStorage::INTERNAL_PROVIDED_SCHEMA_URI, $schema);
47+
4448
$validator = $this->factory->createInstanceFor('schema');
4549
$validator->check($value, $schema);
4650

tests/SchemaStorageTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use JsonSchema\SchemaStorage;
1313
use JsonSchema\Uri\UriRetriever;
14+
use JsonSchema\Validator;
1415
use Prophecy\Argument;
1516

1617
class SchemaStorageTest extends \PHPUnit_Framework_TestCase
@@ -31,6 +32,15 @@ public function testResolveRef()
3132
);
3233
}
3334

35+
public function testResolveTopRef()
36+
{
37+
$input = json_decode('{"propertyOne":"notANumber"}');
38+
$schema = json_decode('{"$ref":"#/definition","definition":{"properties":{"propertyOne":{"type":"number"}}}}');
39+
$v = new Validator();
40+
$v->validate($input, $schema);
41+
$this->assertFalse($v->isValid());
42+
}
43+
3444
/**
3545
* @depends testResolveRef
3646
*/

0 commit comments

Comments
 (0)