Skip to content

Commit

Permalink
[BUGFIX] Don't resolve a schema id against itself (jsonrainbow#452)
Browse files Browse the repository at this point in the history
* Don't resolve a schema id against itself

* Add test for double-resolve bugfix
  • Loading branch information
erayd committed Oct 4, 2017
1 parent b80053b commit dfbcc82
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/JsonSchema/SchemaStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private function expandRefs(&$schema, $base = null)
return;
}

if (property_exists($schema, 'id') && is_string($schema->id)) {
if (property_exists($schema, 'id') && is_string($schema->id) && $base != $schema->id) {
$base = $this->uriResolver->resolve($schema->id, $base);
}

Expand Down
21 changes: 21 additions & 0 deletions tests/SchemaStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,25 @@ public function testMetaSchemaFixes()
$this->assertEquals('uri-reference', $draft_03->properties->{'$ref'}->format);
$this->assertEquals('uri-reference', $draft_04->properties->id->format);
}

public function testNoDoubleResolve()
{
$schemaOne = json_decode('{"id": "test/schema", "$ref": "../test2/schema2"}');

$uriRetriever = $this->prophesize('JsonSchema\UriRetrieverInterface');
$uriRetriever->retrieve('test/schema')->willReturn($schemaOne)->shouldBeCalled();

$s = new SchemaStorage($uriRetriever->reveal());
$schema = $s->addSchema('test/schema');

$r = new \ReflectionObject($s);
$p = $r->getProperty('schemas');
$p->setAccessible(true);
$schemas = $p->getValue($s);

$this->assertEquals(
'file://' . getcwd() . '/test2/schema2#',
$schemas['test/schema']->{'$ref'}
);
}
}

0 comments on commit dfbcc82

Please sign in to comment.