From 0e6a215a69999c208f3f280e4ab53e673052961e Mon Sep 17 00:00:00 2001 From: Kenneth Geisshirt Date: Fri, 24 Nov 2023 16:49:42 +0100 Subject: [PATCH] Bug in mapTo on Realm.List (#6269) * Test of mapTo on Realm.List * Respect public name too * Changelog entry --- CHANGELOG.md | 3 +- integration-tests/tests/src/tests/alias.ts | 50 ++++++++++++++++++++++ packages/babel-plugin/src/index.test.ts | 10 +++++ packages/realm/src/PropertyHelpers.ts | 2 +- 4 files changed, 62 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 423800d55f..e5f804c7f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,8 +7,7 @@ * None ### Fixed -* ([#????](https://github.com/realm/realm-js/issues/????), since v?.?.?) -* None +* When mapTo is used on a property of type List, an error like `Property 'test_list' does not exist on 'Task' objects` occurs when trying to access the property. ([#6268](https://github.com/realm/realm-js/issues/6268), since v12.0.0) ### Compatibility * React Native >= v0.71.4 diff --git a/integration-tests/tests/src/tests/alias.ts b/integration-tests/tests/src/tests/alias.ts index e47f407650..ea235a10a9 100644 --- a/integration-tests/tests/src/tests/alias.ts +++ b/integration-tests/tests/src/tests/alias.ts @@ -25,6 +25,14 @@ type ObjectA = { age?: number; }; +type Person = { + _name: string; + address: string; + age: number; + _married: boolean; + _children: Realm.List; +}; + function addTestObjects(realm: Realm) { realm.write(() => { realm.create("ObjectA", { @@ -35,6 +43,20 @@ function addTestObjects(realm: Realm) { otherName: "Bar", age: 42, }); + + realm.create("Person", { + _name: "Donald", + address: "Elm Street", + age: 42.0, + _married: false, + _children: [ + { + _name: "Nancy", + age: 14.0, + address: "Elm Street", + }, + ], + }); }); } @@ -93,6 +115,23 @@ describe("Aliasing property names using mapTo", () => { // Using the internal name instead of the alias throws an exception. expect(() => realm.create("ObjectA", { name: "Boom" })).to.throw(); }); + + // Create objects with links - must use alias + realm.write(() => { + realm.create("Person", { + _name: "Donald", + address: "Elm Street", + age: 42.0, + _married: false, + _children: [ + { + _name: "Nancy", + age: 14.0, + address: "Elm Street", + }, + ], + }); + }); }); it("supports updating objects", function (this: Mocha.Context & RealmContext) { @@ -130,6 +169,17 @@ describe("Aliasing property names using mapTo", () => { for (const key in obj) { expect(key).not.equals("name"); } + + const donald = realm.objects("Person")[0] as Person; + expect(donald).not.equals(undefined); + + // @ts-expect-error This should be undefined + expect(donald.name).equals(undefined); + expect(donald._name).equals("Donald"); + + // @ts-expect-error This should be undefined + expect(donald.children).equals(undefined); + expect(donald._children.length).equals(1); }); it("supports aliases in queries", function (this: Mocha.Context & RealmContext) { diff --git a/packages/babel-plugin/src/index.test.ts b/packages/babel-plugin/src/index.test.ts index 5a2abc65cb..8cbdcca9a6 100644 --- a/packages/babel-plugin/src/index.test.ts +++ b/packages/babel-plugin/src/index.test.ts @@ -440,6 +440,16 @@ describe("Babel plugin", () => { expect((parsedSchema?.properties.name as PropertySchema).mapTo).toEqual("rename"); }); + it("handles `@mapTo` decorators on Realm.List", () => { + const transformCode = transform({ + source: `import Realm, { Types, BSON, List, Set, Dictionary, Mixed } from "realm"; + export class Person extends Realm.Object { @Realm.mapTo('rename') name: Realm.Types.List; }`, + }); + const parsedSchema = extractSchema(transformCode); + + expect((parsedSchema?.properties.name as PropertySchema).mapTo).toEqual("rename"); + }); + it("ignores `@mapTo` decorators not imported from `realm`", () => { const transformCode = transform({ source: `import Realm, { Types, BSON, List, Set, Dictionary, Mixed } from "realm"; diff --git a/packages/realm/src/PropertyHelpers.ts b/packages/realm/src/PropertyHelpers.ts index acff62d91f..3b4ce67e48 100644 --- a/packages/realm/src/PropertyHelpers.ts +++ b/packages/realm/src/PropertyHelpers.ts @@ -334,7 +334,7 @@ export function createPropertyHelpers(property: PropertyContext, options: Helper const collectionType = property.type & binding.PropertyType.Collection; const typeOptions: TypeOptions = { realm: options.realm, - name: property.name, + name: property.publicName || property.name, getClassHelpers: options.getClassHelpers, objectType: property.objectType, objectSchemaName: property.objectSchemaName,