Skip to content

Cache schemas #74

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 2 commits into from
Sep 7, 2013
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
4 changes: 3 additions & 1 deletion src/JsonSchema/RefResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

namespace JsonSchema;

use JsonSchema\Uri\UriRetriever;
use JsonSchema\Uri\Retrievers\UriRetrieverInterface;
use JsonSchema\Exception\ResourceNotFoundException;

/**
* Take in an object that's a JSON schema and take care of all $ref references
Expand Down Expand Up @@ -41,7 +43,7 @@ public function __construct($retriever = null)
*/
public function fetchRef($ref, $sourceUri)
{
$retriever = $this->getUriRetriever();
$retriever = $this->getUriRetriever();
$jsonSchema = $retriever->retrieve($ref, $sourceUri);
$this->resolve($jsonSchema);

Expand Down
10 changes: 8 additions & 2 deletions src/JsonSchema/Uri/Retrievers/FileGetContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace JsonSchema\Uri\Retrievers;

use JsonSchema\Exception\ResourceNotFoundException;
use JsonSchema\Validator;

/**
Expand All @@ -31,9 +32,14 @@ public function retrieve($uri)

$response = file_get_contents($uri);
if (false === $response) {
throw new ResourceNotFoundException('JSON schema not found');
throw new ResourceNotFoundException('JSON schema not found at ' . $uri);
}

if ($response == ''
&& substr($uri, 0, 7) == 'file://' && substr($uri, -1) == '/'
) {
throw new ResourceNotFoundException('JSON schema not found at ' . $uri);
}

$this->messageBody = $response;
if (! empty($http_response_header)) {
$this->fetchContentType($http_response_header);
Expand Down
28 changes: 21 additions & 7 deletions src/JsonSchema/Uri/UriResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function generate(array $components)
$uri .= $components['query'];
}
if (array_key_exists('fragment', $components)) {
$uri .= $components['fragment'];
$uri .= '#' . $components['fragment'];
}

return $uri;
Expand All @@ -73,10 +73,14 @@ public function generate(array $components)
*
* @param string $uri Absolute or relative
* @param type $baseUri Optional base URI
* @return string
* @return string Absolute URI
*/
public function resolve($uri, $baseUri = null)
{
if ($uri == '') {
return $baseUri;
}

$components = $this->parse($uri);
$path = $components['path'];

Expand All @@ -87,7 +91,10 @@ public function resolve($uri, $baseUri = null)
$basePath = $baseComponents['path'];

$baseComponents['path'] = self::combineRelativePathWithBasePath($path, $basePath);

if (isset($components['fragment'])) {
$baseComponents['fragment'] = $components['fragment'];
}

return $this->generate($baseComponents);
}

Expand All @@ -99,9 +106,16 @@ public function resolve($uri, $baseUri = null)
* @return string Merged path
* @throws UriResolverException
*/
private static function combineRelativePathWithBasePath($relativePath, $basePath)
public static function combineRelativePathWithBasePath($relativePath, $basePath)
{
$relativePath = self::normalizePath($relativePath);
if ($relativePath == '') {
return $basePath;
}
if ($relativePath{0} == '/') {
return $relativePath;
}

$basePathSegments = self::getPathSegments($basePath);

preg_match('|^/?(\.\./(?:\./)*)*|', $relativePath, $match);
Expand All @@ -111,13 +125,13 @@ private static function combineRelativePathWithBasePath($relativePath, $basePath
}
$basePathSegments = array_slice($basePathSegments, 0, -$numLevelUp);
$path = preg_replace('|^/?(\.\./(\./)*)*|', '', $relativePath);

return implode(DIRECTORY_SEPARATOR, $basePathSegments) . '/' . $path;
}

/**
* Normalizes a URI path component by removing dot-slash and double slashes
*
*
* @param string $path
* @return string
*/
Expand Down
52 changes: 44 additions & 8 deletions src/JsonSchema/Uri/UriRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public function getUriRetriever()
* @param object $jsonSchema JSON Schema contents
* @param string $uri JSON Schema URI
* @return object JSON Schema after walking down the fragment pieces
*
* @throws \JsonSchema\Exception\ResourceNotFoundException
*/
public function resolvePointer($jsonSchema, $uri)
{
Expand All @@ -90,11 +92,17 @@ public function resolvePointer($jsonSchema, $uri)
if (! empty($jsonSchema->$pathElement)) {
$jsonSchema = $jsonSchema->$pathElement;
} else {
$jsonSchema = new \stdClass();
throw new \JsonSchema\Exception\ResourceNotFoundException(
'Fragment "' . $parsed['fragment'] . '" not found'
. ' in ' . $uri
);
}

if (! is_object($jsonSchema)) {
$jsonSchema = new \stdClass();
throw new \JsonSchema\Exception\ResourceNotFoundException(
'Fragment part "' . $pathElement . '" is no object '
. ' in ' . $uri
);
}
}
}
Expand All @@ -112,20 +120,48 @@ public function resolvePointer($jsonSchema, $uri)
public function retrieve($uri, $baseUri = null)
{
$resolver = new UriResolver();
$resolvedUri = $resolver->resolve($uri, $baseUri);
$resolvedUri = $fetchUri = $resolver->resolve($uri, $baseUri);

//fetch URL without #fragment
$arParts = $resolver->parse($resolvedUri);
if (isset($arParts['fragment'])) {
unset($arParts['fragment']);
$fetchUri = $resolver->generate($arParts);
}

$jsonSchema = $this->loadSchema($fetchUri);

// Use the JSON pointer if specified
$jsonSchema = $this->resolvePointer($jsonSchema, $resolvedUri);
$jsonSchema->id = $resolvedUri;

return $jsonSchema;
}

/**
* Fetch a schema from the given URI, json-decode it and return it.
* Caches schema objects.
*
* @param string $fetchUri Absolute URI
*
* @return object JSON schema object
*/
protected function loadSchema($fetchUri)
{
if (isset($this->schemaCache[$fetchUri])) {
return $this->schemaCache[$fetchUri];
}

$uriRetriever = $this->getUriRetriever();
$contents = $this->uriRetriever->retrieve($resolvedUri);
$contents = $this->uriRetriever->retrieve($fetchUri);
$this->confirmMediaType($uriRetriever);
$jsonSchema = json_decode($contents);

if (JSON_ERROR_NONE < $error = json_last_error()) {
throw new JsonDecodingException($error);
}

// Use the JSON pointer if specified
$jsonSchema = $this->resolvePointer($jsonSchema, $resolvedUri);
$jsonSchema->id = $resolvedUri;

$this->schemaCache[$fetchUri] = $jsonSchema;
return $jsonSchema;
}

Expand Down
99 changes: 99 additions & 0 deletions tests/JsonSchema/Tests/RefResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,103 @@ public function refProvider() {
),
);
}

public function testFetchRefAbsolute()
{
$retr = new \JsonSchema\Uri\Retrievers\PredefinedArray(
array(
'http://example.org/schema' => <<<JSN
{
"title": "schema",
"type": "object",
"id": "http://example.org/schema"
}
JSN
)
);

$res = new \JsonSchema\RefResolver();
$res->getUriRetriever()->setUriRetriever($retr);

$this->assertEquals(
(object) array(
'title' => 'schema',
'type' => 'object',
'id' => 'http://example.org/schema'
),
$res->fetchRef('http://example.org/schema', 'http://example.org/schema')
);
}

public function testFetchRefAbsoluteAnchor()
{
$retr = new \JsonSchema\Uri\Retrievers\PredefinedArray(
array(
'http://example.org/schema' => <<<JSN
{
"title": "schema",
"type": "object",
"id": "http://example.org/schema",
"definitions": {
"foo": {
"type": "object",
"title": "foo"
}
}
}
JSN
)
);

$res = new \JsonSchema\RefResolver();
$res->getUriRetriever()->setUriRetriever($retr);

$this->assertEquals(
(object) array(
'title' => 'foo',
'type' => 'object',
'id' => 'http://example.org/schema#/definitions/foo',
),
$res->fetchRef(
'http://example.org/schema#/definitions/foo',
'http://example.org/schema'
)
);
}

public function testFetchRefRelativeAnchor()
{
$retr = new \JsonSchema\Uri\Retrievers\PredefinedArray(
array(
'http://example.org/schema' => <<<JSN
{
"title": "schema",
"type": "object",
"id": "http://example.org/schema",
"definitions": {
"foo": {
"type": "object",
"title": "foo"
}
}
}
JSN
)
);

$res = new \JsonSchema\RefResolver();
$res->getUriRetriever()->setUriRetriever($retr);

$this->assertEquals(
(object) array(
'title' => 'foo',
'type' => 'object',
'id' => 'http://example.org/schema#/definitions/foo',
),
$res->fetchRef(
'#/definitions/foo',
'http://example.org/schema'
)
);
}
}
Loading