Skip to content

Commit

Permalink
Capacity to add new itemtypes on schema
Browse files Browse the repository at this point in the history
  • Loading branch information
trasher committed Sep 11, 2024
1 parent c45cd9d commit a63d971
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 50 deletions.
117 changes: 67 additions & 50 deletions lib/php/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ class Converter
private array $extra_properties = [];
/** @var array<string, array<string, array<string, string>>> */
private array $extra_sub_properties = [];
/** @var array<string> */
private array $extra_itemtypes = [];

/**
* @var array<string, array<int, string>>
Expand Down Expand Up @@ -194,6 +196,12 @@ public function setExtraSubProperties(array $properties): self
return $this;
}

public function setExtraItemtypes(array $itemtypes): self

Check failure on line 199 in lib/php/Converter.php

View workflow job for this annotation

GitHub Actions / PHP 7.4 on ubuntu-latest

Method Glpi\Inventory\Converter::setExtraItemtypes() has parameter $itemtypes with no value type specified in iterable type array.

Check failure on line 199 in lib/php/Converter.php

View workflow job for this annotation

GitHub Actions / PHP 8.0 on ubuntu-latest

Method Glpi\Inventory\Converter::setExtraItemtypes() has parameter $itemtypes with no value type specified in iterable type array.

Check failure on line 199 in lib/php/Converter.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 on ubuntu-latest

Method Glpi\Inventory\Converter::setExtraItemtypes() has parameter $itemtypes with no value type specified in iterable type array.

Check failure on line 199 in lib/php/Converter.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 on ubuntu-latest

Method Glpi\Inventory\Converter::setExtraItemtypes() has parameter $itemtypes with no value type specified in iterable type array.

Check failure on line 199 in lib/php/Converter.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 on ubuntu-latest

Method Glpi\Inventory\Converter::setExtraItemtypes() has parameter $itemtypes with no value type specified in iterable type array.
{
$this->extra_itemtypes = $itemtypes;
return $this;
}

/**
* Build (extended) JSON schema
* @return mixed
Expand All @@ -206,63 +214,72 @@ public function buildSchema()
}
$schema = json_decode($string);

$known_itemtypes = [];
preg_match('/\^\((.+)\)\$/', $schema->properties->itemtype->pattern, $known_itemtypes);
$known_itemtypes = explode('|', $known_itemtypes[1]);

Check failure on line 219 in lib/php/Converter.php

View workflow job for this annotation

GitHub Actions / PHP 7.4 on ubuntu-latest

Offset 1 does not exist on array{0?: string, 1?: non-empty-string}.

Check failure on line 219 in lib/php/Converter.php

View workflow job for this annotation

GitHub Actions / PHP 8.0 on ubuntu-latest

Offset 1 does not exist on array{0?: string, 1?: non-empty-string}.

Check failure on line 219 in lib/php/Converter.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 on ubuntu-latest

Offset 1 does not exist on array{0?: string, 1?: non-empty-string}.

Check failure on line 219 in lib/php/Converter.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 on ubuntu-latest

Offset 1 does not exist on array{0?: string, 1?: non-empty-string}.

Check failure on line 219 in lib/php/Converter.php

View workflow job for this annotation

GitHub Actions / PHP 8.3 on ubuntu-latest

Offset 1 does not exist on array{0?: string, 1?: non-empty-string}.
foreach ($this->extra_itemtypes as $extra_itemtype) {
if (!in_array($extra_itemtype, $known_itemtypes)) {
$known_itemtypes[] = addslashes($extra_itemtype);
}
}
$schema->properties->itemtype->pattern = sprintf(
'^(%s)$',
implode('|', $known_itemtypes)
);

$properties = $schema->properties->content->properties;

if ($this->extra_properties != null) {
foreach ($this->extra_properties as $extra_property => $extra_config) {
if (!property_exists($properties, $extra_property)) {
$properties->$extra_property = json_decode((string)json_encode($extra_config));
} else {
trigger_error(
sprintf('Property %1$s already exists in schema.', $extra_property),
E_USER_WARNING
);
}
foreach ($this->extra_properties as $extra_property => $extra_config) {
if (!property_exists($properties, $extra_property)) {
$properties->$extra_property = json_decode((string)json_encode($extra_config));
} else {
trigger_error(
sprintf('Property %1$s already exists in schema.', $extra_property),
E_USER_WARNING
);
}
}

if ($this->extra_sub_properties != null) {
foreach ($this->extra_sub_properties as $extra_sub_property => $extra_sub_config) {
if (property_exists($properties, $extra_sub_property)) {
foreach ($extra_sub_config as $subprop => $subconfig) {
$type = $properties->$extra_sub_property->type;
switch ($type) {
case 'array':
if (!property_exists($properties->$extra_sub_property->items->properties, $subprop)) {
$properties->$extra_sub_property->items->properties->$subprop =
json_decode((string)json_encode($subconfig));
} else {
trigger_error(
sprintf('Property %1$s already exists in schema.', $subprop),
E_USER_WARNING
);
}
break;
case 'object':
if (!property_exists($properties->$extra_sub_property->properties, $subprop)) {
$properties->$extra_sub_property->properties->$subprop =
json_decode((string)json_encode($subconfig));
} else {
trigger_error(
sprintf(
'Property %1$s/%2$s already exists in schema.',
$extra_sub_property,
$subprop
),
E_USER_WARNING
);
}
break;
default:
trigger_error('Unknown type ' . $type, E_USER_WARNING);
}
foreach ($this->extra_sub_properties as $extra_sub_property => $extra_sub_config) {
if (property_exists($properties, $extra_sub_property)) {
foreach ($extra_sub_config as $subprop => $subconfig) {
$type = $properties->$extra_sub_property->type;
switch ($type) {
case 'array':
if (!property_exists($properties->$extra_sub_property->items->properties, $subprop)) {
$properties->$extra_sub_property->items->properties->$subprop =
json_decode((string)json_encode($subconfig));
} else {
trigger_error(
sprintf('Property %1$s already exists in schema.', $subprop),
E_USER_WARNING
);
}
break;
case 'object':
if (!property_exists($properties->$extra_sub_property->properties, $subprop)) {
$properties->$extra_sub_property->properties->$subprop =
json_decode((string)json_encode($subconfig));
} else {
trigger_error(
sprintf(
'Property %1$s/%2$s already exists in schema.',
$extra_sub_property,
$subprop
),
E_USER_WARNING
);
}
break;
default:
trigger_error('Unknown type ' . $type, E_USER_WARNING);
}
} else {
trigger_error(
sprintf('Property %1$s does not exists in schema.', $extra_sub_property),
E_USER_WARNING
);
}
} else {
trigger_error(
sprintf('Property %1$s does not exists in schema.', $extra_sub_property),
E_USER_WARNING
);
}
}

Expand Down
21 changes: 21 additions & 0 deletions tests/Glpi/Inventory/tests/units/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,27 @@ public function testValidateVersionClient()
$this->assertFalse($instance->validate($json));
}

public function testValidateUnknownItemtype()
{
//itemtype \Glpi\Custom\Asset\Mine is unknown
$itemtype = '\Glpi\Custom\Asset\Mine';
$json = json_decode(json_encode(['deviceid' => 'myid', 'itemtype' => $itemtype, 'content' => ['versionclient' => 'GLPI-Agent_v1.0', 'hardware' => ['name' => 'my inventory']]]));
$instance = new \Glpi\Inventory\Converter();
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('\\\\Glpi\\\\Custom\\\\Asset\\\\Mine" does not match to ^(Unmanaged|Computer|Phone|NetworkEquipment|Printer)$');
$this->assertFalse($instance->validate($json));
}

public function testValidateNewItemtype()
{
//itemtype \Glpi\Custom\Asset\Mine is unknown
$itemtype = '\Glpi\Custom\Asset\Mine';
$json = json_decode(json_encode(['deviceid' => 'myid', 'itemtype' => $itemtype, 'content' => ['versionclient' => 'GLPI-Agent_v1.0', 'hardware' => ['name' => 'my inventory']]]));
$instance = new \Glpi\Inventory\Converter();
$this->assertInstanceOf(\Glpi\Inventory\Converter::class, $instance->setExtraItemtypes([$itemtype]));
$this->assertTrue($instance->validate($json));
}

public function testValidateUnknownExtraPlugin_node()
{
//extra "plugin_node" is unknown
Expand Down

0 comments on commit a63d971

Please sign in to comment.