Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions phpunit/functional/WebhookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,4 +297,22 @@ public function testWithHLAPIDisabled(): void
// The main purpose is to test the internal authentication middleware.
$this->assertNotNull($webhook->getResultForPath('/Administration/User/' . $users_id, 'new', 'User', $users_id));
}

public function testGetMonacoSuggestions()
{
$itemtypes = \Webhook::getItemtypesDropdownValues();
$noSuggestions = ['Document_Item'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cconard96 Could you validate that having no suggestions for Document_Item is expected?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it isn't. No itemtype option for webhooks should be missing suggestions because if they are, it means the feature is broken for that itemtype and doesn't know how to get the data from the API.

Copy link
Contributor

@cconard96 cconard96 Sep 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This itemtype is likely broken because it is provided by ManagementController, not ITILController.

Copy link
Member Author

@froozeify froozeify Sep 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it is the ITILController that is called, but indeed inside it I don't see the Document

Image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know where the schema is declared so I can see why document is not returned in ITILController

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be in ITILController::225 but I don't see any reference to the Document type, maybe this schema is missing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In getAPIItemtypeData the Document_Item::class entry needs moved under the ManagementController array so it knows where to find the schema.

Currently, the possible parents for "subtypes" only account for the classes listed under the "main" array for each controller. This should be changed to look in all of the "main" arrays. For any cases where an empty array is used, we could continue using all "main" itemtypes in its own controller or switch them to a static list. I would prefer a static list at this point for more stability.


foreach ($itemtypes as $types) {
$this->assertIsArray($types);
foreach ($types as $itemtype => $label) {
$suggestions = \Webhook::getMonacoSuggestions($itemtype);
if (in_array($itemtype, $noSuggestions)) {
$this->assertEmpty($suggestions, "Suggestions for $itemtype should be empty");
} else {
$this->assertNotEmpty($suggestions, "Missing suggestions for $itemtype");
}
}
}
}
}
22 changes: 17 additions & 5 deletions src/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -438,9 +438,9 @@ public static function getAPIItemtypeData(): array
}

/**
* Return a list of GLPI itemtypes availabel through HL API.
* Return a list of GLPI itemtypes available through HL API.
*
* @return array
* @return array<array>
*/
public static function getItemtypesDropdownValues(): array
{
Expand Down Expand Up @@ -917,10 +917,19 @@ public static function getAPISchemaBySupportedItemtype(string $itemtype): ?array
$controller_class = $controller;
break;
}

if (isset($categories['subtypes']) && array_key_exists($itemtype, $categories['subtypes'])) {
$schema_name = $categories['subtypes'][$itemtype]['name'];
$schema_name = $categories['main'][$categories['subtypes'][$itemtype]['parent']]['name'] . $schema_name;
$controller_class = $controller;
$schema_name = $categories['subtypes'][$itemtype]['name'];

if (
array_key_exists('parent', $categories['subtypes'][$itemtype])
&& array_key_exists($categories['subtypes'][$itemtype]['parent'], $categories['main'])
&& array_key_exists('name', $categories['main'][$categories['subtypes'][$itemtype]['parent']])
) {
$schema_name = $categories['main'][$categories['subtypes'][$itemtype]['parent']]['name'] . $schema_name;
}

break;
}
}
Expand All @@ -939,6 +948,9 @@ public static function getMonacoSuggestions(?string $itemtype): array
return [];
}
$schema = self::getAPISchemaBySupportedItemtype($itemtype);
if (is_null($schema)) {
return [];
}
$props = Doc\Schema::flattenProperties($schema['properties'], 'item.');
$parent_schema = self::getParentItemSchema($itemtype);
$parent_props = $parent_schema !== [] ? Doc\Schema::flattenProperties($parent_schema['properties'], 'parent_item.') : [];
Expand All @@ -953,7 +965,7 @@ public static function getMonacoSuggestions(?string $itemtype): array
$subtype_labels = [];
if (isset($parent_schema['x-subtypes'])) {
foreach ($parent_schema['x-subtypes'] as $subtype) {
$subtype_labels[$subtype] = $subtype['itemtype']::getTypeName(1);
$subtype_labels[$subtype['itemtype']] = $subtype['itemtype']::getTypeName(1);
}
}
foreach ($props as $prop_name => $prop_data) {
Expand Down
Loading