Skip to content

Commit a9f659e

Browse files
lkuvearutop
authored andcommitted
Fail on invalid remote JSON schema (#96)
1 parent 395a9f6 commit a9f659e

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/RefResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ public function resolveReference($referencePath)
194194
$this->setResolutionScope($url);
195195
if (null === $refResolver) {
196196
$rootData = $rootResolver->getRefProvider()->getSchemaData($url);
197-
if ($rootData === false) {
198-
throw new Exception("Failed to resolve $url", Exception::RESOLVE_FAILED);
197+
if ($rootData === null || $rootData === false) {
198+
throw new Exception("Failed to decode content from $url", Exception::RESOLVE_FAILED);
199199
}
200200

201201
$refResolver = new RefResolver($rootData);

tests/resources/invalid_json.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Swaggest\JsonSchema\Tests\PHPUnit\Schema;
4+
5+
use Swaggest\JsonSchema\Schema;
6+
7+
class InvalidSchemaTest extends \PHPUnit_Framework_TestCase
8+
{
9+
10+
public function testValidationFailedWithInvalidSchema()
11+
{
12+
$this->setExpectedException('Swaggest\JsonSchema\Exception');
13+
$data = __DIR__ . '/../../../resources/invalid_json.json';
14+
$schema = Schema::import($data);
15+
$schema->in(json_decode(<<<'JSON'
16+
{
17+
"id": 1,
18+
"name":"John Doe",
19+
"orders":[
20+
{
21+
"id":1
22+
},
23+
{
24+
"price":1.0
25+
}
26+
]
27+
}
28+
JSON
29+
));
30+
}
31+
32+
}

0 commit comments

Comments
 (0)