Skip to content

fix: handle JsonArray nodes in setAtPath and deleteAtPath (RFC 6901 integer-index traversal) - #31

Open
himanshubakshi wants to merge 1 commit into
Contextable:mainfrom
himanshubakshi:hbakshi/fix/array-traversal-set-delete
Open

himanshubakshi wants to merge 1 commit into
Contextable:mainfrom
himanshubakshi:hbakshi/fix/array-traversal-set-delete

Conversation

@himanshubakshi

Copy link
Copy Markdown

Closes #30

Summary

Two related bugs in DataModel where JsonArray nodes at intermediate path segments were silently mishandled:

1. setAtPath — silent data loss on indexed writes

Any JsonArray at an intermediate path node fell into the else branch, replacing it with an empty JsonObject and discarding the entire array. No exception, no log.

// Before — JsonArray hits else, array silently replaced
val child = when (existing) {
    is JsonObject -> setAtPath(existing, remaining, value)
    else -> setAtPath(JsonObject(emptyMap()), remaining, value)
}

2. deleteAtPath — silent no-op on indexed deletes

The same blind spot: deleteAtPath only recursed into JsonObject. A JsonArray at an intermediate node caused the delete to silently do nothing.

Both bugs share the same root cause: missing JsonArray branch in the respective when blocks. getAtPath already handles arrays correctly (integer-index traversal via obj.getOrNull(index)) — this PR brings setAtPath and deleteAtPath to parity.

Fix

Add a JsonArray branch to both functions, delegating to new setAtPathInArray and deleteAtPathInArray helpers:

  • setAtPathInArray: integer-index write with JsonNull padding when index >= size, enabling writes at absolute indices beyond the current array bounds. Non-numeric segment → no-op (protocol error guard).
  • deleteAtPathInArray: integer-index removal. Out-of-bounds index and non-numeric segment → no-op. Recurses into nested JsonObject elements for deeper paths.
  • Both helpers include a defensive isEmpty() guard, mirroring getAtPath's existing pattern.

RFC 6901 reference

RFC 6901 §4 defines integer-index traversal for arrays:

"the reference token MUST contain … digits … that represent an unsigned base-10 integer value, making the new referenced value the array element with the zero-based index identified by the token"

Tests

18 new tests in DataModelTest covering both helpers:

setAtPathInArray (8 tests): append at absolute index, element preservation, JsonNull padding beyond bounds, overwrite existing element, non-numeric segment guard, sparse index gaps, sequential multi-page appends, full-array replace after appends, nested path write into existing element, nested path write into null slot, primitive-at-intermediate-node regression.

deleteAtPathInArray (8 tests): remove middle/first/last element, out-of-bounds index, non-numeric segment, nested field delete inside element, drain array to empty, delete through object→array path, non-object array element guard.

Test plan

  • :a2ui-4k:jvmTest — BUILD SUCCESSFUL, all tests pass

…nteger-index traversal)

Both setAtPath and deleteAtPath only handled JsonObject at intermediate path
nodes. Any JsonArray at an intermediate segment fell into the else branch,
silently clobbering it with an empty JsonObject (set) or leaving it unchanged
(delete) — no exception, no log.

Fixes the silent-clobber bug tracked in issue Contextable#30.

- setAtPath: add JsonArray branch → setAtPathInArray (integer-index write with
  JsonNull padding for out-of-bounds indices)
- deleteAtPath: add JsonArray branch → deleteAtPathInArray (integer-index
  removal; out-of-bounds and non-numeric segments are no-ops)
- Both helpers: defensive isEmpty() guard mirrors getAtPath's existing pattern
- 18 new tests covering: append, preserve, pad, overwrite, non-numeric guard,
  sparse gaps, sequential appends, full replace, nested writes, drain-to-empty,
  delete first/middle/last, OOB, nested field delete, primitive-intermediate

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DataModel.setAtPath silently discards JsonArray nodes on intermediate path segments — indexed writes fail

1 participant