Skip to content

Commit 8dc9b9d

Browse files
committed
Merge pull request #161 from f-go/max_depth_fix
Throw exception if max depth exceeded
2 parents 70a03b4 + 0398e45 commit 8dc9b9d

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/JsonSchema/RefResolver.php

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

1010
namespace JsonSchema;
1111

12+
use JsonSchema\Exception\JsonDecodingException;
1213
use JsonSchema\Uri\Retrievers\UriRetrieverInterface;
1314
use JsonSchema\Uri\UriRetriever;
1415

@@ -96,7 +97,7 @@ public function getUriRetriever()
9697
public function resolve($schema, $sourceUri = null)
9798
{
9899
if (self::$depth > self::$maxDepth) {
99-
return;
100+
throw new JsonDecodingException(JSON_ERROR_DEPTH);
100101
}
101102
++self::$depth;
102103

tests/JsonSchema/Tests/RefResolverTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,4 +334,25 @@ public function testFetchRef()
334334

335335
$this->assertEquals($jsonSchema, $resolver->fetchRef($ref, $sourceUri));
336336
}
337+
338+
/**
339+
* @expectedException \JsonSchema\Exception\JsonDecodingException
340+
*/
341+
public function testMaxDepthExceeded()
342+
{
343+
// stub schema
344+
$jsonSchema = new \stdClass;
345+
$jsonSchema->id = 'stub';
346+
$jsonSchema->additionalItems = 'stub';
347+
348+
// mock retriever
349+
$retriever = $this->getMock('JsonSchema\Uri\UriRetriever', array('retrieve'));
350+
$retriever->expects($this->any())->method('retrieve')->will($this->returnValue($jsonSchema));
351+
352+
// stub resolver
353+
\JsonSchema\RefResolver::$maxDepth = 0;
354+
$resolver = new \JsonSchema\RefResolver($retriever);
355+
356+
$resolver->resolve($jsonSchema);
357+
}
337358
}

0 commit comments

Comments
 (0)