Skip to content

fix: revive nested values before the parse reviver runs - #110

Open
MFA-G wants to merge 2 commits into
WebReflection:mainfrom
MFA-G:fix/parse-reviver-nested-values
Open

MFA-G wants to merge 2 commits into
WebReflection:mainfrom
MFA-G:fix/parse-reviver-nested-values

Conversation

@MFA-G

@MFA-G MFA-G commented Nov 22, 2026

Copy link
Copy Markdown

Summary

Since 3.4.0, parse(text, reviver) calls the reviver for an object/array while its nested containers are still the internal ignore placeholder. A reviver that reads a nested value, or returns a new object built from value, sees {} and the revived children are later assigned to the discarded original instance.

Fixes #109.

Root cause

resolver replaces each not-yet-seen container with ignore and queues {o, k, r}. The queue loop then calls $.call(o, k, revive(r)), but revive(r) only fills in r's primitive keys, so its own containers are still ignore when the reviver runs.

Fix

Revive every queued container first, then walk the queue backwards and hand each container to the reviver. A child is always queued after its parent, so the reverse order assigns bottom-up without recursion, and every reviver receives fully revived nested values — the same thing JSON.parse guarantees.

The two loops do the same amount of work as before (one revive per queued container, one assignment per queued key), just in a different order. As already noted in #88, the reviver callback order may change; this change makes it closer to JSON.parse, where children are revived before their parent.

Tests

Added two cases to test/index.js:

  • a reviver that reads a nested value (Point(1,2) instead of Point(undefined,undefined));
  • a reviver that returns a new object, like log4js' LoggingEvent.deserialise (err.response.status === 500 instead of {}).

Both fail on main with the current assertions and pass with the patch.

Validation run locally:

  • npm run build — cjs/esm/es/min builds and the full test/index.js suite pass with 100% statement/branch coverage on index.js.
  • node test/recursion.js — passes.
  • 3000 random acyclic trees parsed with a copying/number-doubling reviver, compared against JSON.parse(JSON.stringify(tree), sameReviver) — 0 mismatches.

Since 3.4.0 the parse queue swaps every nested container for the shared
`ignore` placeholder and calls the reviver on the parent before those
placeholders are filled in, so a reviver that reads a nested value (or
builds a new object from it) sees empty objects and the revived children
are assigned to the discarded instance.

Revive every queued container first, then walk the queue backwards to
hand each container to the reviver. A child is always queued after its
parent, so the reverse order assigns bottom-up without recursion and
each reviver receives fully revived nested values, matching JSON.parse.

Fixes WebReflection#109
A container is queued before the containers it first references, which is
what makes the reverse walk safe; it is not a general parent-before-child
order for shared references.
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.

parse() reviver sees empty placeholders for nested values since 3.4.0

1 participant