Skip to content

Commit 87b25f8

Browse files
authored
Merge pull request #53 from swaggest/issue-52
Fix #52, proper `not` error handling
2 parents 3f2ddc6 + ff1c9d7 commit 87b25f8

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

src/InvalidValue.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ public function addPath($path)
1717
$this->error = $this->message;
1818
}
1919
$this->path = $path;
20-
$this->message .= ' at ' . $path;
20+
if ('#' !== $this->path) {
21+
$this->message .= ' at ' . $path;
22+
}
2123
}
2224

2325
const INVALID_VALUE = 1;
@@ -32,8 +34,10 @@ public function inspect()
3234
$error->dataPointer = PointerUtil::getDataPointer($error->processingPath);
3335
$error->schemaPointers = PointerUtil::getSchemaPointers($error->processingPath);
3436
if ($this instanceof LogicException) {
35-
foreach ($this->subErrors as $subError) {
36-
$error->subErrors[] = $subError->inspect();
37+
if ($this->subErrors !== null) {
38+
foreach ($this->subErrors as $subError) {
39+
$error->subErrors[] = $subError->inspect();
40+
}
3741
}
3842
}
3943
return $error;

src/Path/PointerUtil.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,14 @@ public static function getSchemaPointers($path, $isURIFragmentId = false)
5353
$pointer .= '/' . JsonPointer::escapeSegment($schemaPaths[0], $isURIFragmentId);
5454
if (isset($schemaPaths[1])) {
5555
$pointer .= '/' . JsonPointer::escapeSegment($schemaPaths[1], $isURIFragmentId);
56-
} elseif ($parts[1]) {
56+
} elseif (isset($parts[1])) {
5757
$pointer .= '/' . JsonPointer::escapeSegment($parts[1], $isURIFragmentId);
5858
}
5959
}
6060
}
61+
if ($pointer === '') {
62+
$pointer = '/';
63+
}
6164
$result[] = $pointer;
6265
return $result;
6366
}
@@ -81,6 +84,9 @@ public static function getDataPointer($path, $isURIFragmentId = false)
8184
$result .= '/' . JsonPointer::escapeSegment($parts[1], $isURIFragmentId);
8285
}
8386
}
87+
if ($result === '') {
88+
return '/';
89+
}
8490
return $result;
8591
}
8692

src/Schema.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ private function processNot($data, Context $options, $path)
284284
// Expected exception
285285
}
286286
if ($exception === false) {
287-
$this->fail(new LogicException('Not ' . json_encode($this->not) . ' expected, ' . json_encode($data) . ' received'), $path);
287+
$this->fail(new LogicException('Not ' . json_encode($this->not) . ' expected, ' . json_encode($data) . ' received'), $path . '->not');
288288
}
289289
}
290290

@@ -1167,9 +1167,7 @@ public function setUseObjectAsArray($useObjectAsArray)
11671167
*/
11681168
private function fail(InvalidValue $exception, $path)
11691169
{
1170-
if ($path !== '#') {
1171-
$exception->addPath($path);
1172-
}
1170+
$exception->addPath($path);
11731171
throw $exception;
11741172
}
11751173

tests/src/PHPUnit/Error/ErrorTest.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
namespace Swaggest\JsonSchema\Tests\PHPUnit\Error;
44

5-
6-
use Swaggest\JsonSchema\Exception\LogicException;
75
use Swaggest\JsonSchema\InvalidValue;
8-
use Swaggest\JsonSchema\Path\Error;
96
use Swaggest\JsonSchema\Schema;
107

118
class ErrorTest extends \PHPUnit_Framework_TestCase
@@ -180,4 +177,26 @@ public function testErrorMessage()
180177
}
181178
}
182179

180+
181+
public function testNoSubErrors()
182+
{
183+
$schema = Schema::import(json_decode(<<<'JSON'
184+
{
185+
"not": {
186+
"type": "string"
187+
}
188+
}
189+
JSON
190+
));
191+
192+
try {
193+
$schema->in('abc');
194+
} catch (InvalidValue $exception) {
195+
$this->assertSame('Not {"type":"string"} expected, "abc" received at #->not', $exception->getMessage());
196+
197+
$error = $exception->inspect();
198+
$this->assertSame('Not {"type":"string"} expected, "abc" received', $error->error);
199+
}
200+
}
201+
183202
}

0 commit comments

Comments
 (0)