Skip to content

Commit f628dd5

Browse files
committed
Merge branch '2.0'
2 parents b76438e + ab59da7 commit f628dd5

File tree

10 files changed

+189
-12
lines changed

10 files changed

+189
-12
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## 2.0.3
4+
5+
* Fix a bug when handling invalid IRIs
6+
* Allow to have a property called id even in JSON-LD
7+
* Exclude static methods from AnnotationPropertyNameCollectionFactory
8+
* Improve compatibility with Symfony 2.8
9+
310
## 2.0.2
411

512
* Fix the support of the Symfony's serializer @MaxDepth annotation

features/hydra/error.feature

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Feature: Error handling
4343
And the response should be in JSON
4444
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
4545
And the JSON node "@context" should be equal to "/contexts/Error"
46-
And the JSON node "@type" should be equal to "Error"
46+
And the JSON node "@type" should be equal to "hydra:Error"
4747
And the JSON node "hydra:title" should be equal to "An error occurred"
4848
And the JSON node "hydra:description" should be equal to 'Nested documents for attribute "relatedDummy" are not allowed. Use IRIs instead.'
4949
And the JSON node "trace" should exist
@@ -63,7 +63,7 @@ Feature: Error handling
6363
And the response should be in JSON
6464
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
6565
And the JSON node "@context" should be equal to "/contexts/Error"
66-
And the JSON node "@type" should be equal to "Error"
66+
And the JSON node "@type" should be equal to "hydra:Error"
6767
And the JSON node "hydra:title" should be equal to "An error occurred"
6868
And the JSON node "hydra:description" should be equal to 'Nested documents for attribute "relatedDummies" are not allowed. Use IRIs instead.'
6969
And the JSON node "trace" should exist
@@ -80,7 +80,7 @@ Feature: Error handling
8080
And the response should be in JSON
8181
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
8282
And the JSON node "@context" should be equal to "/contexts/Error"
83-
And the JSON node "@type" should be equal to "Error"
83+
And the JSON node "@type" should be equal to "hydra:Error"
8484
And the JSON node "hydra:title" should be equal to "An error occurred"
8585
And the JSON node "hydra:description" should exist
8686
And the JSON node "trace" should exist
@@ -98,7 +98,7 @@ Feature: Error handling
9898
And the response should be in JSON
9999
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
100100
And the JSON node "@context" should be equal to "/contexts/Error"
101-
And the JSON node "@type" should be equal to "Error"
101+
And the JSON node "@type" should be equal to "hydra:Error"
102102
And the JSON node "hydra:title" should be equal to "An error occurred"
103103
And the JSON node "hydra:description" should be equal to "Update is not allowed for this operation."
104104
And the JSON node "trace" should exist
@@ -119,7 +119,7 @@ Feature: Error handling
119119
And the response should be in JSON
120120
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
121121
And the JSON node "@context" should be equal to "/contexts/Error"
122-
And the JSON node "@type" should be equal to "Error"
122+
And the JSON node "@type" should be equal to "hydra:Error"
123123
And the JSON node "hydra:title" should be equal to "An error occurred"
124124
And the JSON node "hydra:description" should be equal to "Update is not allowed for this operation."
125125
And the JSON node "trace" should exist

features/main/non_resource.feature

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Feature: Non-resources handling
2+
In order to handle use non-resource types
3+
As a developer
4+
I should be able serialize types not mapped to an API resource.
5+
6+
Scenario: Get a resource containing a raw object
7+
When I send a "GET" request to "/contain_non_resources/1"
8+
Then the JSON should be equal to:
9+
"""
10+
{
11+
"@context": "/contexts/ContainNonResource",
12+
"@id": "/contain_non_resources/1",
13+
"@type": "ContainNonResource",
14+
"id": "1",
15+
"nested": {
16+
"@id": "/contain_non_resources/1-nested",
17+
"@type": "ContainNonResource",
18+
"id": "1-nested",
19+
"nested": null,
20+
"notAResource": {
21+
"foo": "f2",
22+
"bar": "b2"
23+
}
24+
},
25+
"notAResource": {
26+
"foo": "f1",
27+
"bar": "b1"
28+
}
29+
}
30+
"""

features/security/strong_typing.feature

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Feature: Handle properly invalid data submitted to the API
5151
And the response should be in JSON
5252
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
5353
And the JSON node "@context" should be equal to "/contexts/Error"
54-
And the JSON node "@type" should be equal to "Error"
54+
And the JSON node "@type" should be equal to "hydra:Error"
5555
And the JSON node "hydra:title" should be equal to "An error occurred"
5656
And the JSON node "hydra:description" should be equal to 'Expected IRI or nested document for attribute "relatedDummy", "string" given.'
5757
And the JSON node "trace" should exist
@@ -82,7 +82,7 @@ Feature: Handle properly invalid data submitted to the API
8282
And the response should be in JSON
8383
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
8484
And the JSON node "@context" should be equal to "/contexts/Error"
85-
And the JSON node "@type" should be equal to "Error"
85+
And the JSON node "@type" should be equal to "hydra:Error"
8686
And the JSON node "hydra:title" should be equal to "An error occurred"
8787
And the JSON node "hydra:description" should be equal to 'The type of the "relatedDummies" attribute must be "array", "string" given.'
8888
And the JSON node "trace" should exist
@@ -100,7 +100,7 @@ Feature: Handle properly invalid data submitted to the API
100100
And the response should be in JSON
101101
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
102102
And the JSON node "@context" should be equal to "/contexts/Error"
103-
And the JSON node "@type" should be equal to "Error"
103+
And the JSON node "@type" should be equal to "hydra:Error"
104104
And the JSON node "hydra:title" should be equal to "An error occurred"
105105
And the JSON node "hydra:description" should be equal to 'The type of the key "a" must be "int", "string" given.'
106106

@@ -116,7 +116,7 @@ Feature: Handle properly invalid data submitted to the API
116116
And the response should be in JSON
117117
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
118118
And the JSON node "@context" should be equal to "/contexts/Error"
119-
And the JSON node "@type" should be equal to "Error"
119+
And the JSON node "@type" should be equal to "hydra:Error"
120120
And the JSON node "hydra:title" should be equal to "An error occurred"
121121
And the JSON node "hydra:description" should be equal to 'The type of the "name" attribute must be "string", "integer" given.'
122122

src/Hydra/Serializer/ErrorNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function normalize($object, $format = null, array $context = [])
4646

4747
$data = [
4848
'@context' => $this->urlGenerator->generate('api_jsonld_context', ['shortName' => 'Error']),
49-
'@type' => 'Error',
49+
'@type' => 'hydra:Error',
5050
'hydra:title' => $context['title'] ?? 'An error occurred',
5151
'hydra:description' => $message ?? (string) $object,
5252
];

tests/Fixtures/NotAResource.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
namespace ApiPlatform\Core\Tests\Fixtures;
13+
14+
/**
15+
* This class is mapped as an API resource.
16+
*
17+
* @author Kévin Dunglas <[email protected]>
18+
*/
19+
class NotAResource
20+
{
21+
private $foo;
22+
private $bar;
23+
24+
public function __construct($foo, $bar)
25+
{
26+
$this->foo = $foo;
27+
$this->bar = $bar;
28+
}
29+
30+
/**
31+
* @return mixed
32+
*/
33+
public function getFoo()
34+
{
35+
return $this->foo;
36+
}
37+
38+
/**
39+
* @return mixed
40+
*/
41+
public function getBar()
42+
{
43+
return $this->bar;
44+
}
45+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\DataProvider;
13+
14+
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
15+
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
16+
use ApiPlatform\Core\Tests\Fixtures\NotAResource;
17+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\ContainNonResource;
18+
19+
/**
20+
* @author Kévin Dunglas <[email protected]>
21+
*/
22+
class ContainNonResourceItemDataProvider implements ItemDataProviderInterface
23+
{
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
28+
{
29+
if (ContainNonResource::class !== $resourceClass) {
30+
throw new ResourceClassNotSupportedException();
31+
}
32+
33+
// Retrieve the blog post item from somewhere
34+
$cnr = new ContainNonResource();
35+
$cnr->id = $id;
36+
$cnr->notAResource = new NotAResource('f1', 'b1');
37+
$cnr->nested = new ContainNonResource();
38+
$cnr->nested->id = "$id-nested";
39+
$cnr->nested->notAResource = new NotAResource('f2', 'b2');
40+
41+
return $cnr;
42+
}
43+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity;
13+
14+
use ApiPlatform\Core\Annotation\ApiResource;
15+
use ApiPlatform\Core\Tests\Fixtures\NotAResource;
16+
use Doctrine\ORM\Mapping as ORM;
17+
18+
/**
19+
* Resource linked to a standard object.
20+
*
21+
* @ORM\Entity
22+
* @ApiResource
23+
*
24+
* @author Kévin Dunglas <[email protected]>
25+
*/
26+
class ContainNonResource
27+
{
28+
/**
29+
* @var int
30+
*
31+
* @ORM\Column(type="integer")
32+
* @ORM\Id
33+
* @ORM\GeneratedValue(strategy="AUTO")
34+
*/
35+
public $id;
36+
37+
/**
38+
* @var self
39+
*/
40+
public $nested;
41+
42+
/**
43+
* @var NotAResource
44+
*/
45+
public $notAResource;
46+
}

tests/Fixtures/app/config/config.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ nelmio_api_doc:
6969
json: 'application/json'
7070

7171
services:
72+
contain_non_resource.item_data_provider:
73+
class: 'ApiPlatform\Core\Tests\Fixtures\TestBundle\DataProvider\ContainNonResourceItemDataProvider'
74+
public: false
75+
tags:
76+
- { name: 'api_platform.item_data_provider' }
77+
7278
app.user_manager:
7379
class: 'ApiPlatform\Core\Tests\Fixtures\TestBundle\Manager\UserManager'
7480
arguments:

tests/Hydra/Serializer/ErrorNormalizerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function testNormalize()
4545
$this->assertEquals(
4646
[
4747
'@context' => '/context/foo',
48-
'@type' => 'Error',
48+
'@type' => 'hydra:Error',
4949
'hydra:title' => 'An error occurred',
5050
'hydra:description' => 'Hello',
5151
],
@@ -54,7 +54,7 @@ public function testNormalize()
5454
$this->assertEquals(
5555
[
5656
'@context' => '/context/foo',
57-
'@type' => 'Error',
57+
'@type' => 'hydra:Error',
5858
'hydra:title' => 'Hi',
5959
'hydra:description' => 'Hello',
6060
],

0 commit comments

Comments
 (0)