Skip to content

Commit ccdbb5f

Browse files
authored
Merge pull request #3611 from soyuka/fix/identifier-encode
Fix/identifier encode
2 parents dabbcb6 + acfcd13 commit ccdbb5f

File tree

7 files changed

+135
-2
lines changed

7 files changed

+135
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* OpenAPI: Add PHP default values to the documentation (#2386)
1414
* Deprecate using a validation groups generator service not implementing `ApiPlatform\Core\Bridge\Symfony\Validator\ValidationGroupsGeneratorInterface` (#3346)
1515
* Subresources: subresource resourceClass can now be defined as a container parameter in XML and Yaml definitions
16+
* IriConverter: Fix IRI url double encoding - may cause breaking change as some characters no longer encoded in output (#3552)
1617

1718
## 2.5.6
1819

features/bootstrap/DoctrineContext.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\SoMany as SoManyDocument;
6868
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Taxon as TaxonDocument;
6969
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\ThirdLevel as ThirdLevelDocument;
70+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\UrlEncodedId as UrlEncodedIdDocument;
7071
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\User as UserDocument;
7172
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Address;
7273
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Answer;
@@ -129,6 +130,7 @@
129130
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\SoMany;
130131
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Taxon;
131132
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\ThirdLevel;
133+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\UrlEncodedId;
132134
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\User;
133135
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\UuidIdentifierDummy;
134136
use Behat\Behat\Context\Context;
@@ -1055,6 +1057,17 @@ public function thereIsAnAnswerToTheQuestion(string $a, string $q)
10551057
$this->manager->clear();
10561058
}
10571059

1060+
/**
1061+
* @Given there is a UrlEncodedId resource
1062+
*/
1063+
public function thereIsAUrlEncodedIdResource()
1064+
{
1065+
$urlEncodedIdResource = ($this->isOrm() ? new UrlEncodedId() : new UrlEncodedIdDocument());
1066+
$this->manager->persist($urlEncodedIdResource);
1067+
$this->manager->flush();
1068+
$this->manager->clear();
1069+
}
1070+
10581071
/**
10591072
* @Then the password :password for user :user should be hashed
10601073
*/

features/main/table_inheritance.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ Feature: Table inheritance
538538
},
539539
"@id": {
540540
"type": "string",
541-
"pattern": "^/resource_interfaces/single%2520item$"
541+
"pattern": "^/resource_interfaces/single%20item$"
542542
},
543543
"@type": {
544544
"type": "string",

features/main/url_encoded_id.feature

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Feature: Allowing resource identifiers with characters that should be URL encoded
2+
In order to have a resource with an id with special characters
3+
As a client software developer
4+
I need to be able to set and retrieve these resources with the URL encoded ID
5+
6+
@createSchema
7+
Scenario Outline: Get a resource whether or not the id is URL encoded
8+
Given there is a UrlEncodedId resource
9+
And I add "Content-Type" header equal to "application/ld+json"
10+
When I send a "GET" request to "<url>"
11+
Then the response status code should be 200
12+
And the JSON should be equal to:
13+
"""
14+
{
15+
"@context": "/contexts/UrlEncodedId",
16+
"@id": "/url_encoded_ids/%25encode:id",
17+
"@type": "UrlEncodedId",
18+
"id": "%encode:id"
19+
}
20+
"""
21+
Examples:
22+
| url |
23+
| /url_encoded_ids/%encode:id |
24+
| /url_encoded_ids/%25encode%3Aid |
25+
| /url_encoded_ids/%25encode:id |
26+
| /url_encoded_ids/%encode%3Aid |

src/Bridge/Symfony/Routing/IriConverter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ private function generateIdentifiersUrl(array $identifiers, string $resourceClas
182182
}
183183

184184
if (1 === \count($identifiers)) {
185-
return [rawurlencode((string) reset($identifiers))];
185+
return [(string) reset($identifiers)];
186186
}
187187

188188
foreach ($identifiers as $name => $value) {
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+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Document;
15+
16+
use ApiPlatform\Core\Annotation\ApiResource;
17+
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
18+
19+
/**
20+
* @author Daniel West <[email protected]>
21+
*
22+
* Resource with an ID that will be URL encoded
23+
*
24+
* @ODM\Document
25+
*
26+
* @ApiResource(
27+
* itemOperations={
28+
* "get"={
29+
* "method"="GET",
30+
* "requirements"={"id"=".+"}
31+
* }
32+
* }
33+
* )
34+
*/
35+
class UrlEncodedId
36+
{
37+
/**
38+
* @ODM\Id(strategy="none")
39+
*/
40+
private $id = '%encode:id';
41+
42+
public function getId()
43+
{
44+
return $this->id;
45+
}
46+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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\Tests\Fixtures\TestBundle\Entity;
15+
16+
use ApiPlatform\Core\Annotation\ApiResource;
17+
use Doctrine\ORM\Mapping as ORM;
18+
19+
/**
20+
* @author Daniel West <[email protected]>
21+
*
22+
* Resource with an ID that will be URL encoded
23+
*
24+
* @ORM\Entity
25+
*
26+
* @ApiResource(
27+
* itemOperations={
28+
* "get"={
29+
* "method"="GET",
30+
* "requirements"={"id"=".+"}
31+
* }
32+
* }
33+
* )
34+
*/
35+
class UrlEncodedId
36+
{
37+
/**
38+
* @ORM\Column(type="string")
39+
* @ORM\Id
40+
*/
41+
private $id = '%encode:id';
42+
43+
public function getId()
44+
{
45+
return $this->id;
46+
}
47+
}

0 commit comments

Comments
 (0)