From a8e8bdb2f7b93835a00297c72b583cfdbfac8a6f Mon Sep 17 00:00:00 2001 From: Johan Cwiklinski Date: Mon, 10 Feb 2025 08:57:04 +0100 Subject: [PATCH] Allow extra nodes in inventory file --- .../Glpi/Inventory/InventoryTest.php | 51 +++++++++++++++++++ src/Glpi/Inventory/Inventory.php | 36 +++++++++++++ 2 files changed, 87 insertions(+) diff --git a/phpunit/functional/Glpi/Inventory/InventoryTest.php b/phpunit/functional/Glpi/Inventory/InventoryTest.php index e849b2c26e6..e7ac4945d4f 100644 --- a/phpunit/functional/Glpi/Inventory/InventoryTest.php +++ b/phpunit/functional/Glpi/Inventory/InventoryTest.php @@ -9386,4 +9386,55 @@ public function testRuleRecursivityNo(): void } } } + + public function testAdditionalProperty() + { + /** @var \DBmysql $DB */ + global $DB; + + $json_str = <<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() + ); + } } diff --git a/src/Glpi/Inventory/Inventory.php b/src/Glpi/Inventory/Inventory.php index 5d11f71ad93..3d0b3f32676 100644 --- a/src/Glpi/Inventory/Inventory.php +++ b/src/Glpi/Inventory/Inventory.php @@ -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 @@ -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()); @@ -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; + } }