8000 Fix subresources · api-platform/core@b3130df · GitHub
[go: up one dir, main page]

Skip to content

Commit b3130df

Browse files
soyukameyerbaptiste
authored andcommitted
Fix subresources
1 parent b6c338a commit b3130df

File tree

3 files changed

+71
-66
lines changed

3 files changed

+71
-66
lines changed

features/main/subresource.feature

Lines changed: 66 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -193,45 +193,45 @@ Feature: Subresource support
193193
}
194194
"""
195195

196-
# Scenario: Get the subresource relation item
197-
# When I send a "GET" request to "/dummies/1/related_dummies/2"
198-
# Then the response status code should be 200
199-
# And the response should be in JSON
200-
# And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
201-
# And the JSON should be equal to:
202-
# """
203-
# {
204-
# "@context": "/contexts/RelatedDummy",
205-
# "@id": "/related_dummies/2",
206-
# "@type": "https://schema.org/Product",
207-
# "id": 2,
208-
# "name": null,
209-
# "symfony": "symfony",
210-
# "dummyDate": null,
211-
# "thirdLevel": {
212-
# "@id": "/third_levels/1",
213-
# "@type": "ThirdLevel",
214-
# "fourthLevel": "/fourth_levels/1"
215-
# },
216-
# "relatedToDummyFriend": [],
217-
# "dummyBoolean": null,
218-
# "embeddedDummy": [],
219-
# "age": null
220-
# }
221-
# """
196+
Scenario: Get the subresource relation item
197+
When I send a "GET" request to "/dummies/1/related_dummies/2"
198+
Then the response status code should be 200
199+
And the response should be in JSON
200+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
201+
And the JSON should be equal to:
202+
"""
203+
{
204+
"@context": "/contexts/RelatedDummy",
205+
"@id": "/related_dummies/2",
206+
"@type": "https://schema.org/Product",
207+
"id": 2,
208+
"name": null,
209+
"symfony": "symfony",
210+
"dummyDate": null,
211+
"thirdLevel": {
212+
"@id": "/third_levels/1",
213+
"@type": "ThirdLevel",
214+
"fourthLevel": "/fourth_levels/1"
215+
},
216+
"relatedToDummyFriend": [],
217+
"dummyBoolean": null,
218+
"embeddedDummy": [],
219+
"age": null
220+
}
221+
"""
222222

223-
# Scenario: Create a dummy with a relation that is a subresource
224-
# When I add "Content-Type" header equal to "application/ld+json"
225-
# And I send a "POST" request to "/dummies" with body:
226-
# """
227-
# {
228-
# "name": "Dummy with relations",
229-
# "relatedDummy": "/dummies/1/related_dummies/2"
230-
# }
231-
# """
232-
# Then the response status code should be 201
233-
# And the response should be in JSON
234-
# And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
223+
Scenario: Create a dummy with a relation that is a subresource
224+
When I add "Content-Type" header equal to "application/ld+json"
225+
And I send a "POST" request to "/dummies" with body:
226+
"""
227+
{
228+
"name": "Dummy with relations",
229+
"relatedDummy": "/dummies/1/related_dummies/2"
230+
}
231+
"""
232+
Then the response status code should be 201
233+
And the response should be in JSON
234+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
235235

236236
Scenario: Get the embedded relation subresource item at the third level
237237
When I send a "GET" request to "/dummies/1/related_dummies/1/third_level"
@@ -344,7 +344,30 @@ Feature: Subresource support
344344
}
345345
"""
346346

347+
Scenario: Recursive resource
348+
When I send a "GET" request to "/dummy_products/2"
349+
Then the response status code should be 200
350+
And the response should be in JSON
351+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
352+
And the JSON should be equal to:
353+
"""
354+
{
355+
"@context": "/contexts/DummyProduct",
356+
"@id": "/dummy_products/2",
357+
"@type": "DummyProduct",
358+
"offers": [
359+
"/dummy_aggregate_offers/1"
360+
],
361+
"id": 2,
362+
"name": "Dummy product",
363+
"relatedProducts": [
364+
"/dummy_products/1"
365+
],
366+
"parent": null
367+
}
368+
"""
347369

370+
@createSchema
348371
Scenario: The OneToOne subresource should be accessible from owned side
349372
Given there is a RelatedOwnedDummy object with OneToOne relation
350373
When I send a "GET" request to "/related_owned_dummies/1/owning_dummy"
@@ -355,7 +378,7 @@ Feature: Subresource support
355378
"""
356379
{
357380
"@context": "/contexts/Dummy",
358-
"@id": "/dummies/2",
381+
"@id": "/dummies/1",
359382
"@type": "Dummy",
360383
"description": null,
361384
"dummy": null,
@@ -370,13 +393,14 @@ Feature: Subresource support
370393
"name_converted": null,
371394
"relatedOwnedDummy": "/related_owned_dummies/1",
372395
"relatedOwningDummy": null,
373-
"id": 2,
396+
"id": 1,
374397
"name": "plop",
375398
"alias": null,
376399
"foo": null
377400
}
378401
"""
379402

403+
@createSchema
380404
Scenario: The OneToOne subresource should be accessible from owning side
381405
Given there is a RelatedOwningDummy object with OneToOne relation
382406
When I send a "GET" request to "/related_owning_dummies/1/owned_dummy"
@@ -387,7 +411,7 @@ Feature: Subresource support
387411
"""
388412
{
389413
"@context": "/contexts/Dummy",
390-
"@id": "/dummies/3",
414+
"@id": "/dummies/1",
391415
"@type": "Dummy",
392416
"description": null,
393417
"dummy": null,
@@ -402,32 +426,9 @@ Feature: Subresource support
402426
"name_converted": null,
403427
"relatedOwnedDummy": null,
404428
"relatedOwningDummy": "/related_owning_dummies/1",
405-
"id": 3,
429+
"id": 1,
406430
"name": "plop",
407431
"alias": null,
408432
"foo": null
409433
}
410434
"""
411-
412-
Scenario: Recursive resource
413-
When I send a "GET" request to "/dummy_products/2"
414-
Then the response status code should be 200
415-
And the response should be in JSON
416-
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
417-
And the JSON should be equal to:
418-
"""
419-
{
420-
"@context": "/contexts/DummyProduct",
421-
"@id": "/dummy_products/2",
422-
"@type": "DummyProduct",
423-
"offers": [
424-
"/dummy_aggregate_offers/1"
425-
],
426-
"id": 2,
427-
"name": "Dummy product",
428-
"relatedProducts": [
429-
"/dummy_products/1"
430-
],
431-
"parent": null
432-
}
433-
"""

src/Operation/Factory/SubresourceOperationFactory.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ private function computeSubresourceOperations(string $resourceClass, array &$tre
149149
$identifiers = (array) $resourceMetadata->getAttribute('identifiers', null === $this->identifiersExtractor ? ['id'] : $this->identifiersExtractor->getIdentifiersFromResourceClass($resourceClass));
150150
$identifier = \is_string($key = array_key_first($identifiers)) ? $key : $identifiers[0];
151151
$operation['identifiers'] = $parentOperation['identifiers'];
152-
$operation['identifiers'][$parentOperation['property']] = [$resourceClass, $identifiers[$identifier][1] ?? $identifier, $isLastItem ? true : $parentOperation['collection']];
152+
153+
if (!isset($operation['identifiers'][$parentOperation['property']])) {
154+
$operation['identifiers'][$parentOperation['property']] = [$resourceClass, $identifiers[$identifier][1] ?? $identifier, $isLastItem ? true : $parentOperation['collection']];
155+
}
153156

154157
$operation['operation_name'] = str_replace(
155158
'get'.self::SUBRESOURCE_SUFFIX,

tests/Fixtures/TestBundle/Entity/RelatedDummy.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class RelatedDummy extends ParentDummy
3333
{
3434
/**
3535
* @ApiProperty(writable=false)
36+
* @ApiSubresource
3637
* @ORM\Column(type="integer")
3738
* @ORM\Id
3839
* @ORM\GeneratedValue(strategy="AUTO")

0 commit comments

Comments
 (0)
0