Skip to content

Fix #93 ($ref to local definition not working) #182

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

Merged
merged 3 commits into from
Sep 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion src/JsonSchema/RefResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ class RefResolver
*/
protected $uriRetriever = null;

/**
* @var object
*/
protected $rootSchema = null;

/**
* @param UriRetriever $retriever
*/
Expand Down Expand Up @@ -110,6 +115,10 @@ public function resolve($schema, $sourceUri = null)
$sourceUri = $schema->id;
}

if (null === $this->rootSchema) {
$this->rootSchema = $schema;
}

// Resolve $ref first
$this->resolveRef($schema, $sourceUri);

Expand Down Expand Up @@ -205,7 +214,30 @@ public function resolveRef($schema, $sourceUri)
return;
}

$refSchema = $this->fetchRef($schema->$ref, $sourceUri);
$splitRef = explode('#', $schema->$ref, 2);

$refDoc = $splitRef[0];
$refPath = null;
if (count($splitRef) === 2) {
$refPath = explode('/', $splitRef[1]);
array_shift($refPath);
}

if (empty($refDoc) && empty($refPath)) {
// TODO: Not yet implemented - root pointer ref, causes recursion issues
return;
}

if (!empty($refDoc)) {
$refSchema = $this->fetchRef($refDoc, $sourceUri);
} else {
$refSchema = $this->rootSchema;
}

if (null !== $refPath) {
$refSchema = $this->resolveRefSegment($refSchema, $refPath);
}

unset($schema->$ref);

// Augment the current $schema object with properties fetched
Expand All @@ -226,4 +258,19 @@ public function setUriRetriever(UriRetriever $retriever)

return $this;
}

protected function resolveRefSegment($data, $pathParts)
{
foreach ($pathParts as $path) {
$path = strtr($path, array('~1' => '/', '~0' => '~', '%25' => '%'));

if (is_array($data)) {
$data = $data[$path];
} else {
$data = $data->{$path};
}
}

return $data;
}
}
18 changes: 15 additions & 3 deletions tests/JsonSchema/Tests/Constraints/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace JsonSchema\Tests\Constraints;

use JsonSchema\RefResolver;
use JsonSchema\Uri\UriRetriever;
use JsonSchema\Validator;

abstract class BaseTestCase extends \PHPUnit_Framework_TestCase
Expand All @@ -18,9 +20,14 @@ abstract class BaseTestCase extends \PHPUnit_Framework_TestCase
*/
public function testInvalidCases($input, $schema, $checkMode = Validator::CHECK_MODE_NORMAL, $errors = array())
{
$schema = json_decode($schema);

$refResolver = new RefResolver(new UriRetriever);
$refResolver->resolve($schema);

$validator = new Validator($checkMode);

$validator->check(json_decode($input), json_decode($schema));
$validator->check(json_decode($input), $schema);

if (array() !== $errors) {
$this->assertEquals($errors, $validator->getErrors(), print_r($validator->getErrors(),true));
Expand All @@ -33,13 +40,18 @@ public function testInvalidCases($input, $schema, $checkMode = Validator::CHECK_
*/
public function testValidCases($input, $schema, $checkMode = Validator::CHECK_MODE_NORMAL)
{
$schema = json_decode($schema);

$refResolver = new RefResolver(new UriRetriever);
$refResolver->resolve($schema);

$validator = new Validator($checkMode);

$validator->check(json_decode($input), json_decode($schema));
$validator->check(json_decode($input), $schema);
$this->assertTrue($validator->isValid(), print_r($validator->getErrors(), true));
}

abstract public function getValidTests();

abstract public function getInvalidTests();
}
}
2 changes: 1 addition & 1 deletion tests/JsonSchema/Tests/Drafts/Draft4Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ protected function getSkippedTests()
);
}

}
}