Skip to content

Commit e17a44e

Browse files
authored
Merge pull request #2219 from antograssiot/get-item-on-collection
[2.3] Throw an InvalidArgument when trying to get Item from a collection route
2 parents 8e9fc9b + 7f6daca commit e17a44e

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

features/doctrine/search_filter.feature

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,35 @@ Feature: Search filter on collections
374374
}
375375
"""
376376

377+
@sqlite
378+
Scenario: Search for entities with an existing collection route name
379+
When I send a "GET" request to "/dummies?relatedDummies=dummy_cars"
380+
Then the response status code should be 200
381+
And the response should be in JSON
382+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
383+
And the JSON should be valid according to this schema:
384+
"""
385+
{
386+
"type": "object",
387+
"properties": {
388+
"@context": {"pattern": "^/contexts/Dummy$"},
389+
"@id": {"pattern": "^/dummies$"},
390+
"@type": {"pattern": "^hydra:Collection$"},
391+
"hydra:member": {
392+
"type": "array",
393+
"maxItems": 0
394+
},
395+
"hydra:view": {
396+
"type": "object",
397+
"properties": {
398+
"@id": {"pattern": "^/dummies\\?relatedDummies=dummy_cars"},
399+
"@type": {"pattern": "^hydra:PartialCollectionView$"}
400+
}
401+
}
402+
}
403+
}
404+
"""
405+
377406
@createSchema
378407
Scenario: Search related collection by name
379408
Given there are 3 dummy objects having each 3 relatedDummies

src/Bridge/Symfony/Routing/IriConverter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ public function getItemFromIri(string $iri, array $context = [])
7979
throw new InvalidArgumentException(sprintf('No resource associated to "%s".', $iri));
8080
}
8181

82+
if (isset($parameters['_api_collection_operation_name'])) {
83+
throw new InvalidArgumentException(sprintf('The iri "%s" references a collection not an item.', $iri));
84+
}
85+
8286
$attributes = AttributesExtractor::extractAttributes($parameters);
8387

8488
try {

tests/Bridge/Symfony/Routing/IriConverterTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ public function testGetItemFromIriNoResourceException()
6262
$converter->getItemFromIri('/users/3');
6363
}
6464

65+
public function testGetItemFromIriCollectionRouteException()
66+
{
67+
$this->expectException(InvalidArgumentException::class);
68+
$this->expectExceptionMessage('The iri "/users" references a collection not an item.');
69+
70+
$routerProphecy = $this->prophesize(RouterInterface::class);
71+
$routerProphecy->match('/users')->willReturn([
72+
'_api_resource_class' => Dummy::class,
73+
'_api_collection_operation_name' => 'get',
74+
])->shouldBeCalledTimes(1);
75+
76+
$converter = $this->getIriConverter($routerProphecy);
77+
$converter->getItemFromIri('/users');
78+
}
79+
6580
public function testGetItemFromIriItemNotFoundException()
6681
{
6782
$this->expectException(ItemNotFoundException::class);

0 commit comments

Comments
 (0)