Skip to content

Ignore mime type on json-schema.org #73

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
33 changes: 27 additions & 6 deletions src/JsonSchema/Uri/UriRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class UriRetriever
*
* @throws InvalidSchemaMediaTypeException
*/
public function confirmMediaType($uriRetriever)
public function confirmMediaType($uriRetriever, $uri)
{
$contentType = $uriRetriever->getContentType();

Expand All @@ -42,6 +42,11 @@ public function confirmMediaType($uriRetriever)
return;
}

if (substr($uri, 0, 23) == 'http://json-schema.org/') {
//HACK; they deliver broken content types
return true;
}

throw new InvalidSchemaMediaTypeException(sprintf('Media type %s expected', Validator::SCHEMA_MEDIA_TYPE));
}

Expand Down Expand Up @@ -72,6 +77,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 +97,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,10 +125,18 @@ 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);
}

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

if (JSON_ERROR_NONE < $error = json_last_error()) {
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