Skip to content
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

Cache the results of RefResolver::resolve() #290

Merged
merged 2 commits into from
Aug 11, 2016
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
34 changes: 12 additions & 22 deletions src/JsonSchema/RefResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class RefResolver
/** @var UriResolverInterface */
private $uriResolver;

private $paths = array();

/**
* @param UriRetrieverInterface $retriever
* @param UriResolverInterface $uriResolver
Expand All @@ -48,26 +50,16 @@ public function __construct(UriRetrieverInterface $retriever, UriResolverInterfa
* @return object
*/
public function resolve($sourceUri)
{
return $this->resolveCached($sourceUri, array());
}

/**
* @param string $sourceUri URI where this schema was located
* @param array $paths
* @return object
*/
private function resolveCached($sourceUri, array $paths)
{
$jsonPointer = new JsonPointer($sourceUri);

$fileName = $jsonPointer->getFilename();
if (!array_key_exists($fileName, $paths)) {
if (!array_key_exists($fileName, $this->paths)) {
$schema = $this->uriRetriever->retrieve($jsonPointer->getFilename());
$paths[$jsonPointer->getFilename()] = $schema;
$this->resolveSchemas($schema, $jsonPointer->getFilename(), $paths);
$this->paths[$jsonPointer->getFilename()] = $schema;
$this->resolveSchemas($schema, $jsonPointer->getFilename());
}
$schema = $paths[$fileName];
$schema = $this->paths[$fileName];

return $this->getRefSchema($jsonPointer, $schema);
}
Expand All @@ -77,16 +69,15 @@ private function resolveCached($sourceUri, array $paths)
*
* @param object $unresolvedSchema
* @param string $fileName
* @param array $paths
*/
private function resolveSchemas($unresolvedSchema, $fileName, array $paths)
private function resolveSchemas($unresolvedSchema, $fileName)
{
$objectIterator = new ObjectIterator($unresolvedSchema);
foreach ($objectIterator as $toResolveSchema) {
if (property_exists($toResolveSchema, '$ref') && is_string($toResolveSchema->{'$ref'})) {
$jsonPointer = new JsonPointer($this->uriResolver->resolve($toResolveSchema->{'$ref'}, $fileName));
$refSchema = $this->resolveCached((string) $jsonPointer, $paths);
$this->unionSchemas($refSchema, $toResolveSchema, $fileName, $paths);
$refSchema = $this->resolve((string) $jsonPointer);
$this->unionSchemas($refSchema, $toResolveSchema, $fileName);
}
}
}
Expand Down Expand Up @@ -120,14 +111,13 @@ private function getRefSchema(JsonPointer $jsonPointer, $refSchema)
* @param object $refSchema
* @param object $schema
* @param string $fileName
* @param array $paths
*/
private function unionSchemas($refSchema, $schema, $fileName, array $paths)
private function unionSchemas($refSchema, $schema, $fileName)
{
if (property_exists($refSchema, '$ref')) {
$jsonPointer = new JsonPointer($this->uriResolver->resolve($refSchema->{'$ref'}, $fileName));
$newSchema = $this->resolveCached((string) $jsonPointer, $paths);
$this->unionSchemas($newSchema, $refSchema, $fileName, $paths);
$newSchema = $this->resolve((string) $jsonPointer);
$this->unionSchemas($newSchema, $refSchema, $fileName);
}

unset($schema->{'$ref'});
Expand Down
23 changes: 23 additions & 0 deletions tests/RefResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,29 @@ function testUnresolvableJsonPointExceptionShouldBeThrown()
$refResolver->resolve('http://www.example.com/schema.json');
}

public function testExternalReferencesLoadedOnlyOnce()
{
$mainSchema = $this->getMainSchema();
$schema2 = $this->getSchema2();
$schema3 = $this->getSchema3();

/** @var UriRetriever $uriRetriever */
$uriRetriever = $this->prophesize('JsonSchema\UriRetrieverInterface');
$uriRetriever->retrieve('http://www.example.com/schema.json')
->willReturn($mainSchema)
->shouldBeCalledTimes(1);
$uriRetriever->retrieve('http://www.my-domain.com/schema2.json')
->willReturn($schema2)
->shouldBeCalledTimes(1);
$uriRetriever->retrieve('http://www.my-domain.com/schema3.json')
->willReturn($schema3)
->shouldBeCalledTimes(1);

$refResolver = new RefResolver($uriRetriever->reveal(), new UriResolver());
$refResolver->resolve('http://www.example.com/schema.json');
$refResolver->resolve('http://www.example.com/schema.json');
}

/**
* @return object
*/
Expand Down