Skip to content

Commit d1d2781

Browse files
committed
Fix: Allow objects without properties
1 parent f8ccee0 commit d1d2781

File tree

4 files changed

+59
-7
lines changed

4 files changed

+59
-7
lines changed

src/Bridge/Symfony/PropertyInfo/Metadata/Property/PropertyInfoPropertyNameCollectionFactory.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace ApiPlatform\Core\Bridge\Symfony\PropertyInfo\Metadata\Property;
1515

16-
use ApiPlatform\Core\Exception\RuntimeException;
1716
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
1817
use ApiPlatform\Core\Metadata\Property\PropertyNameCollection;
1918
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
@@ -36,16 +35,11 @@ public function __construct(PropertyInfoExtractorInterface $propertyInfo)
3635

3736
/**
3837
* {@inheritdoc}
39-
*
40-
* @throws RuntimeException
4138
*/
4239
public function create(string $resourceClass, array $options = []): PropertyNameCollection
4340
{
4441
$properties = $this->propertyInfo->getProperties($resourceClass, $options);
45-
if (null === $properties) {
46-
throw new RuntimeException(sprintf('There is no PropertyInfo extractor supporting the class "%s".', $resourceClass));
47-
}
4842

49-
return new PropertyNameCollection($properties);
43+
return new PropertyNameCollection($properties ?? []);
5044
}
5145
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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\Core\Tests\Bridge\Symfony\PropertyInfo\Metadata\Property;
15+
16+
use ApiPlatform\Core\Bridge\Symfony\PropertyInfo\Metadata\Property\PropertyInfoPropertyNameCollectionFactory;
17+
use ApiPlatform\Core\Tests;
18+
use PHPUnit\Framework\TestCase;
19+
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
20+
21+
/**
22+
* @author Oskar Stark <[email protected]>
23+
*/
24+
class PropertyInfoPropertyNameCollectionFactoryTest extends TestCase
25+
{
26+
public function testCreateMethodReturnsEmptyPropertyNameCollection()
27+
{
28+
$factory = new PropertyInfoPropertyNameCollectionFactory(new PropertyInfoExtractor());
29+
$collection = $factory->create(Tests\Fixtures\ObjectWithoutPublicProperties::class);
30+
31+
self::assertCount(0, $collection->getIterator());
32+
}
33+
34+
public function testCreateMethodReturnsProperPropertyNameCollection()
35+
{
36+
$factory = new PropertyInfoPropertyNameCollectionFactory(new PropertyInfoExtractor());
37+
$collection = $factory->create(Tests\Fixtures\DummyObjectWithPublicProperties::class);
38+
39+
self::assertCount(2, $collection->getIterator());
40+
}
41+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace ApiPlatform\Core\Tests\Fixtures;
4+
5+
class DummyObjectWithPublicProperties
6+
{
7+
public $foo;
8+
public $bar;
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace ApiPlatform\Core\Tests\Fixtures;
4+
5+
class ObjectWithoutPublicProperties
6+
{
7+
private $foo;
8+
}

0 commit comments

Comments
 (0)