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 d54d4ff
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/JsonMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,11 @@ 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
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
17 changes: 17 additions & 0 deletions tests/SimpleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,5 +247,22 @@ public function testMapSimpleHyphenSetter()
);

}

/**
* @requires PHP 7.4
*/
public function testMapArrayValueToStringProperty()
{
//do that here and not on top of the file - only for php 7.4+
require_once 'JsonMapperTest/ArrayValueForStringProperty.php';

$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()
);
}
}
?>

0 comments on commit d54d4ff

Please sign in to comment.