Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.6] Fix numeric keys in resources #24339

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ protected function filter($data)
}

if (is_numeric($key) && $value instanceof MergeValue) {
return $this->merge($data, $index, $this->filter($value->data), $numericKeys);
return $this->merge(
$data, $index, $this->filter($value->data),
array_values($value->data) === $value->data
);
}

if ($value instanceof self && is_null($value->resource)) {
Expand Down Expand Up @@ -80,8 +83,7 @@ protected function removeMissingValues($data, $numericKeys = false)
}
}

return ! empty($data) && is_numeric(array_keys($data)[0])
? array_values($data) : $data;
return $numericKeys ? array_values($data) : $data;
}

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/Integration/Http/Fixtures/PostResourceWithNumericKeys.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Illuminate\Tests\Integration\Http\Fixtures;

class PostResourceWithNumericKeys extends PostResource
{
public function toArray($request)
{
return [
'array' => [1 => 'foo', 2 => 'bar'],
];
}
}
22 changes: 21 additions & 1 deletion tests/Integration/Http/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Illuminate\Tests\Integration\Http\Fixtures\SerializablePostResource;
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithExtraData;
use Illuminate\Tests\Integration\Http\Fixtures\EmptyPostCollectionResource;
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithNumericKeys;
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalData;
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalMerging;
use Illuminate\Tests\Integration\Http\Fixtures\PostResourceWithOptionalRelationship;
Expand Down Expand Up @@ -524,7 +525,26 @@ public function test_original_on_response_is_collection_of_model_when_collection
});
}

public function test_leading_merge__keyed_value_is_merged_correctly()
public function test_numeric_keys()
{
Route::get('/', function () {
return new PostResourceWithNumericKeys(new Post());
});

$response = $this->withoutExceptionHandling()->get(
'/', ['Accept' => 'application/json']
);

$response->assertStatus(200);

$response->assertJson([
'data' => [
'array' => [1 => 'foo', 2 => 'bar'],
],
]);
}

public function test_leading_merge_keyed_value_is_merged_correctly()
{
$filter = new class {
use ConditionallyLoadsAttributes;
Expand Down