mobx-state-tree
mobx-state-tree copied to clipboard
applySnapshot does not call preProcessSnapshot when using types.snapshotProcessor
Bug report
- [v] I've checked documentation and searched for existing issues
- [v] I've made sure my project is based on the latest MST version
- [v] Fork this code sandbox or another minimal reproduction.
Sandbox link or minimal reproduction code https://codesandbox.io/embed/mobx-state-tree-todolist-1o4pm
Describe the expected behavior applySnapshot will call preProcessSnapshot and postProcessSnapshot
Describe the observed behavior applySnapshot called only postProcessSnapshot
Any update on this?
Sorry, didn't have time personally yet to dive into this. But as the label suggests, feel free to chime in :). A PR with unit tests is already helpful.
Anybody found a workout around for this?
Re-opening, as there are issues with #1823 and I reverted it before releasing.
Until this is fixed it should be mentioned in the docs that snapshot preprocessing is fundamentally broken and shouldn't be used.
I started to take a look at this, but I haven't figured it out. I'm using the test from @Jarrku:
test("applySnapshot runs preprocessors before applying - simple", () => {
const M = types.snapshotProcessor(types.model({ x: types.number }), {
preProcessor(snapshot: { x: string }) {
return { x: Number(snapshot.x) }
},
postProcessor(snapshot) {
return { x: String(snapshot.x) }
}
})
const m = M.create({ x: "1" })
expect(() => applySnapshot(m, { x: "2" })).not.toThrow()
})
when applySnapshot
is called, applySnapshotPreProcessor is called (in src/types/complex-types/model.ts
)
however, when it is, this.preProcessor
is not set / is undefined
.
It seems that when SnapshotProcessor
is constructed, the _subtype
is not given .preProcessor
and .postProcessor
attributes.
When I tried to set them I got type errors.
I haven't been able to understand the inheritance model to the level of understanding how to fix the issue.
Thanks @evinosheaforward - I know you and I are talking offline about this but wanted to acknowledge your comment publicly so folks know we are collaborating on the fix together.
When you get a chance, if you have more specific questions about the details, ask 'em here so we can sort of document-in-public!