Skip to content

Commit cf80a82

Browse files
authored
fix(laravel): skip relation metadata for abstract Eloquent models (#7933)
1 parent 88ddc36 commit cf80a82

3 files changed

Lines changed: 59 additions & 0 deletions

File tree

src/Laravel/Eloquent/Serializer/Mapping/Loader/RelationMetadataLoader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata): bool
4242
}
4343

4444
$refl = $classMetadata->getReflectionClass();
45+
if ($refl->isAbstract()) {
46+
return false;
47+
}
48+
4549
/** @var Model */
4650
$model = $refl->newInstanceWithoutConstructor();
4751
$attributesMetadata = $classMetadata->getAttributesMetadata();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Laravel\Tests\Eloquent\Serializer\Mapping\Loader;
15+
16+
use ApiPlatform\Laravel\Eloquent\Metadata\ModelMetadata;
17+
use ApiPlatform\Laravel\Eloquent\Serializer\Mapping\Loader\RelationMetadataLoader;
18+
use Orchestra\Testbench\TestCase;
19+
use Symfony\Component\Serializer\Mapping\ClassMetadata;
20+
use Workbench\App\Models\AbstractModel;
21+
22+
class RelationMetadataLoaderTest extends TestCase
23+
{
24+
/**
25+
* @see https://github.com/api-platform/core/issues/7911
26+
*/
27+
public function testLoadClassMetadataReturnsFalseForAbstractModelWithoutInstantiating(): void
28+
{
29+
$loader = new RelationMetadataLoader(new ModelMetadata());
30+
31+
$result = $loader->loadClassMetadata(new ClassMetadata(AbstractModel::class));
32+
33+
$this->assertFalse($result);
34+
}
35+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Workbench\App\Models;
15+
16+
use Illuminate\Database\Eloquent\Model;
17+
18+
abstract class AbstractModel extends Model
19+
{
20+
}

0 commit comments

Comments
 (0)