Skip to content

Commit 7e9a7c6

Browse files
authored
Merge pull request #29205 from staudenmeir/collection
[5.8] Fix collections with JsonSerializable items and mixed values
2 parents 99522fc + 0fbd751 commit 7e9a7c6

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

src/Illuminate/Support/Collection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2127,7 +2127,7 @@ protected function getArrayableItems($items)
21272127
} elseif ($items instanceof Jsonable) {
21282128
return json_decode($items->toJson(), true);
21292129
} elseif ($items instanceof JsonSerializable) {
2130-
return $items->jsonSerialize();
2130+
return (array) $items->jsonSerialize();
21312131
} elseif ($items instanceof Traversable) {
21322132
return iterator_to_array($items);
21332133
}

tests/Support/SupportCollectionTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ public function testGetArrayableItems()
178178
$array = $method->invokeArgs($collection, [$items]);
179179
$this->assertSame(['foo' => 'bar'], $array);
180180

181+
$items = new TestJsonSerializeWithScalarValueObject;
182+
$array = $method->invokeArgs($collection, [$items]);
183+
$this->assertSame(['foo'], $array);
184+
181185
$items = new Collection(['foo' => 'bar']);
182186
$array = $method->invokeArgs($collection, [$items]);
183187
$this->assertSame(['foo' => 'bar'], $array);
@@ -3297,6 +3301,14 @@ public function jsonSerialize()
32973301
}
32983302
}
32993303

3304+
class TestJsonSerializeWithScalarValueObject implements JsonSerializable
3305+
{
3306+
public function jsonSerialize()
3307+
{
3308+
return 'foo';
3309+
}
3310+
}
3311+
33003312
class TestCollectionMapIntoObject
33013313
{
33023314
public $value;

0 commit comments

Comments
 (0)