Skip to content

Commit

Permalink
Throw exception when mapping non-flat values in flat typed properties
Browse files Browse the repository at this point in the history
Flat types can all be casted to each other,
but objects and arrays can't be casted to flat types.

Previously an E_NOTICE has been raised:
> Array to string conversion

This is prevented now, and a proper JsonMapper_Exception is thrown.
  • Loading branch information
dktapps authored and cweiske committed Aug 1, 2023
1 parent ed5f95b commit d63a7a8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
8 changes: 5 additions & 3 deletions src/JsonMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,13 @@ public function map($json, $object)
} else if ($this->isSimpleType($type)
&& !(is_array($jvalue) && $this->hasVariadicArrayType($accessor))
) {
if ($type === 'string' && is_object($jvalue)) {
if ($this->isFlatType($type)
&& !$this->isFlatType(gettype($jvalue))
) {
throw new JsonMapper_Exception(
'JSON property "' . $key . '" in class "'
. $strClassName . '" is an object and'
. ' cannot be converted to a string'
. $strClassName . '" is of type ' . gettype($jvalue) . ' and'
. ' cannot be converted to ' . $type
);
}
settype($jvalue, $type);
Expand Down
13 changes: 13 additions & 0 deletions tests/Array_PHP74_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Array_PHP74_Test extends TestCase
protected function setUp(): void
{
require_once 'JsonMapperTest/PHP74_Array.php';
require_once 'JsonMapperTest/ArrayValueForStringProperty.php';
}

public function testJsonMapper()
Expand All @@ -28,4 +29,16 @@ public function testJsonMapper()
$array = $jsonMapper->map($json, new PHP74_Array());
self::assertCount(1, $array->files);
}

public function testMapArrayValueToStringProperty()
{

$jm = new JsonMapper();
$this->expectException(JsonMapper_Exception::class);
$this->expectExceptionMessage('JSON property "value" in class "JsonMapperTest_ArrayValueForStringProperty" is of type array and cannot be converted to string');
$jm->map(
json_decode('{"value":[]}'),
new JsonMapperTest_ArrayValueForStringProperty()
);
}
}
16 changes: 16 additions & 0 deletions tests/JsonMapperTest/ArrayValueForStringProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* Part of JsonMapper
*
* PHP version 5
*
* @package JsonMapper
* @author Christian Weiske <[email protected]>
* @license OSL-3.0 http://opensource.org/licenses/osl-3.0
* @link http://cweiske.de/
*/

class JsonMapperTest_ArrayValueForStringProperty
{
public string $value;
}
2 changes: 1 addition & 1 deletion tests/ObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public function testObjectInvalidNull()
public function testObjectInsteadOfString()
{
$this->expectException(JsonMapper_Exception::class);
$this->expectExceptionMessage('JSON property "pString" in class "JsonMapperTest_Object" is an object and cannot be converted to a string');
$this->expectExceptionMessage('JSON property "pString" in class "JsonMapperTest_Object" is of type object and cannot be converted to string');
$jm = new JsonMapper();
$sn = $jm->map(
json_decode('{"pString":{"key":"val"}}'),
Expand Down

0 comments on commit d63a7a8

Please sign in to comment.