File tree Expand file tree Collapse file tree 5 files changed +121
-4
lines changed
src/Bridge/Symfony/Routing
tests/Fixtures/TestBundle Expand file tree Collapse file tree 5 files changed +121
-4
lines changed Original file line number Diff line number Diff line change @@ -78,6 +78,24 @@ Feature: Using uuid identifier on resource
78
78
}
79
79
"""
80
80
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
+ """
81
99
82
100
@dropSchema
83
101
Scenario : Delete a resource
Original file line number Diff line number Diff line change @@ -122,14 +122,17 @@ private function getIdentifiersFromItem($item): array
122
122
continue ;
123
123
}
124
124
125
- $ identifiers [$ propertyName ] = $ this ->propertyAccessor ->getValue ($ item , $ propertyName );
125
+ $ identifier = $ identifiers [$ propertyName ] = $ this ->propertyAccessor ->getValue ($ item , $ propertyName );
126
126
127
- if (!is_object ($ identifiers [$ propertyName ])) {
127
+ if (!is_object ($ identifier )) {
128
+ continue ;
129
+ } elseif (method_exists ($ identifier , '__toString ' )) {
130
+ $ identifiers [$ propertyName ] = (string ) $ identifier ;
128
131
continue ;
129
132
}
130
133
131
- $ relatedResourceClass = $ this ->getObjectClass ($ identifiers [ $ propertyName ] );
132
- $ relatedItem = $ identifiers [ $ propertyName ] ;
134
+ $ relatedResourceClass = $ this ->getObjectClass ($ identifier );
135
+ $ relatedItem = $ identifier ;
133
136
134
137
unset($ identifiers [$ propertyName ]);
135
138
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments