Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 210 additions & 0 deletions features/hal.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
Feature: HAL support
In order to use the HAL hypermedia format
As a client software developer
I need to be able to retrieve valid HAL responses.

@createSchema
Scenario: Create a third level
When I send a "POST" request to "/third_levels" with body:
"""
{"level": 3}
"""
Then the response status code should be 201

Scenario: Create a related dummy
When I send a "POST" request to "/related_dummies" with body:
"""
{"thirdLevel": "/third_levels/1"}
"""
Then the response status code should be 201

Scenario: Create a dummy with relations
When I send a "POST" request to "/dummies" with body:
"""
{
"name": "Dummy with relations",
"dummyDate": "2015-03-01T10:00:00+00:00",
"relatedDummy": "http://example.com/related_dummies/1",
"relatedDummies": [
"/related_dummies/1"
]
}
"""
Then the response status code should be 201

Scenario: Get a resource with relations
When I add "Accept" header equal to "application/hal+json"
And I send a "GET" request to "/dummies/1"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/hal+json"
And the JSON should be equal to:
"""
{
"_links": {
"self": {
"href": "/dummies/1"
},
"relatedDummy": {
"href": "/related_dummies/1"
},
"relatedDummies": [
{
"href": "/related_dummies/1"
}
]
},
"description": null,
"dummy": null,
"dummyBoolean": null,
"dummyDate": "2015-03-01T10:00:00+00:00",
"dummyPrice": null,
"jsonData": [],
"name_converted": null,
"name": "Dummy with relations",
"alias": null
}
"""

Scenario: Update a resource
When I add "Accept" header equal to "application/hal+json"
And I send a "PUT" request to "/dummies/1" with body:
"""
{
"name": "A nice dummy"
}
"""
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/hal+json"
And the JSON should be equal to:
"""
{
"_links": {
"self": {
"href": "/dummies/1"
},
"relatedDummy": {
"href": "/related_dummies/1"
},
"relatedDummies": [
{
"href": "/related_dummies/1"
}
]
},
"description": null,
"dummy": null,
"dummyBoolean": null,
"dummyDate": "2015-03-01T10:00:00+00:00",
"dummyPrice": null,
"jsonData": [],
"name_converted": null,
"name": "A nice dummy",
"alias": null
}
"""

Scenario: Embed a relation in a parent object
When I send a "POST" request to "/relation_embedders" with body:
"""
{
"related": "/related_dummies/1"
}
"""
Then the response status code should be 201

Scenario: Get the object with the embedded relation
When I add "Accept" header equal to "application/hal+json"
And I send a "GET" request to "/relation_embedders/1"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/hal+json"
And the JSON should be equal to:
"""
{
"_links": {
"self": {
"href": "/relation_embedders/1"
},
"related": {
"href": "/related_dummies/1"
}
},
"_embedded": {
"related": {
"_links": {
"self": {
"href": "/related_dummies/1"
},
"thirdLevel": {
"href": "/third_levels/1"
}
},
"_embedded": {
"thirdLevel": {
"_links": {
"self": {
"href": "/third_levels/1"
}
},
"level": 3
}
},
"symfony": "symfony"
}
},
"krondstadt": "Krondstadt"
}
"""

@dropSchema
Scenario: Get a collection
When I add "Accept" header equal to "application/hal+json"
And I send a "GET" request to "/dummies"
Then the response status code should be 200
And the response should be in JSON
And the header "Content-Type" should be equal to "application/hal+json"
And the JSON should be equal to:
"""
{
"_links": {
"self": "/dummies",
"item": [
{
"href": "/dummies/1"
}
]
},
"_embedded": {
"item": [
{
"_links": {
"self": {
"href": "/dummies/1"
},
"relatedDummy": {
"href": "/related_dummies/1"
},
"relatedDummies": [
{
"href": "/related_dummies/1"
}
]
},
"description": null,
"dummy": null,
"dummyBoolean": null,
"dummyDate": "2015-03-01T10:00:00+00:00",
"dummyPrice": null,
"jsonData": [],
"name_converted": null,
"name": "A nice dummy",
"alias": null
}
]
},
"totalItems": 1,
"itemsPerPage": 3
}
"""
5 changes: 1 addition & 4 deletions features/relation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ Feature: Relations support
Scenario: Create a related dummy
When I send a "POST" request to "/related_dummies" with body:
"""
{
"thirdLevel": "/third_levels/1"
}
{"thirdLevel": "/third_levels/1"}
"""
Then the response status code should be 201
And the response should be in JSON
Expand Down Expand Up @@ -114,7 +112,6 @@ Feature: Relations support
}
"""


Scenario: Create a dummy with relations
When I send a "POST" request to "/dummies" with body:
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
* file that was distributed with this source code.
*/

namespace ApiPlatform\Core\Hydra\Action;
namespace ApiPlatform\Core\Action;

use ApiPlatform\Core\JsonLd\EntrypointBuilderInterface;

/**
* Generates the JSON-LD API entrypoint.
* Generates the API entrypoint.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,15 @@ public function load(array $configs, ContainerBuilder $container)
$container->setParameter('api_platform.enable_swagger', (string) $config['enable_swagger']);
}

$this->enableJsonLd($loader);
if (isset($formats['jsonld'])) {
$loader->load('jsonld.xml');
$loader->load('hydra.xml');
}

if (isset($formats['jsonhal'])) {
$loader->load('hal.xml');
}

$this->registerAnnotationLoaders($container);
$this->registerFileLoaders($container);

Expand All @@ -116,17 +124,6 @@ public function load(array $configs, ContainerBuilder $container)
}
}

/**
* Enables JSON-LD and Hydra support.
*
* @param XmlFileLoader $loader
*/
private function enableJsonLd(XmlFileLoader $loader)
{
$loader->load('jsonld.xml');
$loader->load('hydra.xml');
}

/**
* Registers annotations loaders.
*
Expand Down
35 changes: 35 additions & 0 deletions src/Bridge/Symfony/Bundle/Resources/config/hal.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<!-- Serializer -->
<service id="api_platform.hal.encoder" class="ApiPlatform\Core\Serializer\JsonEncoder" public="false">
<argument>jsonhal</argument>

<tag name="serializer.encoder" />
</service>

<service id="api_platform.hal.normalizer.item" class="ApiPlatform\Core\Hal\Serializer\ItemNormalizer" public="false">
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
<argument type="service" id="api_platform.metadata.property.name_collection_factory" />
<argument type="service" id="api_platform.metadata.property.metadata_factory" />
<argument type="service" id="api_platform.iri_converter" />
<argument type="service" id="api_platform.resource_class_resolver" />
<argument type="service" id="api_platform.property_accessor" />
<argument type="service" id="api_platform.name_converter" on-invalid="ignore" />

<tag name="serializer.normalizer" />
</service>

<service id="api_platform.hal.normalizer.collection" class="ApiPlatform\Core\Hal\Serializer\CollectionNormalizer" public="false">
<argument type="service" id="api_platform.resource_class_resolver" />
<argument>%api_platform.collection.pagination.page_parameter_name%</argument>

<tag name="serializer.normalizer" priority="50" />
</service>
</services>

</container>
3 changes: 1 addition & 2 deletions src/Bridge/Symfony/Bundle/Resources/config/hydra.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,9 @@
<argument type="service" id="api_platform.resource_class_resolver" />
<argument type="service" id="api_platform.filters" />
</service>

<!-- Action -->

<service id="api_platform.hydra.action.entrypoint" class="ApiPlatform\Core\Hydra\Action\EntrypointAction">
<service id="api_platform.action.entrypoint" class="ApiPlatform\Core\Action\EntrypointAction">
<argument type="service" id="api_platform.hydra.entrypoint_builder" />
</service>

Expand Down
6 changes: 3 additions & 3 deletions src/Bridge/Symfony/Bundle/Resources/config/jsonld.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>

<service id="api_platform.jsonld.context_builder" class="ApiPlatform\Core\JsonLd\ContextBuilder" public="false">
<argument type="service" id="api_platform.metadata.resource.name_collection_factory" />
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
Expand All @@ -29,7 +28,9 @@
<tag name="serializer.normalizer" />
</service>

<service id="api_platform.jsonld.encoder" class="ApiPlatform\Core\JsonLd\Serializer\JsonLdEncoder" public="false">
<service id="api_platform.jsonld.encoder" class="ApiPlatform\Core\Serializer\JsonEncoder" public="false">
<argument>jsonld</argument>

<tag name="serializer.encoder" />
</service>

Expand All @@ -40,7 +41,6 @@
<argument type="service" id="api_platform.metadata.resource.name_collection_factory" />
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
</service>

</services>

</container>
15 changes: 15 additions & 0 deletions src/Bridge/Symfony/Bundle/Resources/config/routing/hal.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" ?>

<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing
http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="api_hal_entrypoint" path="/{index}.jsonhal">
<default key="_controller">api_platform.action.entrypoint</default>
<default key="_api_respond">1</default>
<default key="index">index</default>
<requirement key="index">index</requirement>
</route>

</routes>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="api_hydra_entrypoint" path="/{index}.{_format}">
<default key="_controller">api_platform.hydra.action.entrypoint</default>
<default key="_controller">api_platform.action.entrypoint</default>
<default key="_api_respond">1</default>
<default key="_format">jsonld</default>
<default key="index">index</default>
Expand Down
Loading