Skip to content

Commit

Permalink
allow arrays of objects in resource collections (#30800)
Browse files Browse the repository at this point in the history
  • Loading branch information
SjorsO authored and taylorotwell committed Dec 11, 2019
1 parent 2206d52 commit f799c41
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Illuminate/Http/Resources/CollectsResources.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Http\Resources;

use Illuminate\Pagination\AbstractPaginator;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

trait CollectsResources
Expand All @@ -19,6 +20,10 @@ protected function collectResource($resource)
return $resource;
}

if (is_array($resource)) {
$resource = new Collection($resource);
}

$collects = $this->collects();

$this->collection = $collects && ! $resource->first() instanceof $collects
Expand Down
16 changes: 16 additions & 0 deletions tests/Integration/Http/Fixtures/ObjectResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Illuminate\Tests\Integration\Http\Fixtures;

use Illuminate\Http\Resources\Json\JsonResource;

class ObjectResource extends JsonResource
{
public function toArray($request)
{
return [
'name' => $this->first_name,
'age' => $this->age,
];
}
}
42 changes: 42 additions & 0 deletions tests/Integration/Http/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Tests\Integration\Http\Fixtures\Author;
use Illuminate\Tests\Integration\Http\Fixtures\AuthorResourceWithOptionalRelationship;
use Illuminate\Tests\Integration\Http\Fixtures\EmptyPostCollectionResource;
use Illuminate\Tests\Integration\Http\Fixtures\ObjectResource;
use Illuminate\Tests\Integration\Http\Fixtures\Post;
use Illuminate\Tests\Integration\Http\Fixtures\PostCollectionResource;
use Illuminate\Tests\Integration\Http\Fixtures\PostResource;
Expand Down Expand Up @@ -55,6 +56,47 @@ public function testResourcesMayBeConvertedToJson()
]);
}

public function testAnObjectsMayBeConvertedToJson()
{
Route::get('/', function () {
return ObjectResource::make(
(object) ['first_name' => 'Bob', 'age' => 40]
);
});

$this->withoutExceptionHandling()
->get('/', ['Accept' => 'application/json'])
->assertStatus(200)
->assertExactJson([
'data' => [
'name' => 'Bob',
'age' => 40,
],
]);
}

public function testArraysWithObjectsMayBeConvertedToJson()
{
Route::get('/', function () {
$objects = [
(object) ['first_name' => 'Bob', 'age' => 40],
(object) ['first_name' => 'Jack', 'age' => 25],
];

return ObjectResource::collection($objects);
});

$this->withoutExceptionHandling()
->get('/', ['Accept' => 'application/json'])
->assertStatus(200)
->assertExactJson([
'data' => [
['name' => 'Bob', 'age' => 40],
['name' => 'Jack', 'age' => 25],
],
]);
}

public function testResourcesMayHaveNoWrap()
{
Route::get('/', function () {
Expand Down

0 comments on commit f799c41

Please sign in to comment.