Skip to content

Commit 1af3cbc

Browse files
authored
Merge pull request #1101 from soyuka/fix/stringifyable-identifier
fix stringifyable identifier
2 parents d696aa5 + 4c1dace commit 1af3cbc

File tree

5 files changed

+121
-4
lines changed

5 files changed

+121
-4
lines changed

features/main/uuid.feature

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,24 @@ Feature: Using uuid identifier on resource
7878
}
7979
"""
8080

81+
Scenario: Create a resource with custom id generator
82+
When I add "Content-Type" header equal to "application/ld+json"
83+
And I send a "POST" request to "/custom_generated_identifiers" with body:
84+
"""
85+
{}
86+
"""
87+
Then the response status code should be 201
88+
And the response should be in JSON
89+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
90+
And the JSON should be equal to:
91+
"""
92+
{
93+
"@context": "/contexts/CustomGeneratedIdentifier",
94+
"@id": "/custom_generated_identifiers/foo",
95+
"@type": "CustomGeneratedIdentifier",
96+
"id": "foo"
97+
}
98+
"""
8199

82100
@dropSchema
83101
Scenario: Delete a resource

src/Bridge/Symfony/Routing/IriConverter.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,17 @@ private function getIdentifiersFromItem($item): array
122122
continue;
123123
}
124124

125-
$identifiers[$propertyName] = $this->propertyAccessor->getValue($item, $propertyName);
125+
$identifier = $identifiers[$propertyName] = $this->propertyAccessor->getValue($item, $propertyName);
126126

127-
if (!is_object($identifiers[$propertyName])) {
127+
if (!is_object($identifier)) {
128+
continue;
129+
} elseif (method_exists($identifier, '__toString')) {
130+
$identifiers[$propertyName] = (string) $identifier;
128131
continue;
129132
}
130133

131-
$relatedResourceClass = $this->getObjectClass($identifiers[$propertyName]);
132-
$relatedItem = $identifiers[$propertyName];
134+
$relatedResourceClass = $this->getObjectClass($identifier);
135+
$relatedItem = $identifier;
133136

134137
unset($identifiers[$propertyName]);
135138

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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\Fixtures\TestBundle\Doctrine\Generator;
15+
16+
class Uuid
17+
{
18+
private $id;
19+
20+
public function __construct()
21+
{
22+
$this->id = 'foo';
23+
}
24+
25+
public function __toString()
26+
{
27+
return $this->id;
28+
}
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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\Fixtures\TestBundle\Doctrine\Generator;
15+
16+
use Doctrine\ORM\EntityManager;
17+
use Doctrine\ORM\Id\AbstractIdGenerator;
18+
19+
class UuidGenerator extends AbstractIdGenerator
20+
{
21+
public function generate(EntityManager $em, $entity): Uuid
22+
{
23+
return new Uuid();
24+
}
25+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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\Fixtures\TestBundle\Entity;
15+
16+
use ApiPlatform\Core\Annotation\ApiResource;
17+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Doctrine\Generator\Uuid;
18+
use Doctrine\ORM\Mapping as ORM;
19+
20+
/**
21+
* Custom identifier.
22+
*
23+
* @ApiResource
24+
* @ORM\Entity
25+
*/
26+
class CustomGeneratedIdentifier
27+
{
28+
/**
29+
* @var Uuid
30+
*
31+
* @ORM\Id
32+
* @ORM\Column(type="string")
33+
* @ORM\GeneratedValue(strategy="CUSTOM")
34+
* @ORM\CustomIdGenerator(class="ApiPlatform\Core\Tests\Fixtures\TestBundle\Doctrine\Generator\UuidGenerator")
35+
*/
36+
private $id;
37+
38+
public function getId()
39+
{
40+
return (string) $this->id;
41+
}
42+
}

0 commit comments

Comments
 (0)