Skip to content

Commit

Permalink
Bug in mapTo on Realm.List (#6269)
Browse files Browse the repository at this point in the history
* Test of mapTo on Realm.List
* Respect public name too
* Changelog entry
  • Loading branch information
kneth committed Nov 24, 2023
1 parent f3159ab commit 0e6a215
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
* None

### Fixed
* <How to hit and notice issue? what was the impact?> ([#????](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
Expand Down
50 changes: 50 additions & 0 deletions integration-tests/tests/src/tests/alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ type ObjectA = {
age?: number;
};

type Person = {
_name: string;
address: string;
age: number;
_married: boolean;
_children: Realm.List<Person>;
};

function addTestObjects(realm: Realm) {
realm.write(() => {
realm.create("ObjectA", {
Expand All @@ -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",
},
],
});
});
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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>("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) {
Expand Down
10 changes: 10 additions & 0 deletions packages/babel-plugin/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Person>; }`,
});
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";
Expand Down
2 changes: 1 addition & 1 deletion packages/realm/src/PropertyHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 0e6a215

Please sign in to comment.