Skip to content

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

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

Closed
wants to merge 5 commits into from
Closed
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
46 changes: 45 additions & 1 deletion src/JsonSchema/RefResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class RefResolver
* @var UriRetrieverInterface
*/
protected $uriRetriever = null;


protected $rootSchema;


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

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

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

Expand Down Expand Up @@ -204,15 +212,43 @@ public function resolveRef($schema, $sourceUri)
return;
}

$refSchema = $this->fetchRef($schema->$ref, $sourceUri);
if ($schema->{$ref}[0] === '#') {
$path = substr($schema->$ref, 1);
if (empty($path)) {
return;
}

if ($path[0] !== '/') {
return;
}

$pathParts = explode('/', $path);
array_shift($pathParts);

$refSchema = $this->resolveRefSegment($this->rootSchema, $pathParts);
} else {
$refSchema = $this->fetchRef($schema->$ref, $sourceUri);
}
unset($schema->$ref);


// Augment the current $schema object with properties fetched
foreach (get_object_vars($refSchema) as $prop => $value) {
$schema->$prop = $value;
}
}

protected function resolveRefSegment($data, $pathParts)
{
if (empty($pathParts) || empty($data)) {
return $data;
}

$key = $this->transformKey(array_shift($pathParts));

return $this->resolveRefSegment(is_array($data) ? $data[$key] : $data->$key, $pathParts);
}

/**
* Set URI Retriever for use with the Ref Resolver
*
Expand All @@ -225,4 +261,12 @@ public function setUriRetriever(UriRetriever $retriever)

return $this;
}

protected function transformKey($key)
{
return strtr($key, array(
'~1' => '/',
'~0' => '~',
));
}
}
51 changes: 50 additions & 1 deletion tests/JsonSchema/Tests/Drafts/Draft4Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,54 @@ protected function getSkippedTests()
'zeroTerminatedFloats.json'
);
}

public function testInnerDefinitions(){
$schema = <<<JSN
{
"type": "object",
"additionalProperties":false,
"properties": {
"person": { "\$ref": "#/definitions/persondef" },
"person-age": { "\$ref": "#/definitions/persondef/properties/age" },
"slash": { "\$ref": "#/definitions/persondef/properties/~1slash" },
"tilde": { "\$ref": "#/definitions/persondef/properties/~0tilde" }
},
"definitions": {
"persondef": {
"type": "object",
"additionalProperties":false,
"properties": {
"name": {
"type": "string"
},
"age": {
"type" : "integer"
},
"/slash": {
"type" : "integer"
},
"~tilde": {
"type" : "integer"
}
}
}
}
}
JSN;
$schemaObj = json_decode($schema);
$resolver = new \JsonSchema\RefResolver();
$resolver->resolve($schemaObj);

$schema = json_encode($schemaObj);

$this->testValidCases('{"person": {"name" : "John Doe", "age" : 30} }', $schema);
$this->testInvalidCases('{"person": {"name" : "John Doe", "age" : "wrong"} }', $schema);
$this->testValidCases('{"person-age": 30 }', $schema);
$this->testInvalidCases('{"person-age": "wrong" }', $schema);
$this->testValidCases('{"slash": 30 }', $schema);
$this->testInvalidCases('{"slash": "wrong" }', $schema);
$this->testValidCases('{"tilde": 30 }', $schema);
$this->testInvalidCases('{"tilde": "wrong" }', $schema);
}

}
}