Skip to content

Commit

Permalink
CollectionView: Prevent URL escaping when listing pagination URLs (#154)
Browse files Browse the repository at this point in the history
* Prevent URL escaping when listing pagination URLs

* Fix warnings / errors in pipeline
  • Loading branch information
cnizzardini authored Mar 25, 2024
1 parent e56742b commit 25e980d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion config/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export DEBUG="true"
export APP_ENCODING="UTF-8"
export APP_DEFAULT_LOCALE="en_US"
export APP_DEFAULT_TIMEZONE="UTC"
export SECURITY_SALT="__SALT__"
export SECURITY_SALT="bBpSESU8O0gVTzr8Lk9LzJGcy1uHYhah"

# Uncomment these to define cache configuration via environment variables.
#export CACHE_DURATION="+2 minutes"
Expand Down
30 changes: 30 additions & 0 deletions plugins/collection-view/src/View/Helper/PagninatorHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);

namespace MixerApi\CollectionView\View\Helper;

use Cake\View\Helper\PaginatorHelper;

class PagninatorHelper extends PaginatorHelper
{
/**
* Overwrite base method to never escape URLs.
*
* @param array<string, mixed> $options Pagination options.
* @param array $url URL.
* @param array<string, mixed> $urlOptions Array of options
* @return string By default, returns a full pagination URL string for use in non-standard contexts (i.e. JavaScript)
*/
public function generateUrl(
array $options = [],
array $url = [],
array $urlOptions = []
): string {
$urlOptions += [
'escape' => false,
'fullBase' => false,
];

return $this->Url->build($this->generateUrlParams($options, $url), $urlOptions);
}
}
2 changes: 2 additions & 0 deletions plugins/collection-view/src/View/JsonCollectionView.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Cake\Core\Configure;
use Cake\View\JsonView;
use MixerApi\CollectionView\Serializer;
use MixerApi\CollectionView\View\Helper\PagninatorHelper;

class JsonCollectionView extends JsonView
{
Expand Down Expand Up @@ -35,6 +36,7 @@ public function initialize(): void
parent::initialize();
$this->loadHelper('Paginator', [
'templates' => 'MixerApi/CollectionView.paginator-template',
'className' => PagninatorHelper::class,
]);
}

Expand Down
2 changes: 2 additions & 0 deletions plugins/collection-view/src/View/XmlCollectionView.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Cake\Core\Configure;
use Cake\View\SerializedView;
use MixerApi\CollectionView\Serializer;
use MixerApi\CollectionView\View\Helper\PagninatorHelper;

class XmlCollectionView extends SerializedView
{
Expand Down Expand Up @@ -65,6 +66,7 @@ public function initialize(): void
parent::initialize();
$this->loadHelper('Paginator', [
'templates' => 'MixerApi/CollectionView.paginator-template',
'className' => PagninatorHelper::class,
]);
}

Expand Down
3 changes: 2 additions & 1 deletion plugins/collection-view/tests/TestCase/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ public function setUp(): void

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

$this->assertResponseOk();
$this->assertTrue(isset($object->collection->url));
$this->assertStringNotContainsString('&amp;', $object->collection->next);
$this->assertNotEmpty($object->data);
}

Expand Down

0 comments on commit 25e980d

Please sign in to comment.