Skip to content

Commit 141530c

Browse files
authored
Merge pull request #1072 from soyuka/masterb
Merge 2.0 in master
2 parents 3263e82 + 17b3dd6 commit 141530c

File tree

7 files changed

+379
-1
lines changed

7 files changed

+379
-1
lines changed

features/main/table_inheritance.feature

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,216 @@ Feature: Table inheritance
8080
"required": ["hydra:member"]
8181
}
8282
"""
83+
84+
@createSchema
85+
Scenario: Create a table inherited resource
86+
When I add "Content-Type" header equal to "application/ld+json"
87+
And I send a "POST" request to "/dummy_table_inheritance_children" with body:
88+
"""
89+
{"name": "foo", "nickname": "bar"}
90+
"""
91+
Then the response status code should be 201
92+
And the response should be in JSON
93+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
94+
And the JSON should be valid according to this schema:
95+
"""
96+
{
97+
"type": "object",
98+
"properties": {
99+
"@type": {
100+
"type": "string",
101+
"pattern": "^DummyTableInheritanceChild$"
102+
},
103+
"@context": {
104+
"type": "string",
105+
"pattern": "^/contexts/DummyTableInheritanceChild$"
106+
},
107+
"@id": {
108+
"type": "string",
109+
"pattern": "^/dummy_table_inheritance_children/1$"
110+
},
111+
"name": {
112+
"type": "string",
113+
"pattern": "^foo$",
114+
"required": "true"
115+
},
116+
"nickname": {
117+
"type": "string",
118+
"pattern": "^bar$",
119+
"required": "true"
120+
}
121+
}
122+
}
123+
"""
124+
125+
Scenario: Create a different table inherited resource
126+
When I add "Content-Type" header equal to "application/ld+json"
127+
And I send a "POST" request to "/dummy_table_inheritance_different_children" with body:
128+
"""
129+
{"name": "foo", "email": "bar@localhost"}
130+
"""
131+
Then the response status code should be 201
132+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
133+
And the JSON should be valid according to this schema:
134+
"""
135+
{
136+
"type": "object",
137+
"properties": {
138+
"@type": {
139+
"type": "string",
140+
"pattern": "^DummyTableInheritanceDifferentChild$"
141+
},
142+
"@context": {
143+
"type": "string",
144+
"pattern": "^/contexts/DummyTableInheritanceDifferentChild$"
145+
},
146+
"@id": {
147+
"type": "string",
148+
"pattern": "^/dummy_table_inheritance_different_children/2$"
149+
},
150+
"name": {
151+
"type": "string",
152+
"pattern": "^foo$",
153+
"required": "true"
154+
},
155+
"email": {
156+
"type": "string",
157+
"pattern": "^bar\\@localhost$",
158+
"required": "true"
159+
}
160+
}
161+
}
162+
"""
163+
164+
Scenario: Get related entity with multiple inherited children types
165+
When I add "Content-Type" header equal to "application/ld+json"
166+
And I send a "POST" request to "/dummy_table_inheritance_relateds" with body:
167+
"""
168+
{
169+
"children": [
170+
"/dummy_table_inheritance_children/1",
171+
"/dummy_table_inheritance_different_children/2"
172+
]
173+
}
174+
"""
175+
Then the response status code should be 201
176+
And the response should be in JSON
177+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
178+
And the JSON should be valid according to this schema:
179+
"""
180+
{
181+
"type": "object",
182+
"properties": {
183+
"@type": {
184+
"type": "string",
185+
"pattern": "^DummyTableInheritanceRelated$"
186+
},
187+
"@context": {
188+
"type": "string",
189+
"pattern": "^/contexts/DummyTableInheritanceRelated$"
190+
},
191+
"@id": {
192+
"type": "string",
193+
"pattern": "^/dummy_table_inheritance_relateds/1$"
194+
},
195+
"children": {
196+
"items": {
197+
"type": "object",
198+
"anyOf": [
199+
{
200+
"properties": {
201+
"@type": {
202+
"type": "string",
203+
"pattern": "^DummyTableInheritanceChild$"
204+
},
205+
"name": {
206+
"type": "string",
207+
"required": "true"
208+
},
209+
"nickname": {
210+
"type": "string",
211+
"required": "true"
212+
}
213+
}
214+
},
215+
{
216+
"properties": {
217+
"@type": {
218+
"type": "string",
219+
"pattern": "^DummyTableInheritanceDifferentChild$"
220+
},
221+
"name": {
222+
"type": "string",
223+
"required": "true"
224+
},
225+
"email": {
226+
"type": "string",
227+
"required": "true"
228+
}
229+
}
230+
}
231+
]
232+
},
233+
"minItems": 2,
234+
"maxItems": 2
235+
}
236+
}
237+
}
238+
"""
239+
240+
@dropSchema
241+
Scenario: Get the parent entity collection which contains multiple inherited children type
242+
When I send a "GET" request to "/dummy_table_inheritances"
243+
Then the response status code should be 200
244+
And the response should be in JSON
245+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
246+
And the JSON should be valid according to this schema:
247+
"""
248+
{
249+
"type": "object",
250+
"properties": {
251+
"hydra:member": {
252+
"type": "array",
253+
"items": {
254+
"type": "object",
255+
"anyOf": [
256+
{
257+
"properties": {
258+
"@type": {
259+
"type": "string",
260+
"pattern": "^DummyTableInheritanceChild$"
261+
},
262+
"name": {
263+
"type": "string",
264+
"required": "true"
265+
},
266+
"nickname": {
267+
"type": "string",
268+
"required": "true"
269+
}
270+
}
271+
},
272+
{
273+
"properties": {
274+
"@type": {
275+
"type": "string",
276+
"pattern": "^DummyTableInheritanceDifferentChild$"
277+
},
278+
"name": {
279+
"type": "string",
280+
"required": "true"
281+
},
282+
"email": {
283+
"type": "string",
284+
"required": "true"
285+
}
286+
}
287+
}
288+
]
289+
},
290+
"minItems": 2
291+
}
292+
},
293+
"required": ["hydra:member"]
294+
}
295+
"""

src/JsonLd/Serializer/ItemNormalizer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ public function normalize($object, $format = null, array $context = [])
6666
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass);
6767
$data = $this->addJsonLdContext($this->contextBuilder, $resourceClass, $context);
6868

69+
// Use resolved resource class instead of given resource class to support multiple inheritance child types
70+
$context['resource_class'] = $resourceClass;
71+
6972
$rawData = parent::normalize($object, $format, $context);
7073
if (!is_array($rawData)) {
7174
return $rawData;

tests/Fixtures/TestBundle/Entity/DummyTableInheritance.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515

1616
use ApiPlatform\Core\Annotation\ApiResource;
1717
use Doctrine\ORM\Mapping as ORM;
18+
use Symfony\Component\Serializer\Annotation\Groups;
1819

1920
/**
2021
* @ORM\Entity
2122
* @ORM\InheritanceType("JOINED")
2223
* @ORM\DiscriminatorColumn(name="discr", type="string")
23-
* @ORM\DiscriminatorMap({"dummyTableInheritance" = "DummyTableInheritance", "dummyTableInheritanceChild" = "dummyTableInheritanceChild"})
24+
* @ORM\DiscriminatorMap({"dummyTableInheritance" = "DummyTableInheritance", "dummyTableInheritanceChild" = "DummyTableInheritanceChild", "dummyTableInheritanceDifferentChild" = "DummyTableInheritanceDifferentChild"})
2425
* @ApiResource
2526
*/
2627
class DummyTableInheritance
@@ -31,16 +32,28 @@ class DummyTableInheritance
3132
* @ORM\Column(type="integer")
3233
* @ORM\Id
3334
* @ORM\GeneratedValue(strategy="AUTO")
35+
*
36+
* @Groups({"default"})
3437
*/
3538
private $id;
3639

3740
/**
3841
* @var string The dummy name
3942
*
4043
* @ORM\Column
44+
*
45+
* @Groups({"default"})
4146
*/
4247
private $name;
4348

49+
/**
50+
* @var DummyTableInheritanceRelated
51+
*
52+
* @ORM\ManyToOne(targetEntity="DummyTableInheritanceRelated", inversedBy="children")
53+
* @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
54+
*/
55+
private $parent;
56+
4457
public function getName()
4558
{
4659
return $this->name;
@@ -55,4 +68,24 @@ public function getId()
5568
{
5669
return $this->id;
5770
}
71+
72+
/**
73+
* @return DummyTableInheritanceRelated
74+
*/
75+
public function getParent()
76+
{
77+
return $this->parent;
78+
}
79+
80+
/**
81+
* @param DummyTableInheritanceRelated $parent
82+
*
83+
* @return $this
84+
*/
85+
public function setParent(DummyTableInheritanceRelated $parent)
86+
{
87+
$this->parent = $parent;
88+
89+
return $this;
90+
}
5891
}

tests/Fixtures/TestBundle/Entity/DummyTableInheritanceChild.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use ApiPlatform\Core\Annotation\ApiResource;
1717
use Doctrine\ORM\Mapping as ORM;
18+
use Symfony\Component\Serializer\Annotation\Groups;
1819

1920
/**
2021
* @ORM\Entity
@@ -26,6 +27,8 @@ class DummyTableInheritanceChild extends DummyTableInheritance
2627
* @var string The dummy nickname
2728
*
2829
* @ORM\Column
30+
*
31+
* @Groups({"default"})
2932
*/
3033
private $nickname;
3134

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 Doctrine\ORM\Mapping as ORM;
18+
use Symfony\Component\Serializer\Annotation\Groups;
19+
20+
/**
21+
* @ORM\Entity
22+
* @ApiResource
23+
*/
24+
class DummyTableInheritanceDifferentChild extends DummyTableInheritance
25+
{
26+
/**
27+
* @var string The dummy email
28+
*
29+
* @ORM\Column
30+
*
31+
* @Groups({"default"})
32+
*/
33+
private $email;
34+
35+
public function getEmail()
36+
{
37+
return $this->email;
38+
}
39+
40+
public function setEmail($email)
41+
{
42+
$this->email = $email;
43+
}
44+
}

0 commit comments

Comments
 (0)