Skip to content

Commit a6c0cb3

Browse files
authored
Merge pull request #1938 from antograssiot/jsonapi-related-resources
[JSON-API] Add initial spec compliance on "Inclusion of Related Resou…
2 parents 7a680f1 + 5020e45 commit a6c0cb3

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Feature: JSON API Inclusion of Related Resources
2+
In order to be able to handle inclusion of related resources
3+
As a client software developer
4+
I need to be able to specify include parameters according to JSON API recommendation
5+
6+
Background:
7+
Given I add "Accept" header equal to "application/vnd.api+json"
8+
And I add "Content-Type" header equal to "application/vnd.api+json"
9+
10+
@createSchema
11+
@dropSchema
12+
Scenario: Request inclusion of a related resources on collection
13+
Given there are 3 dummy objects
14+
When I send a "GET" request to "/dummies/1?include=foo"
15+
Then the response status code should be 400
16+
And the response should be in JSON
17+

src/Bridge/Symfony/Bundle/Resources/config/jsonapi.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
</service>
5858

5959
<!-- Event listener -->
60+
<service id="api_platform.jsonapi.listener.request.include_parameters" class="ApiPlatform\Core\JsonApi\EventListener\IncludeParametersListener">
61+
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="5" />
62+
</service>
6063

6164
<service id="api_platform.jsonapi.listener.request.transform_pagination_parameters" class="ApiPlatform\Core\JsonApi\EventListener\TransformPaginationParametersListener">
6265
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" priority="5" />
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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\JsonApi\EventListener;
15+
16+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
17+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
18+
19+
/**
20+
* @internal
21+
*
22+
* @see http://jsonapi.org/format/#fetching-includes
23+
*/
24+
final class IncludeParametersListener
25+
{
26+
public function onKernelRequest(GetResponseEvent $event)
27+
{
28+
$request = $event->getRequest();
29+
30+
if (
31+
'jsonapi' !== $request->getRequestFormat() ||
32+
!$request->attributes->get('_api_resource_class') ||
33+
!$request->query->get('include')
34+
) {
35+
return;
36+
}
37+
38+
throw new BadRequestHttpException('Inclusion of related resources is not supported yet.');
39+
}
40+
}

0 commit comments

Comments
 (0)