Skip to content

Bug: applying snapshot getted by getSnapshot to union of models with different optional properties throws error #961

@codewylle

Description

@codewylle

Applying snapshot getted by getSnapshot to union of models with different optional properties throws error:

import { types, getSnapshot, applySnapshot } from "../src"

test("apply snapshot to union should not throw when union keeps models with different properties and snapshot getted by getSnapshot", () => {
    const Foo = types.model({ foo: 1 })
    const Bar = types.model({ bar: 1 })
    const U = types.union(Foo, Bar)

    const u = U.create({ foo: 1 })
    applySnapshot(u, getSnapshot(Bar.create())) // THROW error
})

When union type validates snapshot, it validates all subtypes until one of them fits to snapshot. But validation of model type can add property to snapshot if value of optional model property is absent in snapshot (is it normal?):

import { types } from "../src"

test("model creating should not change snapshot", () => {
    const M = types.model({ foo: 1 })
    const o = {}

    M.create(o)
    expect(o).toEqual({}) // o is { foo: 1 }
})

getSnapshot returns frozen object. When mst tries to add property to frozen object in validation of model it throws error Cannot add property *, object is not extensible

Metadata

Metadata

Assignees

Labels

bugConfirmed bughas 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