JSON pager "type: page" doesn't parse the last page

Created on 19 November 2024, 30 days ago

Problem/Motivation

When using the pager for JSON data with type: page the last page doesn't get added to the next URLs.
See the code snippet below from data_parser/Json.php:

case 'page':
        if (NULL !== $selector_data && is_scalar($selector_data)) {
          // Just use 'page' as a default parameter key if not provided.
          $key = !empty($this->configuration['pager']['key']) ? $this->configuration['pager']['key'] : 'page';
          // Define the max page to generate.
          $max = $selector_data + 1;
          if (!empty($this->configuration['pager']['selector_max'])) {
            $max = $this->getSourceData($url, $this->configuration['pager']['selector_max']);
          }

          // Parse the url and replace the page param value and rebuild the url.
          $path = UrlHelper::parse($url);
          for ($page = $selector_data + 1; $page < $max; ++$page) {
            $path['query'][$key] = $page;
            $next_urls[] = Url::fromUri($path['path'], [
              'query' => $path['query'],
              'fragment' => $path['fragment'],
            ])->toString();
          }
        }
        break;

$selector_data is the current page number for the source while the selector_max parameter should define the maximum number of available pages.
The condition $page < $max in the for loop will stop the loop before reaching the max page.

Proposed resolution

Use $page <= $max to include the last page.

🐛 Bug report
Status

Active

Version

6.0

Component

Plugins

Created by

🇫🇮Finland pulakka

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇦Ukraine init90 Ukraine

    @pulakka Thanks! I can confirm that the problem exists.

    I use migration for recurring product imports from external API.

    How look API URLs process with the current codebase:

    Process data from https://API_URL/api/v1/products.json
    Process data from https://API_URL/api/v1/products.json?page_no=2
    Process data from https://API_URL/api/v1/products.json?page_no=3
    ...
    Process data from https://API_URL/api/v1/products.json?page_no=23
    Process data from https://API_URL/api/v1/products.json?page_no=24
    Process data from https://API_URL/api/v1/products.json?page_no=25
    

    Number of imported products: 2527

    After the proposed correction:

    Process data from https://API_URL/api/v1/products.json
    Process data from https://API_URL/api/v1/products.json?page_no=2
    Process data from https://API_URL/api/v1/products.json?page_no=3
    ...
    Process data from https://API_URL/api/v1/products.json?page_no=24
    Process data from https://API_URL/api/v1/products.json?page_no=25
    Process data from https://API_URL/api/v1/products.json?page_no=26
    

    Number of imported products: 2592

  • 🇺🇦Ukraine init90 Ukraine
  • Pipeline finished with Failed
    26 days ago
    Total: 3683s
    #347251
  • 🇫🇮Finland pulakka

    @init90 Thanks for the patch! Works well.

Production build 0.71.5 2024