Skip to content

Commit be6817f

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

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 PHPUnit\Framework\TestCase;
18+
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
19+
20+
/**
21+
* @author Oskar Stark <[email protected]>
22+
*/
23+
class PropertyInfoPropertyNameCollectionFactoryTest extends TestCase
24+
{
25+
public function testCreateMethodReturnsEmptyPropertyNameCollection()
26+
{
27+
$factory = new PropertyInfoPropertyNameCollectionFactory(new PropertyInfoExtractor());
28+
$collection = $factory->create(ObjectWithoutPublicProperties::class);
29+
30+
self::assertCount(0, $collection->getIterator());
31+
}
32+
33+
public function testCreateMethodReturnsProperPropertyNameCollection()
34+
{
35+
$factory = new PropertyInfoPropertyNameCollectionFactory(new PropertyInfoExtractor());
36+
$collection = $factory->create(ObjectWithPublicProperties::class);
37+
38+
self::assertCount(1, $collection->getIterator());
39+
}
40+
}
41+
42+
class ObjectWithoutPublicProperties
43+
{
44+
private $foo;
45+
}
46+
47+
class ObjectWithPublicProperties
48+
{
49+
public $foo;
50+
}

0 commit comments

Comments
 (0)