Error with count() in SearchApiTypesenseService if value is an array

Created on 21 November 2023, 10 months ago
Updated 24 March 2024, 6 months ago

Problem/Motivation

We're working on indexing field types not in the current version of the module. Our example is a geopoint, which means the value we're indexing is an array. See documentation: https://typesense.org/docs/0.25.1/api/geosearch.html#searching-within-a-...

I came across this bug/mistake in the code causing this error:

TypeError: count(): Argument #1 ($value) must be of type Countable|array, bool given in count() (line 367 of modules/contrib/search_api_typesense/src/Api/SearchApiTypesenseService.php).

Code causing error:

public function prepareItemValue($value, $type) {
    if (is_array($value) && count($value <= 1)) {
      $value = reset($value);
    }

Steps to reproduce

Clone any of the data_type plugins and return an array. Could be as simple as:

return (string) [$value, $value];

Proposed resolution

Easy to fix by moving the closing parentheses so it's "count($value) <= 1" instead of "count($value <= 1)".

Remaining tasks

Test patch

User interface changes

N/A

API changes

N/A

Data model changes

N/A

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

🇬🇧United Kingdom khaled.zaidan

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

Comments & Activities

Production build 0.71.5 2024