Skip to content

Commit 86365be

Browse files
authoredAug 22, 2024··
fix(laravel): Eloquent PropertyAccessor (#6536)
* fix(laravel): null values handling in Eloquent PropertyAccessor * add test
1 parent b8368c6 commit 86365be

File tree

5 files changed

+33
-10
lines changed

5 files changed

+33
-10
lines changed
 

‎src/Laravel/Eloquent/PropertyAccess/PropertyAccessor.php

+17-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace ApiPlatform\Laravel\Eloquent\PropertyAccess;
1515

16-
use Illuminate\Database\Eloquent\Relations\HasMany;
16+
use Illuminate\Database\Eloquent\Model;
1717
use Symfony\Component\PropertyAccess\PropertyAccess;
1818
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
1919
use Symfony\Component\PropertyAccess\PropertyPathInterface;
@@ -36,6 +36,12 @@ public function __construct(
3636
*/
3737
public function setValue(object|array &$objectOrArray, string|PropertyPathInterface $propertyPath, mixed $value): void
3838
{
39+
if ($objectOrArray instanceof Model) {
40+
$objectOrArray->{$propertyPath} = $value;
41+
42+
return;
43+
}
44+
3945
$this->inner->setValue($objectOrArray, $propertyPath, $value);
4046
}
4147

@@ -44,20 +50,22 @@ public function setValue(object|array &$objectOrArray, string|PropertyPathInterf
4450
*/
4551
public function getValue(object|array $objectOrArray, string|PropertyPathInterface $propertyPath): mixed
4652
{
47-
$value = $this->inner->getValue($objectOrArray, $propertyPath);
48-
49-
if ($value instanceof HasMany) {
53+
if ($objectOrArray instanceof Model) {
5054
return $objectOrArray->{$propertyPath};
5155
}
5256

53-
return $value;
57+
return $this->inner->getValue($objectOrArray, $propertyPath);
5458
}
5559

5660
/**
5761
* @param array<mixed, mixed>|object $objectOrArray
5862
*/
5963
public function isWritable(object|array $objectOrArray, string|PropertyPathInterface $propertyPath): bool
6064
{
65+
if ($objectOrArray instanceof Model) {
66+
return true;
67+
}
68+
6169
return $this->inner->isWritable($objectOrArray, $propertyPath);
6270
}
6371

@@ -66,6 +74,10 @@ public function isWritable(object|array $objectOrArray, string|PropertyPathInter
6674
*/
6775
public function isReadable(object|array $objectOrArray, string|PropertyPathInterface $propertyPath): bool
6876
{
77+
if ($objectOrArray instanceof Model) {
78+
return true;
79+
}
80+
6981
return $this->inner->isReadable($objectOrArray, $propertyPath);
7082
}
7183
}

‎src/Laravel/Tests/JsonApiTest.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,19 @@ public function testCreateBook(): void
6969
'/api/books',
7070
[
7171
'data' => [
72-
'attributes' => ['name' => 'Don Quichotte', 'isbn' => fake()->isbn13(), 'publicationDate' => fake()->date()],
73-
'relationships' => ['author' => ['data' => ['id' => $this->getIriFromResource($author), 'type' => 'Author']]],
72+
'attributes' => [
73+
'name' => 'Don Quichotte',
74+
'isbn' => fake()->isbn13(),
75+
'publicationDate' => fake()->optional()->date(),
76+
],
77+
'relationships' => [
78+
'author' => [
79+
'data' => [
80+
'id' => $this->getIriFromResource($author),
81+
'type' => 'Author',
82+
],
83+
],
84+
],
7485
],
7586
],
7687
[

‎src/Laravel/Tests/JsonLdTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function testCreateBook(): void
6363
'name' => 'Don Quichotte',
6464
'author' => $this->getIriFromResource($author),
6565
'isbn' => fake()->isbn13(),
66-
'publicationDate' => fake()->date(),
66+
'publicationDate' => fake()->optional()->date(),
6767
],
6868
[
6969
'accept' => 'application/ld+json',

‎src/Laravel/workbench/database/factories/BookFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function definition(): array
3636
'id' => (string) new Ulid(),
3737
'author_id' => AuthorFactory::new(),
3838
'isbn' => fake()->isbn13(),
39-
'publication_date' => fake()->date(),
39+
'publication_date' => fake()->optional()->date(),
4040
];
4141
}
4242
}

‎src/Laravel/workbench/database/migrations/2023_07_15_231244_create_book_table.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function up(): void
3131
$table->ulid('id')->primary();
3232
$table->string('name');
3333
$table->string('isbn');
34-
$table->date('publication_date');
34+
$table->date('publication_date')->nullable();
3535
$table->integer('author_id')->unsigned();
3636
$table->foreign('author_id')->references('id')->on('authors');
3737
$table->timestamps();

0 commit comments

Comments
 (0)
Please sign in to comment.