Skip to content

fix: append scalars when Set targets array index beyond length#286

Closed
Solaris-star wants to merge 1 commit into
buger:masterfrom
Solaris-star:fix/267-set-array-index
Closed

fix: append scalars when Set targets array index beyond length#286
Solaris-star wants to merge 1 commit into
buger:masterfrom
Solaris-star:fix/267-set-array-index

Conversation

@Solaris-star

Copy link
Copy Markdown

Summary

Set with successive array indexes on a newly created path overwrote the whole array instead of appending:

Set({}, 1, "test", "[0]") // {"test":[1]}
Set(..., 2, "test", "[1]") // was {"test":[2]}, want {"test":[1,2]}

Root cause: when extending an existing non-empty array, only arrays whose next token was { were treated as appendable. Scalar arrays took the overwrite branch.

Fix

Treat any non-empty array as extendable when the next key is an index ([n]), matching object-append behavior and issue #267.

Test plan

  • go test -run TestSet .
  • TestSetAppendsScalarToExistingArrayByIndex
  • updated supplemental coverage expectation to append (not overwrite)

Fixes #267

@buger

buger commented Jul 27, 2026

Copy link
Copy Markdown
Owner

Closing as already-fixed — thank you for the fix, @Solaris-star.

The same root cause (Set on an array-index path beyond the current length silently overwriting instead of appending) was fixed in #283 as SYS-REQ-110, with three additional dimensions your PR didn't cover:

  1. Scalar arrays — your fix handled the array-of-objects case (data[startOffset+1+nextToken(...)] == '{'); fix(parser): guard Delete leading-comma panic + empty-string key path components; L3 strict proof review #283 also fixes scalar arrays ([1,2,3]), which fell through to the overwrite path. Set({"a":[1,2,3]}, 99, "a", "[9]") now correctly returns {"a":[1,2,3,99]} instead of {"a":[99]} (silent data loss).
  2. Cross-type auto-coerceSet({}, 9, "[5]") previously produced malformed JSON ({[9]}); fix(parser): guard Delete leading-comma panic + empty-string key path components; L3 strict proof review #283 auto-coerces the container type so the output is always valid JSON ([9]). See KI-3 / DEFECT-260726-MFPA.
  3. Top-level arraysSet([1,2,3], 9, "[5]") still returns KeyPathNotFoundError; tracked as KI-4 (medium) pending a design call on whether top-level append is in scope.

Regression coverage: set_spec_test.go:TestSetAutoCoerce_KI3 + TestSetArrayIndexUnderObjectMalformedJSON_KI3 + the structure-aware fuzzer's output-validity gate. Filed as DEFECT-260727-WWWY.

Appreciate the contribution — the original issue (#267) was the right call, and your fix pointed at the correct code region.

@buger buger closed this Jul 27, 2026
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.

Set with index overrides always first position of the array

2 participants