Skip to content

Fix decoder state restoration after decoding child - #18

Open
hippi345 wants to merge 1 commit into
apple:mainfrom
hippi345:fix/decoder-restore-current-value-3
Open

hippi345 wants to merge 1 commit into
apple:mainfrom
hippi345:fix/decoder-restore-current-value-3

Conversation

@hippi345

Copy link
Copy Markdown

Fix decoder state restoration after decoding child

Type of change

  • Bug fix
  • xcprojformatter (CLI) change
  • Documentation update
  • Test addition
  • Other (describe below)

Summary

Fixes a bug in XCJSON.Decoder.decode(_:pathComponent:) where the decoder incorrectly saved the incoming value parameter instead of its own currentValue before decoding a child element. This caused the decoder to lose track of the parent context after child decoding completed.

Related issue

Fixes #3

Changes

  • Changed Decoder.swift line 33 from let oldCurrentValue = value to let oldCurrentValue = currentValue
  • Added DecoderStateTests.swift with a regression test that verifies the decoder properly restores parent state after decoding a child

Testing

  • Existing tests pass (swift test)
  • New tests added for new functionality
  • Tested manually:
    1. Added the reproducer test from issue [Bug]: Decoder does not restore the parent value after decoding a child #3 to Sources/Tests/DecoderStateTests.swift
    2. Ran swift test --filter restoresParentValueAfterDecodingChild before the fix - it failed with "Expected an instance of dictionary but an instance string was specified"
    3. Applied the one-line fix to Decoder.swift
    4. Ran the test again - it passed
    5. Ran full test suite (swift test) - all 32 tests passed

Checklist

  • I have read CONTRIBUTING.md
  • My change is in scope (see CONTRIBUTING.md)
  • Code follows the project's style guidelines
  • I have reviewed my own code and can explain all changes in detail
  • Documentation updated where applicable

Additional notes

This contribution was prepared with AI assistance (Claude via Cursor Cloud Agent). The fix itself is a single-line change that corrects the variable being captured in the defer block. The test case was provided in the original issue report and validates that after decoding a child value, the decoder's currentNodeType remains .object and the parent's keyed container can be reopened.

The decoder's decode(_:pathComponent:) method was incorrectly saving
the incoming value parameter instead of the decoder's currentValue
before decoding a child. This caused the parent context to be lost
after child decoding completed.

Changed to save and restore currentValue, ensuring the decoder's
state is properly maintained across nested decode operations.

Fixes apple#3
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.

[Bug]: Decoder does not restore the parent value after decoding a child

1 participant