Skip to content

Fix: Allow objects without properties #3544

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

Merged
Merged
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 @@ -13,7 +13,6 @@

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

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

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

return new PropertyNameCollection($properties);
return new PropertyNameCollection($properties ?? []);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Bridge\Symfony\PropertyInfo\Metadata\Property;

use ApiPlatform\Core\Bridge\Symfony\PropertyInfo\Metadata\Property\PropertyInfoPropertyNameCollectionFactory;
use ApiPlatform\Core\Tests\Fixtures\DummyObjectWithOnlyPrivateProperty;
use ApiPlatform\Core\Tests\Fixtures\DummyObjectWithOnlyPublicProperty;
use ApiPlatform\Core\Tests\Fixtures\DummyObjectWithoutProperty;
use ApiPlatform\Core\Tests\Fixtures\DummyObjectWithPublicAndPrivateProperty;
use PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;

/**
* @author Oskar Stark <[email protected]>
*/
class PropertyInfoPropertyNameCollectionFactoryTest extends TestCase
{
public function testCreateMethodReturnsEmptyPropertyNameCollectionForObjectWithOnlyPrivateProperty()
{
$factory = new PropertyInfoPropertyNameCollectionFactory(new PropertyInfoExtractor([
new ReflectionExtractor(),
]));

$collection = $factory->create(DummyObjectWithOnlyPrivateProperty::class);

self::assertCount(0, $collection->getIterator());
}

public function testCreateMethodReturnsEmptyPropertyNameCollectionForObjectWithoutProperties()
{
$factory = new PropertyInfoPropertyNameCollectionFactory(new PropertyInfoExtractor([
new ReflectionExtractor(),
]));

$collection = $factory->create(DummyObjectWithoutProperty::class);

self::assertCount(0, $collection->getIterator());
}

public function testCreateMethodReturnsProperPropertyNameCollectionForObjectWithPublicAndPrivateProperty()
{
$factory = new PropertyInfoPropertyNameCollectionFactory(new PropertyInfoExtractor([
new ReflectionExtractor(),
]));

$collection = $factory->create(DummyObjectWithPublicAndPrivateProperty::class);

self::assertCount(1, $collection->getIterator());
}

public function testCreateMethodReturnsProperPropertyNameCollectionForObjectWithPublicProperty()
{
$factory = new PropertyInfoPropertyNameCollectionFactory(new PropertyInfoExtractor([
new ReflectionExtractor(),
]));

$collection = $factory->create(DummyObjectWithOnlyPublicProperty::class);

self::assertCount(1, $collection->getIterator());
}
}
19 changes: 19 additions & 0 deletions tests/Fixtures/DummyObjectWithOnlyPrivateProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Fixtures;

class DummyObjectWithOnlyPrivateProperty
{
private $foo;
}
19 changes: 19 additions & 0 deletions tests/Fixtures/DummyObjectWithOnlyPublicProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Fixtures;

class DummyObjectWithOnlyPublicProperty
{
public $foo;
}
20 changes: 20 additions & 0 deletions tests/Fixtures/DummyObjectWithPublicAndPrivateProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Fixtures;

class DummyObjectWithPublicAndPrivateProperty
{
public $foo;
private $bar;
}
18 changes: 18 additions & 0 deletions tests/Fixtures/DummyObjectWithoutProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Fixtures;

class DummyObjectWithoutProperty
{
}