Skip to content

Commit 25e980d

Browse files
authored
CollectionView: Prevent URL escaping when listing pagination URLs (#154)
* Prevent URL escaping when listing pagination URLs * Fix warnings / errors in pipeline
1 parent e56742b commit 25e980d

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

config/.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export DEBUG="true"
1818
export APP_ENCODING="UTF-8"
1919
export APP_DEFAULT_LOCALE="en_US"
2020
export APP_DEFAULT_TIMEZONE="UTC"
21-
export SECURITY_SALT="__SALT__"
21+
export SECURITY_SALT="bBpSESU8O0gVTzr8Lk9LzJGcy1uHYhah"
2222

2323
# Uncomment these to define cache configuration via environment variables.
2424
#export CACHE_DURATION="+2 minutes"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace MixerApi\CollectionView\View\Helper;
5+
6+
use Cake\View\Helper\PaginatorHelper;
7+
8+
class PagninatorHelper extends PaginatorHelper
9+
{
10+
/**
11+
* Overwrite base method to never escape URLs.
12+
*
13+
* @param array<string, mixed> $options Pagination options.
14+
* @param array $url URL.
15+
* @param array<string, mixed> $urlOptions Array of options
16+
* @return string By default, returns a full pagination URL string for use in non-standard contexts (i.e. JavaScript)
17+
*/
18+
public function generateUrl(
19+
array $options = [],
20+
array $url = [],
21+
array $urlOptions = []
22+
): string {
23+
$urlOptions += [
24+
'escape' => false,
25+
'fullBase' => false,
26+
];
27+
28+
return $this->Url->build($this->generateUrlParams($options, $url), $urlOptions);
29+
}
30+
}

plugins/collection-view/src/View/JsonCollectionView.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Cake\Core\Configure;
77
use Cake\View\JsonView;
88
use MixerApi\CollectionView\Serializer;
9+
use MixerApi\CollectionView\View\Helper\PagninatorHelper;
910

1011
class JsonCollectionView extends JsonView
1112
{
@@ -35,6 +36,7 @@ public function initialize(): void
3536
parent::initialize();
3637
$this->loadHelper('Paginator', [
3738
'templates' => 'MixerApi/CollectionView.paginator-template',
39+
'className' => PagninatorHelper::class,
3840
]);
3941
}
4042

plugins/collection-view/src/View/XmlCollectionView.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Cake\Core\Configure;
77
use Cake\View\SerializedView;
88
use MixerApi\CollectionView\Serializer;
9+
use MixerApi\CollectionView\View\Helper\PagninatorHelper;
910

1011
class XmlCollectionView extends SerializedView
1112
{
@@ -65,6 +66,7 @@ public function initialize(): void
6566
parent::initialize();
6667
$this->loadHelper('Paginator', [
6768
'templates' => 'MixerApi/CollectionView.paginator-template',
69+
'className' => PagninatorHelper::class,
6870
]);
6971
}
7072

plugins/collection-view/tests/TestCase/ControllerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ public function setUp(): void
2929

3030
public function test_json(): void
3131
{
32-
$this->get('/actors.json');
32+
$this->get('/actors.json?limit=1');
3333
$body = (string)$this->_response->getBody();
3434
$object = json_decode($body);
3535

3636
$this->assertResponseOk();
3737
$this->assertTrue(isset($object->collection->url));
38+
$this->assertStringNotContainsString('&amp;', $object->collection->next);
3839
$this->assertNotEmpty($object->data);
3940
}
4041

0 commit comments

Comments
 (0)