Bug 27: unveränderter distance=0 einer dynamischen Route beim Set abgelehnt

Gleiche Ursache wie Bug 26, aber bei einem kuratierten statt einem
unkuratierten Feld: eine dynamische/verbundene Route hat distance=0,
RouterOS akzeptiert das nur system-intern, nicht als expliziten Eingabe-
wert für "set" — auch wenn der Wert unverändert zurückgesendet wird
("value of distance out of range (1...255)").

Fix generalisiert das "nur bei Änderung senden"-Prinzip aus Bug 26 von
unkuratierten auf ALLE Felder beim Bearbeiten eines bestehenden Items:
pendingCommand diffed jetzt gegen das ursprünglich geladene Item, statt
kuratierte Felder immer komplett neu zu senden. Deckt implizit auch das
Leeren eines Feldes ab (Bug 25), dessen Sonderfall dadurch überflüssig
wurde und entfernt ist.

Nebenbei: ExpertViewModelTests.swift lief bisher gar nicht mit, weil
nach dem Anlegen der Datei kein "xcodegen generate" lief (Xcodegen
erzeugt die Sources-Dateiliste einmalig beim Generieren). Nach erneutem
Generate 58 Unit-Tests grün.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YDmUd93KxsYGr2kLTotWnG
This commit is contained in:
Kay
2026-09-15 13:42:45 +02:00
co-authored by Claude Sonnet 5
parent 58c9ed5481
commit f25d02af17
4 changed files with 81 additions and 30 deletions
@@ -150,13 +150,22 @@ final class ExpertViewModel: ObservableObject {
/// actually executes, consistent with the rest of the app never applying silently.
var pendingCommand: RouterOSCommand? {
guard let schema = selectedSchema, let editingItem else { return nil }
// Keep a curated field even when now empty if it previously held a value RouterOS'
// `set` only touches parameters it's given, so simply omitting a cleared field (the
// naive "drop empty values" rule) leaves the old value in place instead of clearing it.
// Confirmed live (2026-09-15): deleting a comment's text and saving left the old comment
// on the router. Still drop fields that were already empty/unset, same as before no
// point sending e.g. an untouched optional field as "" on every save.
var arguments = formValues.filter { !$0.value.isEmpty || !(editingItem.fields[$0.key] ?? "").isEmpty }
let isNew = editingItem.id.isEmpty
// When editing an existing item, only send a field if its value actually differs from
// what the router originally reported RouterOS' `set` only touches parameters it's
// given, so an unchanged field doesn't need resending, and for some fields resending the
// existing value verbatim is actively rejected: a dynamic/connected route's `distance=0`
// ("value of distance out of range (1...255)") and a route's read-only `immediate-gw`
// ("bad parameter immediate-gw") both failed this way when saving an edit to a completely
// different field like the comment (confirmed live, 2026-09-15). Diffing against the
// original also naturally covers *clearing* a field (new value "" differs from the old
// non-empty one, so it's still sent explicitly, as "" unlike a field that was already
// empty and stays empty, which is correctly left out). New items have no "original" to
// diff against, so every non-empty curated value is sent as before.
var arguments: [String: String] = isNew
? formValues.filter { !$0.value.isEmpty }
: formValues.filter { $0.value != (editingItem.fields[$0.key] ?? "") }
// RouterOS' CLI parser rejects "true"/"false" for boolean parameters it only accepts
// "yes"/"no" (confirmed live: "disabled=false" on "/interface vlan add" produced
// "syntax error (line 1 column 30)"; "disabled=no" succeeded). Schema defaults are
@@ -167,20 +176,14 @@ final class ExpertViewModel: ObservableObject {
if value == "true" { arguments[field.key] = "yes" }
else if value == "false" { arguments[field.key] = "no" }
}
// Only resend an uncurated ("Weitere Parameter") field if the user actually changed it
// (or added a brand-new one) RouterOS' `print`/REST GET returns some fields that are
// computed/read-only and rejected on `set` (confirmed live: a route's "immediate-gw",
// "bad parameter immediate-gw"). Since `startEditing` pre-fills every uncurated field
// from the live item for visibility, blindly resending all of them on every save even
// to change one unrelated curated field like a comment resent that computed value
// unchanged and broke the whole edit. Comparing against `editingItem.fields` (the
// untouched original) tells "user touched this" apart from "just showing what's there".
// Same "only if changed" rule for uncurated ("Weitere Parameter") fields these are
// pre-filled from the live item for visibility by `startEditing`, so without this every
// save would resend all of them, including computed/read-only ones RouterOS refuses.
for extra in extraFields where !extra.key.isEmpty {
if extra.value != editingItem.fields[extra.key] {
arguments[extra.key] = extra.value
}
}
let isNew = editingItem.id.isEmpty
let summary = "\(isNew ? "Neu anlegen" : "Ändern") unter \(schema.menuPath)"
if isNew {
return .add(menuPath: schema.menuPath, restPath: schema.restPath, arguments: arguments, summary: summary)