From 9a2c0162d23e1e2790243fd4435de819f1f322a8 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Fri, 7 Oct 2022 10:05:01 +0200 Subject: [PATCH] Sync: respect null properties on merge fixes #2623 --- library/Director/Import/Sync.php | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/library/Director/Import/Sync.php b/library/Director/Import/Sync.php index 7523676c..adf8d161 100644 --- a/library/Director/Import/Sync.php +++ b/library/Director/Import/Sync.php @@ -46,6 +46,9 @@ class Sync /** @var IcingaObject[] Objects to work with */ protected $objects; + /** @var array> key => [property, property]*/ + protected $setNull = []; + /** @var bool Whether we already prepared your sync */ protected $isPrepared = false; @@ -513,7 +516,7 @@ class Sync } $object = $objects[$key]; - $this->prepareNewObject($row, $object, $sourceId); + $this->prepareNewObject($row, $object, $key, $sourceId); } } @@ -527,7 +530,7 @@ class Sync * @throws \Icinga\Exception\NotFoundError * @throws \Icinga\Module\Director\Exception\DuplicateKeyException */ - protected function prepareNewObject($row, DbObject $object, $sourceId) + protected function prepareNewObject($row, DbObject $object, $objectKey, $sourceId) { foreach ($this->syncProperties as $propertyKey => $p) { if ($p->get('source_id') !== $sourceId) { @@ -539,7 +542,6 @@ class Sync } $prop = $p->get('destination_field'); - $val = SyncUtils::fillVariables($p->get('source_expression'), $row); if ($object instanceof IcingaObject) { @@ -561,15 +563,23 @@ class Sync $this->wantArray($val) ); } else { - $object->vars()->$varName = $val; + if ($val === null) { + $this->setNull[$objectKey][] = $prop; + } else { + $object->vars()->$varName = $val; + } } } else { - if ($val !== null) { + if ($val === null) { + $this->setNull[$objectKey][] = $prop; + } else { $object->set($prop, $val); } } } else { - if ($val !== null) { + if ($val === null) { + $this->setNull[$objectKey][] = $prop; + } else { $object->set($prop, $val); } } @@ -776,6 +786,12 @@ class Sync } } } + + if (isset($this->setNull[$key])) { + foreach ($this->setNull[$key] as $property) { + $this->objects[$key]->set($property, null); + } + } } /**