Skip to content

Commit 350d050

Browse files
authored
Merge pull request #1037 from insekticid/patch-1
Allow Upper Case property names
2 parents 38d6a6d + 1576bd7 commit 350d050

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

src/Metadata/Property/Factory/AnnotationPropertyNameCollectionFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function create(string $resourceClass, array $options = []): PropertyName
7474
}
7575

7676
$propertyName = $this->reflection->getProperty($reflectionMethod->name);
77-
if (!preg_match('/^[A-Z]{2,}/', $propertyName)) {
77+
if (!$reflectionClass->hasProperty($propertyName) && !preg_match('/^[A-Z]{2,}/', $propertyName)) {
7878
$propertyName = lcfirst($propertyName);
7979
}
8080

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity;
13+
14+
use ApiPlatform\Core\Annotation\ApiResource;
15+
use Doctrine\ORM\Mapping as ORM;
16+
17+
/**
18+
* UpperCaseIdentifier dummy.
19+
*
20+
* @ApiResource
21+
* @ORM\Entity
22+
*/
23+
class UpperCaseIdentifierDummy
24+
{
25+
/**
26+
* @var string The custom identifier
27+
*
28+
* @ORM\Column(type="guid")
29+
* @ORM\Id
30+
*/
31+
private $Uuid;
32+
33+
/**
34+
* @var string The dummy name
35+
*
36+
* @ORM\Column(length=30)
37+
*/
38+
private $name;
39+
40+
public function getUuid(): string
41+
{
42+
return $this->Uuid;
43+
}
44+
45+
public function setUuid(string $Uuid)
46+
{
47+
$this->Uuid = $Uuid;
48+
}
49+
50+
public function getName(): string
51+
{
52+
return $this->name;
53+
}
54+
55+
public function setName(string $name)
56+
{
57+
$this->name = $name;
58+
}
59+
}

tests/Fixtures/TestBundle/Entity/UuidIdentifierDummy.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
/**
1818
* Custom identifier dummy.
1919
*
20+
* @author Exploit.cz <[email protected]>
21+
*
2022
* @ApiResource
2123
* @ORM\Entity
2224
*/

tests/Metadata/Property/Factory/AnnotationPropertyNameCollectionFactoryTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
1818
use ApiPlatform\Core\Metadata\Property\PropertyNameCollection;
1919
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
20+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\UpperCaseIdentifierDummy;
2021
use Doctrine\Common\Annotations\Reader;
2122
use Prophecy\Argument;
2223
use Prophecy\Prophecy\ProphecyInterface;
@@ -60,6 +61,36 @@ public function getDependencies()
6061
];
6162
}
6263

64+
/**
65+
* @dataProvider getUpperCaseDependencies
66+
*/
67+
public function testUpperCaseCreate(ProphecyInterface $decorated = null, array $results)
68+
{
69+
$reader = $this->prophesize(Reader::class);
70+
$reader->getPropertyAnnotation(new \ReflectionProperty(UpperCaseIdentifierDummy::class, 'name'), ApiProperty::class)->willReturn(new ApiProperty())->shouldBeCalled();
71+
$reader->getPropertyAnnotation(new \ReflectionProperty(UpperCaseIdentifierDummy::class, 'Uuid'), ApiProperty::class)->willReturn(new ApiProperty())->shouldBeCalled();
72+
$reader->getPropertyAnnotation(Argument::type(\ReflectionProperty::class), ApiProperty::class)->willReturn(null)->shouldBeCalled();
73+
$reader->getMethodAnnotation(new \ReflectionMethod(UpperCaseIdentifierDummy::class, 'getName'), ApiProperty::class)->willReturn(new ApiProperty())->shouldBeCalled();
74+
$reader->getMethodAnnotation(new \ReflectionMethod(UpperCaseIdentifierDummy::class, 'getUuid'), ApiProperty::class)->willReturn(new ApiProperty())->shouldBeCalled();
75+
$reader->getMethodAnnotation(Argument::type(\ReflectionMethod::class), ApiProperty::class)->willReturn(null)->shouldBeCalled();
76+
77+
$factory = new AnnotationPropertyNameCollectionFactory($reader->reveal(), $decorated ? $decorated->reveal() : null);
78+
$metadata = $factory->create(UpperCaseIdentifierDummy::class, []);
79+
80+
$this->assertEquals($results, iterator_to_array($metadata));
81+
}
82+
83+
public function getUpperCaseDependencies()
84+
{
85+
$decoratedThrowsNotFound = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
86+
$decoratedThrowsNotFound->create(UpperCaseIdentifierDummy::class, [])->willThrow(new ResourceClassNotFoundException())->shouldBeCalled();
87+
88+
return [
89+
[null, ['Uuid', 'name']],
90+
[$decoratedThrowsNotFound, ['Uuid', 'name']],
91+
];
92+
}
93+
6394
/**
6495
* @expectedException \ApiPlatform\Core\Exception\ResourceClassNotFoundException
6596
* @expectedExceptionMessage The resource class "\DoNotExist" does not exist.

0 commit comments

Comments
 (0)