Skip to content

Commit 255b22f

Browse files
NikoGranoalanpoulain
authored andcommitted
Throw exception if cursor value is set, but empty. (#3360)
1 parent 24fa80c commit 255b22f

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

CHANGELOG.md

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

3+
## 2.5.5
4+
5+
* GraphQL: Do not allow empty cursor values on `before` or `after`
6+
37
## 2.5.4
48

59
* Add a local cache in `ResourceClassResolver::getResourceClass()`

src/GraphQl/Resolver/Stage/SerializeStage.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,17 @@ private function serializePaginatedCollection(iterable $collection, array $norma
126126
$nbPageItems = $collection->count();
127127
if (isset($args['after'])) {
128128
$after = base64_decode($args['after'], true);
129-
if (false === $after) {
130-
throw Error::createLocatedError(sprintf('Cursor %s is invalid', $args['after']), $info->fieldNodes, $info->path);
129+
if (false === $after || '' === $args['after']) {
130+
$msg = '' === $args['after'] ? 'Empty cursor is invalid' : sprintf('Cursor %s is invalid', $args['after']);
131+
throw Error::createLocatedError($msg, $info->fieldNodes, $info->path);
131132
}
132133
$offset = 1 + (int) $after;
133134
}
134135
if (isset($args['before'])) {
135136
$before = base64_decode($args['before'], true);
136-
if (false === $before) {
137-
throw Error::createLocatedError(sprintf('Cursor %s is invalid', $args['before']), $info->fieldNodes, $info->path);
137+
if (false === $before || '' === $args['before']) {
138+
$msg = '' === $args['before'] ? 'Empty cursor is invalid' : sprintf('Cursor %s is invalid', $args['before']);
139+
throw Error::createLocatedError($msg, $info->fieldNodes, $info->path);
138140
}
139141
$offset = (int) $before - $nbPageItems;
140142
}

tests/GraphQl/Resolver/Stage/SerializeStageTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,10 @@ public function applyCollectionWithPaginationProvider(): array
145145
'paginator' => [new ArrayPaginator([new \stdClass(), new \stdClass(), new \stdClass()], 0, 2), [], ['totalCount' => 3., 'edges' => [['node' => ['normalized_item'], 'cursor' => 'MA=='], ['node' => ['normalized_item'], 'cursor' => 'MQ==']], 'pageInfo' => ['startCursor' => 'MA==', 'endCursor' => 'MQ==', 'hasNextPage' => true, 'hasPreviousPage' => false]]],
146146
'paginator with after cursor' => [new ArrayPaginator([new \stdClass(), new \stdClass(), new \stdClass()], 1, 2), ['after' => 'MA=='], ['totalCount' => 3., 'edges' => [['node' => ['normalized_item'], 'cursor' => 'MQ=='], ['node' => ['normalized_item'], 'cursor' => 'Mg==']], 'pageInfo' => ['startCursor' => 'MQ==', 'endCursor' => 'Mg==', 'hasNextPage' => false, 'hasPreviousPage' => true]]],
147147
'paginator with bad after cursor' => [new ArrayPaginator([], 0, 0), ['after' => '-'], null, Error::class, 'Cursor - is invalid'],
148+
'paginator with empty after cursor' => [new ArrayPaginator([], 0, 0), ['after' => ''], null, Error::class, 'Empty cursor is invalid'],
148149
'paginator with before cursor' => [new ArrayPaginator([new \stdClass(), new \stdClass(), new \stdClass()], 1, 1), ['before' => 'Mg=='], ['totalCount' => 3., 'edges' => [['node' => ['normalized_item'], 'cursor' => 'MQ==']], 'pageInfo' => ['startCursor' => 'MQ==', 'endCursor' => 'MQ==', 'hasNextPage' => true, 'hasPreviousPage' => true]]],
149150
'paginator with bad before cursor' => [new ArrayPaginator([], 0, 0), ['before' => '-'], null, Error::class, 'Cursor - is invalid'],
151+
'paginator with empty before cursor' => [new ArrayPaginator([], 0, 0), ['before' => ''], null, Error::class, 'Empty cursor is invalid'],
150152
'paginator with last' => [new ArrayPaginator([new \stdClass(), new \stdClass(), new \stdClass()], 1, 2), ['last' => 2], ['totalCount' => 3., 'edges' => [['node' => ['normalized_item'], 'cursor' => 'MQ=='], ['node' => ['normalized_item'], 'cursor' => 'Mg==']], 'pageInfo' => ['startCursor' => 'MQ==', 'endCursor' => 'Mg==', 'hasNextPage' => false, 'hasPreviousPage' => true]]],
151153
];
152154
}

0 commit comments

Comments
 (0)