Skip to content

Commit

Permalink
Merge pull request geraintluff#11 from wesleyks/master
Browse files Browse the repository at this point in the history
fixed issue where multiple $ref to the same resource would not resolve
  • Loading branch information
geraintluff committed Jun 10, 2014
2 parents 7859ad9 + ddd9536 commit 0bf8708
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
8 changes: 5 additions & 3 deletions schema-store.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@ public function add($url, $schema, $trusted=FALSE) {
$this->schemas[$baseUrl] = $schema;
}
if (isset($this->refs[$baseUrl])) {
foreach ($this->refs[$baseUrl] as $fullUrl => &$refSchema) {
$refSchema = $this->get($fullUrl);
foreach ($this->refs[$baseUrl] as $fullUrl => $refSchemas) {
foreach ($refSchemas as &$refSchema) {
$refSchema = $this->get($fullUrl);
}
unset($this->refs[$baseUrl][$fullUrl]);
}
if (count($this->refs[$baseUrl]) == 0) {
Expand All @@ -151,7 +153,7 @@ private function normalizeSchema($url, &$schema, $trustPrefix = '') {
$urlParts = explode("#", $refUrl);
$baseUrl = array_shift($urlParts);
$fragment = urldecode(implode("#", $urlParts));
$this->refs[$baseUrl][$refUrl] =& $schema;
$this->refs[$baseUrl][$refUrl][] =& $schema;
}
} else if (isset($schema->id)) {
$schema->id = $url = self::resolveUrl($url, $schema->id);
Expand Down
21 changes: 21 additions & 0 deletions tests/03 - schema store/03 - references.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,25 @@
throw new Exception('There should be no more missing schemas');
}

// Add external $ref twice
$schema = json_decode('{
"title": "Test schema 3 a",
"properties": {
"foo1": {"$ref": "'.$urlBase.'test-schema-3-b#/foo"},
"foo2": {"$ref": "'.$urlBase.'test-schema-3-b#/foo"}
}
}');
$store->add($urlBase."test-schema-3-a", $schema);
$schema = json_decode('{
"title": "Test schema 3 b",
"foo": {"type": "object"}
}');
$schema = $store->add($urlBase."test-schema-3-b", $schema);
$schema = $store->get($urlBase."test-schema-3-a");
if (property_exists($schema->properties->foo1, '$ref')) {
throw new Exception('$ref was not resolved for foo1');
}
if (property_exists($schema->properties->foo2, '$ref')) {
throw new Exception('$ref was not resolved for foo2');
}
?>

0 comments on commit 0bf8708

Please sign in to comment.