Skip to content

fix(sdk): walk backward through cursor pages in the TypeScript listAll iterators - #5601

Merged
gergely-kurucz-konghq merged 1 commit into
mainfrom
fix/sdk-cursor-backward-pagination
Nov 20, 2026
Merged

gergely-kurucz-konghq merged 1 commit into
mainfrom
fix/sdk-cursor-backward-pagination

Conversation

@gergely-kurucz-konghq

@gergely-kurucz-konghq gergely-kurucz-konghq commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Overview

What

  • The TypeScript SDK's cursor listAll iterators walk backward when the request starts with page.before: they follow meta.page.previous through page.before.
  • Requests without page.before keep following meta.page.next through page.after.
  • No page request carries both cursors. A request that starts with both throws before any fetch.
  • An empty page ends the walk, even when the server returns a cursor.

Why

  • The iterator added page.after from meta.page.next but kept the caller's page.before. The API rejects both cursors together, so listAll with page.before threw after the first page. This affects every cursor list that accepts page.before, such as credit transactions.
  • Following previous keeps the walk inside the caller's before bound; following next would return items past it.
  • The Go client already behaves this way (paginateCursor in api/v3/client/pagination.go), so both SDKs now agree.

How

  • api/spec/packages/typespec-typescript/templates/runtime/paginate.ts: the runtime template picks the direction from the first request. previous is optional in the envelope type, so forward-only callers type-check unchanged.
  • api/spec/packages/aip-client-javascript/src/lib/paginate.ts is regenerated with make gen-api.
  • tests/paginate.spec.ts adds the backward walk, the never-both-cursors check, the up-front rejection, and the empty-page stop. All four fail without the fix.

Notes for reviewer

  • Verified: the SDK's paginate.spec.ts and typecheck, the typespec-typescript vitest suite, and prettier on the touched files.
  • Untracked design documents and local files are excluded.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge.

Summary

Fixes backward cursor walks in the TypeScript SDK’s listAll iterators.

  • Cursor listAll walks backward from a before cursor.

No actionable issues found. Review used source inspection; tests were not run.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Start iteration] --> B{Both cursors supplied?}
  B -->|Yes| C[Throw before fetching]
  B -->|No| D{before supplied?}
  D -->|Yes| E[Fetch backward using before]
  D -->|No| F[Fetch forward using after]
  E --> G[Yield page items]
  F --> G
  G --> H{Empty page or no cursor?}
  H -->|Yes| I[Stop]
  H -->|No, backward| J[Set before to previous]
  H -->|No, forward| K[Set after to next]
  J --> E
  K --> F
Loading

Reviews (1) · Last reviewed commit: "fix(sdk): walk backward through cursor p..." · Reviewed by Greptile

Summary by CodeRabbit

  • New Features
    • Cursor pagination now supports navigating backward through results while preserving other request options.
  • Bug Fixes
    • Requests that include both forward and backward cursors are rejected before fetching.
    • Pagination now stops when a page is empty, even if it includes another cursor.

…l iterators

A listAll call that started with page.before kept that cursor and added
page.after from meta.page.next. The API rejects both cursors together,
so the iterator threw after the first page.

The direction now follows the caller's request, as in the Go client:
with page.before the walk follows meta.page.previous, otherwise it
follows meta.page.next. No page request carries both cursors, a request
that starts with both throws before fetching, and an empty page ends
the walk.
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Cursor pagination now supports backward traversal through page.before and meta.page.previous. It rejects requests that contain both cursors, preserves other request fields while advancing, and stops when the selected cursor is absent or the returned page is empty.

Changes: Cursor pagination

Layer / File(s) Summary
Bidirectional cursor pagination
api/spec/packages/aip-client-javascript/src/lib/paginate.ts, api/spec/packages/typespec-typescript/templates/runtime/paginate.ts, api/spec/packages/aip-client-javascript/tests/paginate.spec.ts
Both paginator implementations support backward traversal and reject requests containing both cursors. Tests cover backward traversal, cursor exclusivity, and stopping after an empty page.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: tothandras, borosr

Merge Risk: Minimal · up to 3d500

The change makes listAll follow previous through page.before when the request starts with before, so backward walks no longer send both cursors and get rejected by the API. It also rejects conflicting cursors before any request is sent and stops on empty pages. No concrete merge-blocking defect was found, and the added tests cover the changed behavior.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: the TypeScript listAll iterators now walk backward through cursor pages. The raw summary confirms that paginateCursor follows meta.page.previous via page.before when t…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: the TypeScript listAll iterators now walk backward through cursor pages. The raw summary confirms that paginateCursor follows meta.page.previous via page.before when the request sets page.before, and otherwise follows meta.page.next via page.after. The title also fits the fix described in the objectives. It is specific and does not rely on vague terms. The title does not mention the new rejection of requests with both cursors or the empty-page stop, but it does not need to cover every detail.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between fa13eb4 and 3d5006f.

Files selected for processing: 3

  • api/spec/packages/aip-client-javascript/src/lib/paginate.ts
  • api/spec/packages/aip-client-javascript/tests/paginate.spec.ts
  • api/spec/packages/typespec-typescript/templates/runtime/paginate.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

@gergely-kurucz-konghq
gergely-kurucz-konghq merged commit 8eb6a5b into main Nov 20, 2026
37 of 39 checks passed
@gergely-kurucz-konghq
gergely-kurucz-konghq deleted the fix/sdk-cursor-backward-pagination branch November 20, 2026 14:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/bug-fix Release note: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants