Skip to content
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

Trying to reset an embedded object to an invalid one updates the original with default values if ex. is caught #6355

Open
elle-j opened this issue Jan 5, 2024 · 0 comments

Comments

@elle-j
Copy link
Contributor

elle-j commented Jan 5, 2024

If resetting an embedded object to an object with e.g. a missing required property, the SDK expectedly throws. If this exception is caught within the write transaction by the user, the original embedded object will still be updated but with default values.

Since our call into Core's obj.create_and_set_linked_object() is made prior to setting the user-provided values, the values on the embedded object have been set to default values.

Example test (this test has already been set up by PR #6356):

// Create a valid object with a valid embedded object (`address`).
const amy = this.realm.write(() => {
  return this.realm.create(PersonWithEmbeddedSchema.name, {
    name: "Amy",
    age: 30,
    address: { street: "Broadway" },
  });
});
expect(this.realm.objects(PersonWithEmbeddedSchema.name).length).equals(1);
expect(amy.address?.street).equals("Broadway");

// Update the embedded object with an invalid one.
this.realm.write(() => {
  // It is important to catch the exception within `realm.write()` in order to test
  // that the object creation path does not modify the object (rather than being due
  // to `realm.write()` cancelling the transaction).
  expect(() => {
    const invalidEmbeddedAddress = {};
    // @ts-expect-error Testing setting invalid type.
    amy.address = invalidEmbeddedAddress;
  }).to.throw("Missing value for property 'street'");
});
const objects = this.realm.objects<IPersonWithEmbedded>(PersonWithEmbeddedSchema.name);
expect(objects.length).equals(1);
expect(objects[0].address).to.not.be.null;
// 💥 Fails: `street` will be the default value (empty string).
expect(objects[0].address?.street).equals("Broadway");
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

No branches or pull requests

1 participant