@@ -19,6 +19,7 @@ import software.amazon.smithy.kotlin.codegen.lang.KotlinTypes
1919import software.amazon.smithy.kotlin.codegen.model.SymbolProperty
2020import software.amazon.smithy.kotlin.codegen.model.expectShape
2121import software.amazon.smithy.kotlin.codegen.model.hasTrait
22+ import software.amazon.smithy.kotlin.codegen.model.traits.PaginationTruncationMember
2223import software.amazon.smithy.kotlin.codegen.utils.getOrNull
2324import software.amazon.smithy.model.Model
2425import software.amazon.smithy.model.knowledge.PaginatedIndex
@@ -28,6 +29,7 @@ import software.amazon.smithy.model.shapes.MapShape
2829import software.amazon.smithy.model.shapes.OperationShape
2930import software.amazon.smithy.model.shapes.ServiceShape
3031import software.amazon.smithy.model.shapes.Shape
32+ import software.amazon.smithy.model.shapes.StructureShape
3133import software.amazon.smithy.model.traits.PaginatedTrait
3234
3335/* *
@@ -77,6 +79,7 @@ class PaginatorGenerator : KotlinIntegration {
7779 serviceSymbol,
7880 paginatedOperation,
7981 inputSymbol,
82+ paginationInfo.output,
8083 outputSymbol,
8184 paginationInfo,
8285 cursorSymbol,
@@ -100,6 +103,7 @@ class PaginatorGenerator : KotlinIntegration {
100103 serviceSymbol : Symbol ,
101104 operationShape : OperationShape ,
102105 inputSymbol : Symbol ,
106+ outputShape : StructureShape ,
103107 outputSymbol : Symbol ,
104108 paginationInfo : PaginationInfo ,
105109 cursorSymbol : Symbol ,
@@ -141,18 +145,26 @@ class PaginatorGenerator : KotlinIntegration {
141145 ) {
142146 withBlock(" #T {" , " }" , ExternalTypes .KotlinxCoroutines .FlowGenerator ) {
143147 write(" var cursor: #F = null" , cursorSymbol)
144- write(" var isFirstPage : Boolean = true" )
148+ write(" var hasNextPage : Boolean = true" )
145149 write(" " )
146- withBlock(" while (isFirstPage || (cursor?.isNotEmpty() == true) ) {" , " }" ) {
150+ withBlock(" while (hasNextPage ) {" , " }" ) {
147151 withBlock(" val req = initialRequest.copy {" , " }" ) {
148152 write(" this.$markerLiteral = cursor" )
149153 }
150154 write(
151155 " val result = this@#1LPaginated.#1L(req)" ,
152156 operationShape.defaultName(),
153157 )
154- write(" isFirstPage = false" )
155158 write(" cursor = result.$nextMarkerLiteral " )
159+
160+ val hasNextPageFlag = outputShape
161+ .members()
162+ .singleOrNull { it.hasTrait(PaginationTruncationMember .ID ) }
163+ ?.defaultName()
164+ ?.let { " result.$it " }
165+ ? : " cursor?.isNotEmpty()"
166+
167+ write(" hasNextPage = #L == true" , hasNextPageFlag)
156168 write(" emit(result)" )
157169 }
158170 }
0 commit comments