Skip to content

getSnapshot() doesn't traverse all nodes in the tree #926

@bourquep

Description

@bourquep

I have:

  • A conceptual question.
    • I've checked documentation and searched for existing issues
    • I tried the spectrum channel first
  • I think something is not working as it should.
    • I've checked documentation and searched for existing issues
    • I've made sure your project is based on the latest MST version
    • Fork this code sandbox or another minimal reproduction.
    • Describe expected behavior
    • Describe observed behavior
  • Feature request
    • Describe the feature and why you feel your feature needs to be generically solved inside MST
    • Are you willing to (attempt) a PR?

Not following the above template might result in your issue being closed without further notice


It seems like getSnapshot() is broken in v3.0.0.

Running this repro code:

const { types, getSnapshot } = require("mobx-state-tree");

const Nested = types.model("Nested", {
    foo: "bar",
    name: "",
}).postProcessSnapshot(snapshot => ({ ...snapshot, "fooz": "barz" }));

const Root = types.model("Root", {
    foo: "bar",
    name: "",
    nested: types.array(Nested)
});

const nested = Nested.create({});
const root = Root.create({nested: [nested]});

console.log("Snapshot of nested node includes 'fooz' attribute added in post-process:");
const nestedSnap = getSnapshot(nested);
console.dir(nestedSnap);

console.log();

console.log("But snapshot of root node does not include post-processed attribute:");
const rootSnap = getSnapshot(root);
console.dir(rootSnap.nested[0]);

Yields this output:

Snapshot of nested node includes 'fooz' attribute added in post-process:
{ foo: 'bar', name: '', fooz: 'barz' }

But snapshot of root node does not include post-processed attribute:
{ foo: [Getter/Setter], name: [Getter/Setter] }

Running the same code against MST v2.2.0 (after moving postProcessSnapshot() to an action) yields this output:

Snapshot of nested node includes 'fooz' attribute added in post-process:
{ foo: 'bar', name: '', fooz: 'barz' }

But snapshot of root node does not include post-processed attribute:
{ foo: 'bar', name: '', fooz: 'barz' }

As you can see, calling getSnapshot() on a root node does not process the nested node's snapshot.

Metadata

Metadata

Assignees

No one assigned

    Labels

    has PRA Pull Request to fix the issue is available

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions