Fix #17886: yii\rest\Serializer serialize arrays

This commit is contained in:
patacca
2020-02-22 16:38:03 +01:00
committed by Alexander Makarov
parent 110199bcfd
commit ecbafa2e3a
3 changed files with 33 additions and 0 deletions

View File

@ -6,6 +6,7 @@ Yii Framework 2 Change Log
- Bug #17859: Fix loading fixtures under Windows (samdark)
- Bug #11945: Fix Schema Builder MySQL column definition order (simialbi)
- Bug #17886: yii\rest\Serializer serialize arrays (patacca)
- Bug #16334: Serializer support `\JsonSerializable` (germanow)
- Bug #17798: Avoid creating folder for stream log targets in `FileTarget` (wapmorgan)
- Bug #17850: Update to `ReplaceArrayValue` config exception message (alex-code)

View File

@ -152,6 +152,12 @@ class Serializer extends Component
return $this->serializeModel($data);
} elseif ($data instanceof DataProviderInterface) {
return $this->serializeDataProvider($data);
} elseif (is_array($data)) {
$serializedArray = [];
foreach ($data as $key => $value) {
$serializedArray[$key] = $this->serialize($value);
}
return $serializedArray;
}
return $data;

View File

@ -442,6 +442,32 @@ class SerializerTest extends TestCase
],
$serializer->serialize($model));
}
/**
* @see https://github.com/yiisoft/yii2/issues/17886
*/
public function testSerializeArray()
{
$serializer = new Serializer();
$model1 = new TestModel();
$model2 = new TestModel();
$model3 = new TestModel();
$this->assertSame([
[
'field1' => 'test',
'field2' => 2,
],
[
'field1' => 'test',
'field2' => 2,
],
'testKey' => [
'field1' => 'test',
'field2' => 2,
],
], $serializer->serialize([$model1, $model2, 'testKey' => $model3]));
}
}
class TestModel extends Model