diff --git a/src/Metadata/Property/Factory/AnnotationPropertyNameCollectionFactory.php b/src/Metadata/Property/Factory/AnnotationPropertyNameCollectionFactory.php index b67a078b97d..8323a870775 100644 --- a/src/Metadata/Property/Factory/AnnotationPropertyNameCollectionFactory.php +++ b/src/Metadata/Property/Factory/AnnotationPropertyNameCollectionFactory.php @@ -74,7 +74,7 @@ public function create(string $resourceClass, array $options = []): PropertyName } $propertyName = $this->reflection->getProperty($reflectionMethod->name); - if (!preg_match('/^[A-Z]{2,}/', $propertyName)) { + if (!$reflectionClass->hasProperty($propertyName) && !preg_match('/^[A-Z]{2,}/', $propertyName)) { $propertyName = lcfirst($propertyName); } diff --git a/tests/Fixtures/TestBundle/Entity/UpperCaseIdentifierDummy.php b/tests/Fixtures/TestBundle/Entity/UpperCaseIdentifierDummy.php new file mode 100644 index 00000000000..c12bcfdba14 --- /dev/null +++ b/tests/Fixtures/TestBundle/Entity/UpperCaseIdentifierDummy.php @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity; + +use ApiPlatform\Core\Annotation\ApiResource; +use Doctrine\ORM\Mapping as ORM; + +/** + * UpperCaseIdentifier dummy. + * + * @ApiResource + * @ORM\Entity + */ +class UpperCaseIdentifierDummy +{ + /** + * @var string The custom identifier + * + * @ORM\Column(type="guid") + * @ORM\Id + */ + private $Uuid; + + /** + * @var string The dummy name + * + * @ORM\Column(length=30) + */ + private $name; + + public function getUuid(): string + { + return $this->Uuid; + } + + public function setUuid(string $Uuid) + { + $this->Uuid = $Uuid; + } + + public function getName(): string + { + return $this->name; + } + + public function setName(string $name) + { + $this->name = $name; + } +} diff --git a/tests/Fixtures/TestBundle/Entity/UuidIdentifierDummy.php b/tests/Fixtures/TestBundle/Entity/UuidIdentifierDummy.php index c62f5c9a849..8f86fd5ff12 100644 --- a/tests/Fixtures/TestBundle/Entity/UuidIdentifierDummy.php +++ b/tests/Fixtures/TestBundle/Entity/UuidIdentifierDummy.php @@ -17,6 +17,8 @@ /** * Custom identifier dummy. * + * @author Exploit.cz + * * @ApiResource * @ORM\Entity */ diff --git a/tests/Metadata/Property/Factory/AnnotationPropertyNameCollectionFactoryTest.php b/tests/Metadata/Property/Factory/AnnotationPropertyNameCollectionFactoryTest.php index c0d6a9233b1..0a764559550 100644 --- a/tests/Metadata/Property/Factory/AnnotationPropertyNameCollectionFactoryTest.php +++ b/tests/Metadata/Property/Factory/AnnotationPropertyNameCollectionFactoryTest.php @@ -17,6 +17,7 @@ use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; use ApiPlatform\Core\Metadata\Property\PropertyNameCollection; use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy; +use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\UpperCaseIdentifierDummy; use Doctrine\Common\Annotations\Reader; use Prophecy\Argument; use Prophecy\Prophecy\ProphecyInterface; @@ -60,6 +61,36 @@ public function getDependencies() ]; } + /** + * @dataProvider getUpperCaseDependencies + */ + public function testUpperCaseCreate(ProphecyInterface $decorated = null, array $results) + { + $reader = $this->prophesize(Reader::class); + $reader->getPropertyAnnotation(new \ReflectionProperty(UpperCaseIdentifierDummy::class, 'name'), ApiProperty::class)->willReturn(new ApiProperty())->shouldBeCalled(); + $reader->getPropertyAnnotation(new \ReflectionProperty(UpperCaseIdentifierDummy::class, 'Uuid'), ApiProperty::class)->willReturn(new ApiProperty())->shouldBeCalled(); + $reader->getPropertyAnnotation(Argument::type(\ReflectionProperty::class), ApiProperty::class)->willReturn(null)->shouldBeCalled(); + $reader->getMethodAnnotation(new \ReflectionMethod(UpperCaseIdentifierDummy::class, 'getName'), ApiProperty::class)->willReturn(new ApiProperty())->shouldBeCalled(); + $reader->getMethodAnnotation(new \ReflectionMethod(UpperCaseIdentifierDummy::class, 'getUuid'), ApiProperty::class)->willReturn(new ApiProperty())->shouldBeCalled(); + $reader->getMethodAnnotation(Argument::type(\ReflectionMethod::class), ApiProperty::class)->willReturn(null)->shouldBeCalled(); + + $factory = new AnnotationPropertyNameCollectionFactory($reader->reveal(), $decorated ? $decorated->reveal() : null); + $metadata = $factory->create(UpperCaseIdentifierDummy::class, []); + + $this->assertEquals($results, iterator_to_array($metadata)); + } + + public function getUpperCaseDependencies() + { + $decoratedThrowsNotFound = $this->prophesize(PropertyNameCollectionFactoryInterface::class); + $decoratedThrowsNotFound->create(UpperCaseIdentifierDummy::class, [])->willThrow(new ResourceClassNotFoundException())->shouldBeCalled(); + + return [ + [null, ['Uuid', 'name']], + [$decoratedThrowsNotFound, ['Uuid', 'name']], + ]; + } + /** * @expectedException \ApiPlatform\Core\Exception\ResourceClassNotFoundException * @expectedExceptionMessage The resource class "\DoNotExist" does not exist.