mobx-state-tree icon indicating copy to clipboard operation
mobx-state-tree copied to clipboard

applySnapshot does not call preProcessSnapshot when using types.snapshotProcessor

Open khomyakov42 opened this issue 5 years ago • 9 comments

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

khomyakov42 avatar Jun 14 '19 03:06 khomyakov42

Any update on this?

mario-jerkovic avatar Jan 07 '20 12:01 mario-jerkovic

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.

mweststrate avatar Jan 07 '20 12:01 mweststrate

Anybody found a workout around for this?

Aryk avatar Apr 23 '21 02:04 Aryk

Re-opening, as there are issues with #1823 and I reverted it before releasing.

jamonholmgren avatar Mar 13 '23 17:03 jamonholmgren

Until this is fixed it should be mentioned in the docs that snapshot preprocessing is fundamentally broken and shouldn't be used.

jcready avatar Jun 19 '23 13:06 jcready

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.

evinosheaforward avatar Apr 21 '24 00:04 evinosheaforward

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!

coolsoftwaretyler avatar Apr 22 '24 15:04 coolsoftwaretyler