Skip to content

Commit

Permalink
Fix broken "array"-typed properties
Browse files Browse the repository at this point in the history
Resolves: #205
Close: #206
  • Loading branch information
reinfi authored and cweiske committed Feb 3, 2023
1 parent 74b702e commit 67632ea
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/JsonMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ public function map($json, $object)
} else if ($this->isObjectOfSameType($type, $jvalue)) {
$this->setProperty($object, $accessor, $jvalue);
continue;
} else if ($this->isSimpleType($type) && !is_array($jvalue)) {
} else if ($this->isSimpleType($type)
&& !(is_array($jvalue) && $this->hasVariadicArrayType($accessor))
) {
if ($type === 'string' && is_object($jvalue)) {
throw new JsonMapper_Exception(
'JSON property "' . $key . '" in class "'
Expand Down
31 changes: 31 additions & 0 deletions tests/Array_PHP74_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

use PHPUnit\Framework\TestCase;

/**
* Unit tests for JsonMapper's support for PHP 7.4 typed arrays
*
* @category Tools
* @package JsonMapper
* @author Martin Reinfandt <martin.reinfandt@check24.de>
* @license OSL-3.0 http://opensource.org/licenses/osl-3.0
* @link https://github.com/cweiske/jsonmapper
* @requires PHP 7.4
*/
class Array_PHP74_Test extends TestCase
{
protected function setUp(): void
{
require_once 'JsonMapperTest/PHP74_Array.php';
}

public function testJsonMapper()
{
$json = json_decode('{"files": ["test.txt"]}');
$jsonMapper = new \JsonMapper();
$array = $jsonMapper->map($json, new PHP74_Array());
self::assertCount(1, $array->files);
}
}
8 changes: 8 additions & 0 deletions tests/JsonMapperTest/PHP74_Array.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

class PHP74_Array
{
public array $files;
}

0 comments on commit 67632ea

Please sign in to comment.