/api/v1/users/search: can't get pagination to work

Infos:

  • Used Zammad version: 2.9.x
  • Used Zammad installation source: (source, package, …) docker-compose
  • Operating system: Debian 9
  • Browser + version: curl

Background

I want to use the API to find all users that have a company email adress. My original request was:

/api/v1/users/search?query=email:@ourcompany.tld

But that broke as soon as there were more than 100 results, so I want to use pagination. My request now looks like this (5 results per page for testing):

/api/v1/users/search?query=email:@ourcompany.tld&per_page=5&page=1

This works for the first page (I’m using jq here so one can see how many results were returned):

martin@dogmeat ~ % curl -s -H "Authorization: Token token=x" 'https://zammad.ourcompany.tld/api/v1/users/search?query=email:@ourcompany.tld&per_page=5&page=1' | jq 'length'
5

But all other pages do not return any results:

martin@dogmeat ~ % curl -s -H "Authorization: Token token=x" 'https://zammad.ourcompany.tld/api/v1/users/search?query=email:@ourcompany.tld&per_page=5&page=2' | jq 'length'
0
martin@dogmeat ~ % curl -s -H "Authorization: Token token=x" 'https://zammad.ourcompany.tld/api/v1/users/search?query=email:@ourcompany.tld&per_page=5&page=3' | jq 'length'
0

Am I doing something wrong, or is pagination bugged for /api/v1/users/search?

It works fine for /api/v1/tickets

martin@dogmeat ~ % curl -s -H "Authorization: Token token=x" 'https://zammad.ourcompany.tld/api/v1/tickets?per_page=5&page=1' | jq 'length' 
5
martin@dogmeat ~ % curl -s -H "Authorization: Token token=x" 'https://zammad.ourcompany.tld/api/v1/tickets?per_page=5&page=2' | jq 'length' 
5

Expected behavior:

  • /api/v1/users/search?query=email:@ourcompany.tld&per_page=5&page=n should return results for n > 1

Actual behavior:

  • /api/v1/users/search?query=email:@ourcompany.tld&per_page=5&page=n does not return results for n > 1

Steps to reproduce the behavior:

  • Run the curl requests I’ve posted; replace the token and the search query.

OK, I’m certain now it’s bugged. The necessary pagination information is passed to User.search which then returns already paginated results, but the # do pagination if needed block afterwards then assumes unpaginated results, and tries to extract the paginated subpart again.

It’s probably broken since cf1626494047f27d8fe094a7b954e04fef0f182b:

commit cf1626494047f27d8fe094a7b954e04fef0f182b
Author: Martin Edenhofer <me@edenhofer.de>
Date:   Fri Apr 13 09:22:55 2018 +0200

    Implemented issue #1951 - Allow pagination in search result with more then 100 results.

@martini reworked pagination across multiple controllers and removed the # do pagination if needed block from organizations_controller.rb and tickets_controller.rb, but apparently missed it in users_controller.rb :slight_smile:

This fixes it:

zammad@martin.mein-iserv.de ~ (develop) % git diff
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index f39a36ddb..106aa60f9 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -405,12 +405,6 @@ class UsersController < ApplicationController
     # do query
     user_all = User.search(query_params)

-    # do pagination if needed
-    if params[:page] && params[:per_page]
-      offset = (params[:page].to_i - 1) * params[:per_page].to_i
-      user_all = user_all[offset, params[:per_page].to_i] || []
-    end
-
     if response_expand?
       list = []
       user_all.each do |user|

I’ll look into writing tests for this and will submit a MR when I’m done.

EDIT: filed it: https://github.com/zammad/zammad/issues/2539

2 Likes

:hearts:

That would be great!

JFI: Here would be a good point to add a new test.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.