Skip to content

Commit 065aa86

Browse files
committed
Workaround for BC break with json-schema v2.0.3
Workaround to jsonrainbow/json-schema#262
1 parent 275b3fb commit 065aa86

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace FR3D\SwaggerAssertions\JsonSchema\Uri\Retrievers;
4+
5+
use JsonSchema\Exception\ResourceNotFoundException;
6+
use JsonSchema\Uri\Retrievers\FileGetContents;
7+
8+
/**
9+
* Workaround to BC Break https://github.com/justinrainbow/json-schema/pull/262
10+
*/
11+
class FileGetContentsRetriever extends FileGetContents
12+
{
13+
public function retrieve($uri)
14+
{
15+
set_error_handler(function () use ($uri) {
16+
throw new ResourceNotFoundException('JSON schema not found at ' . $uri);
17+
});
18+
$response = file_get_contents($uri);
19+
restore_error_handler();
20+
21+
if (false === $response) {
22+
throw new ResourceNotFoundException('JSON schema was not retrieved at ' . $uri);
23+
}
24+
25+
if ($response == '' && substr($uri, 0, 7) == 'file://' && substr($uri, -1) == '/') {
26+
throw new ResourceNotFoundException('JSON schema not found at ' . $uri);
27+
}
28+
29+
$this->messageBody = $response;
30+
if (!empty($http_response_header)) {
31+
$this->fetchContentType($http_response_header);
32+
} else {
33+
// Could be a "file://" url or something else - fake up the response
34+
$this->contentType = null;
35+
}
36+
37+
return $this->messageBody;
38+
}
39+
40+
/**
41+
* @param array $headers HTTP Response Headers
42+
*
43+
* @return boolean Whether the Content-Type header was found or not
44+
*/
45+
private function fetchContentType(array $headers)
46+
{
47+
foreach ($headers as $header) {
48+
if ($this->contentType = self::getContentTypeMatchInHeader($header)) {
49+
return true;
50+
}
51+
}
52+
53+
return false;
54+
}
55+
}

src/SchemaManager.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace FR3D\SwaggerAssertions;
44

5+
use FR3D\SwaggerAssertions\JsonSchema\Uri\Retrievers\FileGetContentsRetriever;
56
use InvalidArgumentException;
67
use JsonSchema\RefResolver;
78
use JsonSchema\Uri\UriResolver;
@@ -30,7 +31,7 @@ class SchemaManager
3031
*/
3132
public static function fromUri($definitionUri)
3233
{
33-
$refResolver = new RefResolver(new UriRetriever(), new UriResolver());
34+
$refResolver = new RefResolver((new UriRetriever())->setUriRetriever(new FileGetContentsRetriever()), new UriResolver());
3435

3536
return new self($refResolver->resolve($definitionUri));
3637
}

0 commit comments

Comments
 (0)