Skip to content

Commit

Permalink
Allow extra nodes in inventory file
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher authored and cedric-anne committed Feb 10, 2025
1 parent d9acbd0 commit a8e8bdb
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
51 changes: 51 additions & 0 deletions phpunit/functional/Glpi/Inventory/InventoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9386,4 +9386,55 @@ public function testRuleRecursivityNo(): void
}
}
}

public function testAdditionalProperty()
{
/** @var \DBmysql $DB */
global $DB;

$json_str = <<<JSON
{
"action": "inventory",
"content": {
"hardware": {
"name": "pc_with_additional_prop",
"uuid": "32EED9C2-204C-42A1-A97E-A6EF2CE44B4F"
},
"unknown_property": [
{
"description": "An unknown property, will be silently ignored",
"any": true
}
],
"versionclient": "GLPI-Inventory_v1.11"
},
"deviceid": "WinDev2404Eval-2024-10-14-15-28-37",
"itemtype": "Computer"
}
JSON;
$json = json_decode($json_str);
$computer = new \Computer();

//initial import
$inventory = new \Glpi\Inventory\Inventory();
$this->assertFalse($inventory->isSchemaStrict());
$inventory->setData($json);
$inventory->doInventory();
$this->assertTrue($computer->getFromDBByCrit(['name' => 'pc_with_additional_prop']));

//enable strict schema checks; an error is thrown
$inventory = new \Glpi\Inventory\Inventory();
$inventory->setStrictSchema();
$this->assertTrue($inventory->isSchemaStrict());
$inventory->setData($json);

$this->assertTrue($inventory->inError());
$this->assertSame(
['JSON does not validate. Violations:
Additional properties not allowed: unknown_property at #->properties:content
'
],
$inventory->getErrors()
);
}
}
36 changes: 36 additions & 0 deletions src/Glpi/Inventory/Inventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class Inventory
private $request_query;
/** @var bool */
private bool $is_discovery = false;
private bool $strict_schema = false;

/**
* @param mixed $data Inventory data, optional
Expand Down Expand Up @@ -129,6 +130,9 @@ public function setData($data, $format = Request::JSON_MODE): bool
$converter = new Converter();

$schema = $converter->getSchema();
if (!$this->strict_schema) {
$schema->setFlexible(); //allow extra nodes in inventory file
}

$schema->setExtraItemtypes($this->getExtraItemtypes());

Expand Down Expand Up @@ -1019,4 +1023,36 @@ protected function getExtraItemtypes(): array
}
return $itemtypes;
}

/**
* Set schema validation strict (no additional properties allowed anywhere)
*
* @return self
*/
public function setFlexibleSchema(): self
{
$this->strict_schema = false;
return $this;
}

/**
* Set schema validation strict (no additional properties allowed anywhere)
*
* @return self
*/
public function setStrictSchema(): self
{
$this->strict_schema = true;
return $this;
}

/**
* Is scheam validation strict?
*
* @return bool
*/
public function isSchemaStrict(): bool
{
return $this->strict_schema;
}
}

0 comments on commit a8e8bdb

Please sign in to comment.