Skip to content

Fix snapshotProcessor.is #1495

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2020

Conversation

hovsater
Copy link
Contributor

No description provided.

Copy link
Member

@mweststrate mweststrate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change itself looks fine, but can you explain why the existing test needed updating? It now changes it's behavior and no longer converts a string to a number.

Also, can you introduce a new test that hit's and confirms the change in this PR?

@hovsater
Copy link
Contributor Author

hovsater commented Apr 2, 2020

@mweststrate thank you for the prompt reply, appreciate it. 🙂

The existing test was updated to reflect the change I made in this pull request. Without the change, the test will fail. In addition, the test is still valid for the behavior it was introduced by so I don't think an additional test is needed here.

@hovsater hovsater requested a review from mweststrate April 2, 2020 20:28
@mweststrate
Copy link
Member

mweststrate commented Apr 2, 2020

Ok, then I don't get your change. The test is a valid case: where a string x was converted to a number x, and you changed it in totally something else (instead of changing a type, changing an attribute name, which is a valid case as well, but a different one). If that original test failed with your change, it means that you are introducing a breaking change?

Your original issue with the code sandbox had a nice test case, where a model instance wasn't properly recognized as being assignable to a snapshot type.

So what I would expect to see: the current test to be unchanged (or an explanation why that test was wrong all along). And a new test that demonstrates that model instances are now indeed correctly recognized as instances of that snapshot type.

Hope that makes sense!

@hovsater
Copy link
Contributor Author

hovsater commented Apr 3, 2020

Let me try to explain a bit more clearly.

The intent of the original test case was to check that is on the snapshotProcessor type clearly identified instances of itself and unprocessed objects correctly. The fact that we converted a string representing a number to a number was just an implementation detail. It could have been something else entirely.

The test passed but it was accidental.

In the following expectation

expect(ProcessedModel.is(processedModel)).toBe(true)

we're passing the model instance to .is. This instance was wrongly passed through the preProcessor step to finally emit { x: 1 } again. It still made the test pass because x exists on the model instance (x is a number in this case).

So in the step:

{ ...sn, x: Number(sn.x) }

sn isn't a plain object but a model instance, and x isn't a string but a number.


I changed the implementation of the preProcessor step do something that's not idempotent, i.e., we cannot run the model instance through the preProcessor without first running it through the postProcessor. Now, the test fail (correctly so) because the original implementation was passing the model instance (already pre-processed) to the preProcessor step again.

The implementation was then changed to check if thing isStateTreeNode and if it is, grab a snapshot of itself instead of running it through the preProcessor step.

It should not be a breaking change since the only difference is using getSnapshot instead of the preProcessor for state tree nodes.

Hopefully this made it somewhat more clear. 🙂

@hovsater
Copy link
Contributor Author

hovsater commented Apr 7, 2020

@mweststrate did that clear things up or do you still want me to introduce a separate test case? 🙂

@mweststrate
Copy link
Member

Nope, thanks for the explanation and apologies for the late follow up! If you need more changes like this in the future and are interested in maintainer rights, let me know :) It allows you to branch directly on this repo and that is a much more convenient workflow.

I'll try to release this today :)

@mweststrate mweststrate merged commit 17f477a into mobxjs:master Apr 15, 2020
@mweststrate
Copy link
Member

Released in 3.16.0

airhorns added a commit to airhorns/mobx-state-tree that referenced this pull request Jun 4, 2024
…cessor.is override

This fixes a bug where within `.is` on models with snapshot processors, we validated against the model snapshot, instead of the model node when given a node. This means that two different models with compatible snapshots would not `.is` instances of each other normally, but once a snapshot processor is added, they would. This adds a failing test capturing this case.

The PR that introduced this behaviour of validating the snapshot was introduced here: mobxjs#1495, and I think was actually targeting a different use case -- passing *snapshots* to `.is`, not passing instances. The behaviour that PR added was to validate against the __processed__ version of a snapshot if passed a snapshot, which this PR maintains. But, if passed an instance, that PR elected to snapshot it as well, which I don't think is necessary, and breaks the substitutability of snapshot processed instances with their unwrapped counterparts.
airhorns added a commit to airhorns/mobx-state-tree that referenced this pull request Jun 4, 2024
…cessor.is override

This fixes a bug where within `.is` on models with snapshot processors, we validated against the model snapshot, instead of the model node when given a node. This means that two different models with compatible snapshots would not `.is` instances of each other normally, but once a snapshot processor is added, they would. This adds a failing test capturing this case.

The PR that introduced this behaviour of validating the snapshot was introduced here: mobxjs#1495, and I think was actually targeting a different use case -- passing *snapshots* to `.is`, not passing instances. The behaviour that PR added was to validate against the __processed__ version of a snapshot if passed a snapshot, which this PR maintains. But, if passed an instance, that PR elected to snapshot it as well, which I don't think is necessary, and breaks the substitutability of snapshot processed instances with their unwrapped counterparts.
coolsoftwaretyler added a commit that referenced this pull request Jul 12, 2024
…ocessor.is` override (#2182)

* Validate state tree instances instead of snapshots in the SnapshotProcessor.is override

This fixes a bug where within `.is` on models with snapshot processors, we validated against the model snapshot, instead of the model node when given a node. This means that two different models with compatible snapshots would not `.is` instances of each other normally, but once a snapshot processor is added, they would. This adds a failing test capturing this case.

The PR that introduced this behaviour of validating the snapshot was introduced here: #1495, and I think was actually targeting a different use case -- passing *snapshots* to `.is`, not passing instances. The behaviour that PR added was to validate against the __processed__ version of a snapshot if passed a snapshot, which this PR maintains. But, if passed an instance, that PR elected to snapshot it as well, which I don't think is necessary, and breaks the substitutability of snapshot processed instances with their unwrapped counterparts.

* docs: bump JSdocon snapshotProcessor is

* docs: one more link for snapshotProcessor

* chore: version bump to 7.0.0-pre.1

---------

Co-authored-by: Tyler Scott Williams <tyler@coolsoftware.dev>
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.

2 participants