Skip to content

Conversation

@stonebuzz
Copy link
Contributor

@stonebuzz stonebuzz commented Oct 24, 2025

Checklist before requesting a review

Please delete options that are not relevant.

  • I have performed a self-review of my code.
  • I have added tests (when available) that prove my fix is effective or that my feature works.
  • I have updated the CHANGELOG with a short functional description of the fix or new feature.
  • This change requires a documentation update.

Description

Fix :

Prevent error when decoding itemtypes with GLPI custom asset classes

[2025-10-24 07:35:00] glpi.CRITICAL: *** Uncaught PHP Exception Twig\Error\RuntimeError: 
"An exception has been thrown during the rendering of a template 
("in_array(): Argument #2 ($haystack) must be of type array, null given") 
in "pages/generic_form.html.twig" at line 33." 
at generic_form.html.twig line 33
Backtrace :

The issue occurs when fields['itemtypes'] contains GLPI Custom Asset class names using namespaces, for example:

["Ticket", "Glpi\CustomAsset\Car"]

In such cases, json_decode() fails because unescaped backslashes make the JSON string invalid, returning null instead of an array.

To ensure consistent decoding of itemtypes, enforce the use of the following helper method:

public static function decodeJSONItemtypes(string $itemtypes, ?bool $associative = null)
{
    $jsonitemtype = json_decode($itemtypes, $associative);

    // Attempt recovery when JSON decoding fails due to unescaped backslashes
    if ($jsonitemtype === null && json_last_error() !== JSON_ERROR_NONE) {
        $fixed_json = str_replace('\\', '\\\\', $itemtypes);
        $jsonitemtype = json_decode($fixed_json, $associative);
    }

    return $jsonitemtype;
}

This ensures proper decoding of itemtypes even when they contain GLPI namespaced asset classes, avoiding in_array() runtime errors during template rendering.

Fix migration for GenericObject itemtypes

The migration process for GenericObject types has been updated to correctly generate the corresponding GLPI Custom Asset class names using proper namespace escaping:

foreach ($result as $type) {
    $migration_genericobject_itemtype[$type['itemtype']] = [
        'genericobject_itemtype' => $type['itemtype'],
        'itemtype' => 'Glpi\\\\CustomAsset\\\\' . $type['name'] . 'Asset', // HERE
        'genericobject_name' => $type['name'],
        'name' => $type['name'] . 'Asset',
    ];
}

This ensures that migrated itemtypes reference the correct fully qualified class names (e.g., Glpi\\CustomAsset\\Car) and remain compatible with json_decode() and Twig rendering.

For instances that have already completed the migration prior to this fix, the correction must still be applied to maintain consistency and prevent runtime errors during template rendering.

@stonebuzz stonebuzz force-pushed the fix_itemtype_extract branch from 66bd3c6 to 28e77f1 Compare October 24, 2025 08:03
@stonebuzz stonebuzz self-assigned this Oct 24, 2025
@stonebuzz stonebuzz added the bug label Oct 24, 2025
@stonebuzz stonebuzz merged commit 71d2399 into main Oct 24, 2025
3 checks passed
@stonebuzz stonebuzz deleted the fix_itemtype_extract branch October 24, 2025 11:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants