Skip to content

A free open source IT asset/license management system

Fixed #19983 - custom http adapter custom fields not being saved

snipe410ea3fde284
diff --git a/app/SyncAdapters/CustomHttp/CustomHttpAdapter.php b/app/SyncAdapters/CustomHttp/CustomHttpAdapter.php
index bed08005fc..2deb9a2035 100644
--- a/app/SyncAdapters/CustomHttp/CustomHttpAdapter.php
+++ b/app/SyncAdapters/CustomHttp/CustomHttpAdapter.php
@@ -6,7 +6,6 @@ use App\Models\Asset;
use App\Models\SyncAdapterConfig;
use App\SyncAdapters\HostInventoryRecord;
use App\SyncAdapters\PushableSyncAdapter;
-use App\SyncAdapters\SyncAdapter;
use Carbon\Carbon;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Http;
@@ -403,58 +402,62 @@ class CustomHttpAdapter extends PushableSyncAdapter
* After the base saveConfig persists field_paths + directions,
* make sure every custom:/native: destination has a self-routing
* mapping.<key> = <key> config so the framework's extras loop
- * writes each extracted value to its target at sync time. Cheap
- * to re-persist on every save because the values are stable.
+ * writes each extracted value to its target at sync time, and
+ * clean up any destinations the admin removed from the field_paths
+ * widget on this save.
+ *
+ * Deletion is "present in field_paths before, absent
+ * after" rather than the mapping-picker omission the earlier
+ * revision keyed on. CustomHttp does not render the mapping-picker
+ * widget (rendersExtrasPicker() = false), so no `<slug>_mapping`
+ * key is ever POSTed, and the omission check matched every
+ * pre-existing extra on every save, dropping legitimate mappings
+ * on round-trip. (We pulled supportsAdminDefinedExtras() from this
+ * adapter since the "Vendor Response Paths" widget should handle that
+ * mapping. It was clunky and confusing with both before.)
*/
public function saveConfig(\Illuminate\Http\Request $request): void
{
$slug = $this->instance->slug;
// Snapshot the stored field_paths BEFORE parent::saveConfig
- // overwrites the credential. We need it to tell a pre-existing
- // extras entry (whose row might have been deleted via the
- // mapping-picker on this save) from a brand-new one added via
- // the field_paths widget's pending-row flow (which has no
- // matching _mapping key either). Without the snapshot, every
- // new row would look identical to a deletion.
+ // overwrites the credential, so we can diff it against the
+ // post-save state and detect widget-side deletions.
$existingPathsBefore = $this->fieldPathMap();
- // The parent's persistFieldMappings forgets any mapping key
- // the POST omitted - that's how the mapping-picker's delete
- // button signals removal.
- $postedMappings = (array) $request->input($slug.'_mapping', []);
-
parent::saveConfig($request);
+ $pathsAfter = $this->fieldPathMap();
$postedDirections = (array) $request->input($slug.'_direction', []);
- $prunedFromFieldPaths = [];
- foreach (array_keys($this->fieldPathMap()) as $destination) {
+ // Deletions: destinations the admin removed via the field_paths
+ // widget's × button are absent from `pathsAfter`. parent::
+ // persistCredentialsFromSchema already wrote the pruned
+ // field_paths credential, so we just need to drop the orphan
+ // direction + self-routing mapping entries.
+ foreach (array_keys($existingPathsBefore) as $destination) {
if (! str_starts_with($destination, 'custom:') && ! str_starts_with($destination, 'native:')) {
continue;
}
-
- // Deletion test: the entry existed before this save AND
- // the admin's mapping-picker POST omits it. Brand-new
- // entries (added via the field_paths pending-row flow on
- // this same save) pass through unchanged - they have no
- // _mapping key yet, but weren't previously stored either.
- $existedBefore = array_key_exists($destination, $existingPathsBefore);
- $omittedFromMappings = ! array_key_exists($destination, $postedMappings);
- if ($existedBefore && $omittedFromMappings) {
+ if (!array_key_exists($destination, $pathsAfter)) {
SyncAdapterConfig::forget($this->instance->id, 'direction.'.$destination);
- $prunedFromFieldPaths[] = $destination;
+ SyncAdapterConfig::forget($this->instance->id, 'mapping.' . $destination);
+ }
+ }
+ // Current entries: write the self-routing mapping and persist
+ // the per-row direction. parent::persistFieldMappings forgets
+ // every extras `mapping.<key>` on each save (its own cleanup
+ // keys on the mapping-picker POST, which this adapter does not
+ // emit), so we re-write them here for the destinations the
+ // admin kept.
+ foreach (array_keys($pathsAfter) as $destination) {
+ if (!str_starts_with($destination, 'custom:') && !str_starts_with($destination, 'native:')) {
continue;
}
SyncAdapterConfig::put($this->instance->id, 'mapping.'.$destination, $destination);
- // Parent's persistFieldDirections dropped these because
- // validMappingFields didn't yet include the destination
- // key on a first save (extraFields() only picks them up
- // once mapping.<key> is stored). Persist here so per-row
- // direction from the widget survives the first save.
if (isset($postedDirections[$destination])) {
$direction = (string) $postedDirections[$destination];
if (in_array($direction, ['pull', 'push', 'both', 'skip'], true)) {
@@ -462,19 +465,6 @@ class CustomHttpAdapter extends PushableSyncAdapter
}
}
}
-
- // Rewrite the field_paths credential with the pruned entries
- // removed so the deleted extras don't regenerate on next
- // render. parent::saveConfig already persisted the raw
- // credential, so we re-decode / filter / re-encode here.
- if ($prunedFromFieldPaths !== []) {
- $raw = (string) $this->safeCredential('field_paths');
- $decoded = $raw === '' ? [] : (array) (json_decode($raw, true) ?? []);
- foreach ($prunedFromFieldPaths as $key) {
- unset($decoded[$key]);
- }
- SyncAdapterConfig::put($this->instance->id, 'field_paths', json_encode($decoded, JSON_UNESCAPED_SLASHES));
- }
}
/**
diff --git a/tests/Feature/SyncAdapters/CustomHttp/CustomHttpAdapterTest.php b/tests/Feature/SyncAdapters/CustomHttp/CustomHttpAdapterTest.php
index 0b7b5859e4..f70eae1f2a 100644
--- a/tests/Feature/SyncAdapters/CustomHttp/CustomHttpAdapterTest.php
+++ b/tests/Feature/SyncAdapters/CustomHttp/CustomHttpAdapterTest.php
@@ -271,6 +271,92 @@ class CustomHttpAdapterTest extends TestCase
$this->assertNull(SyncAdapterConfig::get($instance->id, 'direction.hostname'));
}
+ public function test_saveconfig_preserves_existing_custom_field_mappings_on_round_trip()
+ {
+ // Regression for #19983: saving a CustomHttp adapter with its
+ // existing custom:X mappings re-posted (no changes made) must
+ // keep them. The previous deletion-detection logic keyed on
+ // the `<slug>_mapping` POST, which CustomHttp does not emit
+ // (rendersExtrasPicker = false), so every pre-existing extra
+ // looked "deleted" on every save and the mapping / direction /
+ // field_paths rows were wiped.
+ $adapter = $this->configuredAdapter([
+ 'field_paths' => json_encode([
+ 'custom:5' => 'tags.asset_tag',
+ 'custom:6' => 'location.room',
+ ]),
+ 'mapping.custom:5' => 'custom:5',
+ 'mapping.custom:6' => 'custom:6',
+ 'direction.custom:5' => 'pull',
+ 'direction.custom:6' => 'pull',
+ ]);
+ $slug = $adapter->name();
+ $instance = SyncAdapterInstance::where('slug', $slug)->firstOrFail();
+
+ $adapter->saveConfig(\Illuminate\Http\Request::create('/', 'POST', [
+ $slug.'_url' => 'https://vendor.example/api',
+ $slug.'_source_id_path' => 'id',
+ $slug.'_field_paths' => [
+ 'custom:5' => 'tags.asset_tag',
+ 'custom:6' => 'location.room',
+ ],
+ $slug.'_direction' => [
+ 'custom:5' => 'pull',
+ 'custom:6' => 'pull',
+ ],
+ ]));
+
+ $stored = json_decode(SyncAdapterConfig::get($instance->id, 'field_paths'), true);
+ $this->assertSame(
+ ['custom:5' => 'tags.asset_tag', 'custom:6' => 'location.room'],
+ $stored,
+ 'Custom field paths should round-trip untouched when the admin re-saves without changes.',
+ );
+ $this->assertSame('custom:5', SyncAdapterConfig::get($instance->id, 'mapping.custom:5'));
+ $this->assertSame('custom:6', SyncAdapterConfig::get($instance->id, 'mapping.custom:6'));
+ $this->assertSame('pull', SyncAdapterConfig::get($instance->id, 'direction.custom:5'));
+ $this->assertSame('pull', SyncAdapterConfig::get($instance->id, 'direction.custom:6'));
+ }
+
+ public function test_saveconfig_removes_mapping_direction_when_field_paths_row_is_deleted()
+ {
+ // Deletion signal: a destination present in the stored
+ // field_paths map but absent from the POST means the admin
+ // removed the row via the field_paths widget's × button.
+ // Clean up the orphan mapping + direction entries so a stale
+ // self-routing config doesn't survive the deletion.
+ $adapter = $this->configuredAdapter([
+ 'field_paths' => json_encode([
+ 'custom:5' => 'tags.asset_tag',
+ 'custom:6' => 'location.room',
+ ]),
+ 'mapping.custom:5' => 'custom:5',
+ 'mapping.custom:6' => 'custom:6',
+ 'direction.custom:5' => 'pull',
+ 'direction.custom:6' => 'pull',
+ ]);
+ $slug = $adapter->name();
+ $instance = SyncAdapterInstance::where('slug', $slug)->firstOrFail();
+
+ // Admin removed custom:6 via the widget. POST only re-sends custom:5.
+ $adapter->saveConfig(\Illuminate\Http\Request::create('/', 'POST', [
+ $slug.'_url' => 'https://vendor.example/api',
+ $slug.'_source_id_path' => 'id',
+ $slug.'_field_paths' => [
+ 'custom:5' => 'tags.asset_tag',
+ ],
+ $slug.'_direction' => [
+ 'custom:5' => 'pull',
+ ],
+ ]));
+
+ $stored = json_decode(SyncAdapterConfig::get($instance->id, 'field_paths'), true);
+ $this->assertSame(['custom:5' => 'tags.asset_tag'], $stored);
+ $this->assertSame('custom:5', SyncAdapterConfig::get($instance->id, 'mapping.custom:5'));
+ $this->assertNull(SyncAdapterConfig::get($instance->id, 'mapping.custom:6'));
+ $this->assertNull(SyncAdapterConfig::get($instance->id, 'direction.custom:6'));
+ }
+
public function test_pull_warns_when_configured_path_returns_null_on_every_record()
{
// A dot-path that returns null on every record is almost