Skip to content

Paging (APIResponse.Next) Ignores request's context #595

Description

@erane-opti

Describe the bug?

func (res *APIResponse) Next (v interface{}) (*APIResponse, error) doesn't accepts ctx any more (since v2?) and opting use client's ctx, this means the code like this:

users, resp, err := client.UserAPI.ListUsers(ctx, query)
// Do something with your users until you run out of users to iterate.
for resp.HasNextPage() {
  var nextUserSet []okta.User
  resp, err = resp.Next(&user2)
  ...
}

can't be cancelled (or timed-out) because resp building new request that uses another context.
I understand the client's context is used for auth, but it should be copied at most, not override the newer ctx provided in request's ctor

another issue using the old context is OTEL/spans which then new spans are not created in the current context

What is expected to happen?

calling cancel() on ctx stops the paging/loop

What is the actual behavior?

nothing happens, the original request context is ignored after the first execution ,
the migration guide of v6 mention the new pager (req.After()) but it isn't complete:

  1. No "get next token" on resp,
  2. After (or a "next page" method) should also consider an updated Limit from the Next link
  3. Not all paged requests implement After method (e.g. ApiListUserGroupsRequest, or ListAssignedRolesForUser and probably more)

Reproduction Steps?

ctx, canel := context.WithCancel(context.Background())
users, resp, err := oktaCli.UserAPI.ListUsers(ctx).Limit(1).Execute()
if err != nil {
  panic(err)
}
readUsers(users)
for resp.HasNextPage() {
  var newResp okta.APIResponse
  reps, err = resp.After(&newResp)
  if err != nil {
    panic(err)
  }
  readUsers(users)
  cancel()
}

Additional Information?

No response

Golang Version

go version go1.25.11 linux/amd64

SDK Version

v6.1.6

OS version

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions