Skip to content

Commit 6f192a8

Browse files
authored
Fix Pagination when token is an array (#1811)
1 parent 0f0016c commit 6f192a8

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

src/Generator/PaginationGenerator.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
namespace AsyncAws\CodeGenerator\Generator;
66

77
use AsyncAws\CodeGenerator\Definition\ListShape;
8+
use AsyncAws\CodeGenerator\Definition\MapShape;
89
use AsyncAws\CodeGenerator\Definition\Operation;
910
use AsyncAws\CodeGenerator\Definition\Pagination;
11+
use AsyncAws\CodeGenerator\Definition\Shape;
1012
use AsyncAws\CodeGenerator\Definition\StructureShape;
1113
use AsyncAws\CodeGenerator\Generator\CodeGenerator\TypeGenerator;
1214
use AsyncAws\CodeGenerator\Generator\Naming\NamespaceRegistry;
@@ -165,7 +167,7 @@ private function generateOutputPagination(Operation $operation, Pagination $pagi
165167
'PROPERTY_NAME' => GeneratorHelper::normalizeName($resultKey),
166168
'PAGE_LOADER_CODE' => $this->generateOutputPaginationLoader(
167169
strtr('yield from $page->PROPERTY_ACCESSOR;', ['PROPERTY_ACCESSOR' => GeneratorHelper::normalizeName($resultKey)]),
168-
$common, $moreResult, $outputToken, $pagination, $classBuilder, $operation
170+
$common, $moreResult, $outputToken, $pagination, $classBuilder, $operation, $shape
169171
),
170172
]));
171173
}
@@ -182,7 +184,7 @@ private function generateOutputPagination(Operation $operation, Pagination $pagi
182184
'PROPERTY_ACCESSOR' => 'get' . ucfirst(GeneratorHelper::normalizeName($resultKeys[0])),
183185
]);
184186
} else {
185-
$body = $this->generateOutputPaginationLoader($iteratorBody, $common, $moreResult, $outputToken, $pagination, $classBuilder, $operation);
187+
$body = $this->generateOutputPaginationLoader($iteratorBody, $common, $moreResult, $outputToken, $pagination, $classBuilder, $operation, $shape);
186188
}
187189

188190
$classBuilder->addMethod('getIterator')
@@ -197,22 +199,39 @@ private function generateOutputPagination(Operation $operation, Pagination $pagi
197199
/**
198200
* @param string[] $outputToken
199201
*/
200-
private function generateOutputPaginationLoader(string $iterator, string $common, string $moreResult, array $outputToken, Pagination $pagination, ClassBuilder $classBuilder, Operation $operation): string
202+
private function generateOutputPaginationLoader(string $iterator, string $common, string $moreResult, array $outputToken, Pagination $pagination, ClassBuilder $classBuilder, Operation $operation, StructureShape $shape): string
201203
{
202204
if (empty($outputToken)) {
203205
return strtr($iterator, ['$page->' => '$this->']);
204206
}
205207

206208
$inputToken = $pagination->getInputToken();
209+
$moreCondition = '';
210+
$moreShape = null;
207211
if (!$moreResult) {
208-
$moreCondition = '';
209212
foreach ($outputToken as $property) {
210-
$moreCondition .= 'null !== ' . $this->generateGetter('$page', $property, (bool) $common);
213+
$moreResult = $property;
214+
$moreShape = $shape->getMember($moreResult)->getShape();
211215

212216
break;
213217
}
214218
} else {
215-
$moreCondition = $this->generateGetter('$page', $moreResult, (bool) $common);
219+
if ($common) {
220+
$shape = $shape->getMember($common)->getShape();
221+
}
222+
if ($shape instanceof StructureShape) {
223+
$moreShape = $shape->getMember($moreResult)->getShape();
224+
}
225+
}
226+
227+
if ($moreResult) {
228+
if ($moreShape instanceof ListShape || $moreShape instanceof MapShape) {
229+
$moreCondition .= '[] !== ' . $this->generateGetter('$page', $moreResult, (bool) $common);
230+
} elseif ($moreShape instanceof Shape && 'boolean' === $moreShape->getType()) {
231+
$moreCondition .= $this->generateGetter('$page', $moreResult, (bool) $common);
232+
} else {
233+
$moreCondition .= 'null !== ' . $this->generateGetter('$page', $moreResult, (bool) $common);
234+
}
216235
}
217236
$setter = '';
218237
foreach ($inputToken as $index => $property) {

0 commit comments

Comments
 (0)