Skip to content

Commit c969aba

Browse files
committed
Throw exception when field type is incorrect
(cherry picked from commit d820bfe)
1 parent b9da3c5 commit c969aba

File tree

3 files changed

+114
-1
lines changed

3 files changed

+114
-1
lines changed

Diff for: src/InvalidFieldTypeException.php

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Swaggest\JsonDiff;
4+
5+
6+
use Swaggest\JsonDiff\JsonPatch\OpPath;
7+
use Throwable;
8+
9+
class InvalidFieldTypeException extends Exception
10+
{
11+
/** @var string */
12+
private $field;
13+
/** @var string */
14+
private $expectedType;
15+
/** @var OpPath|object */
16+
private $operation;
17+
18+
/**
19+
* @param string $field
20+
* @param string $expectedType
21+
* @param OpPath|object $operation
22+
* @param int $code
23+
* @param Throwable|null $previous
24+
*/
25+
public function __construct(
26+
$field,
27+
$expectedType,
28+
$operation,
29+
$code = 0,
30+
Throwable $previous = null
31+
)
32+
{
33+
parent::__construct(
34+
'Invalid field type - "' . $field . '" should be of type: ' . $expectedType,
35+
$code,
36+
$previous
37+
);
38+
$this->field = $field;
39+
$this->expectedType = $expectedType;
40+
$this->operation = $operation;
41+
}
42+
43+
/**
44+
* @return string
45+
*/
46+
public function getField()
47+
{
48+
return $this->field;
49+
}
50+
51+
/**
52+
* @return string
53+
*/
54+
public function getExpectedType()
55+
{
56+
return $this->expectedType;
57+
}
58+
59+
/**
60+
* @return OpPath|object
61+
*/
62+
public function getOperation()
63+
{
64+
return $this->operation;
65+
}
66+
}

Diff for: src/JsonPatch.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ public static function import(array $data)
5959
if (is_array($operation)) {
6060
$operation = (object)$operation;
6161
}
62-
6362
if (!is_object($operation)) {
6463
throw new Exception('Invalid patch operation - should be a JSON object');
6564
}
@@ -71,6 +70,13 @@ public static function import(array $data)
7170
throw new MissingFieldException('path', $operation);
7271
}
7372

73+
if (!is_string($operation->op)) {
74+
throw new InvalidFieldTypeException('op', 'string', $operation);
75+
}
76+
if (!is_string($operation->path)) {
77+
throw new InvalidFieldTypeException('path', 'string', $operation);
78+
}
79+
7480
$op = null;
7581
switch ($operation->op) {
7682
case Add::OP:
@@ -104,6 +110,8 @@ public static function import(array $data)
104110
} elseif ($op instanceof OpPathFrom) {
105111
if (!isset($operation->from)) {
106112
throw new MissingFieldException('from', $operation);
113+
} elseif (!is_string($operation->from)) {
114+
throw new InvalidFieldTypeException('from', 'string', $operation);
107115
}
108116
$op->from = $operation->from;
109117
}

Diff for: tests/src/JsonPatchTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,45 @@ public function testInvalidOp()
110110
}
111111
}
112112

113+
/**
114+
* @dataProvider provideInvalidFieldType
115+
*
116+
* @param object $operation
117+
* @param string $expectedException
118+
* @param string $expectedMessage
119+
*/
120+
public function testInvalidFieldType($operation, $expectedException, $expectedMessage)
121+
{
122+
try {
123+
JsonPatch::import(array($operation));
124+
$this->fail('Expected exception was not thrown');
125+
} catch (Exception $exception) {
126+
$this->assertInstanceOf($expectedException, $exception);
127+
$this->assertSame($expectedMessage, $exception->getMessage());
128+
}
129+
}
130+
131+
public function provideInvalidFieldType()
132+
{
133+
return [
134+
'"op" invalid type' => [
135+
(object)array('op' => array('foo' => 'bar'), 'path' => '/123', 'value' => 'test'),
136+
Exception::class,
137+
'Invalid field type - "op" should be of type: string'
138+
],
139+
'"path" invalid type' => [
140+
(object)array('op' => 'add', 'path' => array('foo' => 'bar'), 'value' => 'test'),
141+
Exception::class,
142+
'Invalid field type - "path" should be of type: string'
143+
],
144+
'"from" invalid type' => [
145+
(object)array('op' => 'move', 'path' => '/123', 'from' => array('foo' => 'bar')),
146+
Exception::class,
147+
'Invalid field type - "from" should be of type: string'
148+
]
149+
];
150+
}
151+
113152
public function testMissingFrom()
114153
{
115154
$this->setExpectedException(get_class(new Exception()), 'Missing "from" in operation data');

0 commit comments

Comments
 (0)