Skip to content

Search endpoint does not correctly encode criteria/metacriteria query parameters #1

@AdamOuali

Description

@AdamOuali

Description

The search() API helper currently passes criteria, metacriteria, and forcedisplay directly to the HTTP client as query parameters.

Example:

search = api.computer.search(
    criteria=[
        {
            "field": 126,
            "searchtype": "equals",
            "value": "X.X.X.X",
        }
    ],
    forcedisplay=[1, 5, 126],
    range="0-1999",
)

The request completes successfully, but GLPI returns no results because the generated query string is not encoded in the format expected by the GLPI Search API.

Instead of generating keys such as:

criteria[0][field]=126
criteria[0][searchtype]=equals
criteria[0][value]=X.X.X.X
forcedisplay[0]=1
forcedisplay[1]=5
forcedisplay[2]=126

the underlying HTTP client receives Python lists/dicts and serializes them incorrectly.

This makes complex searches silently fail, especially when using criteria, metacriteria, or forcedisplay.

Proposed solution

Flatten nested search parameters before passing them to requests / aiohttp.

Example implementation:

def _flatten_search_params(params: dict) -> dict:
    flat = {}

    for key in ("criteria", "metacriteria"):
        items = params.pop(key, None)
        if not items:
            continue

        for i, crit in enumerate(items):
            for sub_key, value in crit.items():
                flat[f"{key}[{i}][{sub_key}]"] = value

    forcedisplay = params.pop("forcedisplay", None)
    if forcedisplay:
        for i, field in enumerate(forcedisplay):
            flat[f"forcedisplay[{i}]"] = field

    flat.update(params)
    return flat

and then:

params = _boolify_params(_flatten_search_params(kwargs))

inside GlpiAPI.search().

Impact

This change allows the Python API to generate search requests that match the format documented by GLPI and enables criteria-based searches such as searching computers by IP address, serial number, inventory number, etc.

Notes

I have a working local patch implementing this approach. I have not opened a pull request yet, but I wanted to report the issue and share the proposed fix.

Additional informations

  • Python version :
    Python 3.10.12

  • GLPI version :
    GLPI 10.0.19

  • Lib version :

Name: glpi-utils
Version: 1.4.4
Summary: Python library for working with the GLPI REST API (9.1+, 10.x, 11.x)
Home-page: https://github.com/giovanny07/python-glpi-utils
Author: Giovanny Rodriguez
Author-email: giovanny@imagunet.com
License: UNKNOWN
Location: /home/adamo/libtest/python-glpi-utils
Requires: requests
Required-by: 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions