Skip to content

Commit 05ecea1

Browse files
nogatesclaude
andauthored
Allow cursor-based pagination to query extra page until collection is empty (#3790)
Mirrors DataDog/datadog-api-client-go#3769. Cursor paginators now keep querying until the response data is empty, instead of stopping when a page returns fewer items than the requested limit. Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
1 parent ff2bdd5 commit 05ecea1

36 files changed

Lines changed: 68 additions & 3 deletions

.generator/src/generator/templates/Api.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public class {{ name }} {
205205
args.put("optionalParams", parameters);
206206
{%- endif %}
207207

208-
PaginationIterable iterator = new PaginationIterable(this, "{{ operationId }}", resultsPath, valueGetterPath, valueSetterPath, valueSetterParamOptional, {% if pagination.pageParam %}false{% else %}true{% endif %}, limit, args, {{ pagination.pageStart | default(0) }});
208+
PaginationIterable iterator = new PaginationIterable(this, "{{ operationId }}", resultsPath, valueGetterPath, valueSetterPath, valueSetterParamOptional, {% if pagination.pageParam %}false{% else %}true{% endif %}, {% if pagination.cursorParam %}true{% else %}false{% endif %}, limit, args, {{ pagination.pageStart | default(0) }});
209209

210210
return iterator;
211211
{%- endmacro %}

.generator/src/generator/templates/PaginationIterable.j2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class PaginationIterable<T> implements Iterable<T> {
1212
String[] valueSetterPath;
1313
Boolean valueSetterParamOptional;
1414
Boolean offsetPageIncrement;
15+
Boolean cursorPagination;
1516
Object limit;
1617
LinkedHashMap<String, Object> args;
1718
int pageStart;
@@ -24,6 +25,7 @@ public class PaginationIterable<T> implements Iterable<T> {
2425
String valueSetterPath,
2526
Boolean valueSetterParamOptional,
2627
Boolean offsetPageIncrement,
28+
Boolean cursorPagination,
2729
Object limit,
2830
LinkedHashMap<String, Object> args,
2931
int pageStart) {
@@ -41,6 +43,7 @@ public class PaginationIterable<T> implements Iterable<T> {
4143
this.valueSetterPath = valueSetterPath.split("\\.");
4244
this.valueSetterParamOptional = valueSetterParamOptional;
4345
this.offsetPageIncrement = offsetPageIncrement;
46+
this.cursorPagination = cursorPagination;
4447
this.limit = limit;
4548
this.args = args;
4649
this.pageStart = pageStart;

.generator/src/generator/templates/PaginationIterator.j2

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ public class PaginationIterator<T> implements Iterator<T> {
142142
return true;
143143
}
144144

145-
if (this.data.size() < convertToInt(this.iterable.limit)) {
145+
if (this.iterable.cursorPagination) {
146+
if (this.data.size() == 0) {
147+
return false;
148+
}
149+
} else if (this.data.size() < convertToInt(this.iterable.limit)) {
146150
return false;
147151
}
148152

src/main/java/com/datadog/api/client/PaginationIterable.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class PaginationIterable<T> implements Iterable<T> {
1616
String[] valueSetterPath;
1717
Boolean valueSetterParamOptional;
1818
Boolean offsetPageIncrement;
19+
Boolean cursorPagination;
1920
Object limit;
2021
LinkedHashMap<String, Object> args;
2122
int pageStart;
@@ -28,6 +29,7 @@ public PaginationIterable(
2829
String valueSetterPath,
2930
Boolean valueSetterParamOptional,
3031
Boolean offsetPageIncrement,
32+
Boolean cursorPagination,
3133
Object limit,
3234
LinkedHashMap<String, Object> args,
3335
int pageStart) {
@@ -45,6 +47,7 @@ public PaginationIterable(
4547
this.valueSetterPath = valueSetterPath.split("\\.");
4648
this.valueSetterParamOptional = valueSetterParamOptional;
4749
this.offsetPageIncrement = offsetPageIncrement;
50+
this.cursorPagination = cursorPagination;
4851
this.limit = limit;
4952
this.args = args;
5053
this.pageStart = pageStart;

src/main/java/com/datadog/api/client/PaginationIterator.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,11 @@ public boolean hasNext() {
147147
return true;
148148
}
149149

150-
if (this.data.size() < convertToInt(this.iterable.limit)) {
150+
if (this.iterable.cursorPagination) {
151+
if (this.data.size() == 0) {
152+
return false;
153+
}
154+
} else if (this.data.size() < convertToInt(this.iterable.limit)) {
151155
return false;
152156
}
153157

src/main/java/com/datadog/api/client/v1/api/DashboardsApi.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,6 +1543,7 @@ public PaginationIterable<DashboardSummaryDefinition> listDashboardsWithPaginati
15431543
valueSetterPath,
15441544
valueSetterParamOptional,
15451545
true,
1546+
false,
15461547
limit,
15471548
args,
15481549
0);

src/main/java/com/datadog/api/client/v1/api/MonitorsApi.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,7 @@ public PaginationIterable<Monitor> listMonitorsWithPagination(
13291329
valueSetterPath,
13301330
valueSetterParamOptional,
13311331
false,
1332+
false,
13321333
limit,
13331334
args,
13341335
0);

src/main/java/com/datadog/api/client/v1/api/NotebooksApi.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,7 @@ public PaginationIterable<NotebooksResponseData> listNotebooksWithPagination(
690690
valueSetterPath,
691691
valueSetterParamOptional,
692692
true,
693+
false,
693694
limit,
694695
args,
695696
0);

src/main/java/com/datadog/api/client/v1/api/ServiceLevelObjectiveCorrectionsApi.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ public PaginationIterable<SLOCorrection> listSLOCorrectionWithPagination(
597597
valueSetterPath,
598598
valueSetterParamOptional,
599599
true,
600+
false,
600601
limit,
601602
args,
602603
0);

src/main/java/com/datadog/api/client/v1/api/ServiceLevelObjectivesApi.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,7 @@ public PaginationIterable<ServiceLevelObjective> listSLOsWithPagination(
14451445
valueSetterPath,
14461446
valueSetterParamOptional,
14471447
true,
1448+
false,
14481449
limit,
14491450
args,
14501451
0);

0 commit comments

Comments
 (0)