Skip to content

Commit

Permalink
Merge pull request #1790 from cardstack/cs-7463-self-referential-link…
Browse files Browse the repository at this point in the history
…s-to-relationships-break-a-card

Fix relative URL calculation when URL is the same as the `relativeTo`
  • Loading branch information
habdelra authored Nov 13, 2024
2 parents 6fb73c0 + 6f8bd68 commit a7d4a87
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 3 deletions.
101 changes: 101 additions & 0 deletions packages/host/tests/integration/realm-indexing-and-querying-test.gts
Original file line number Diff line number Diff line change
Expand Up @@ -2833,6 +2833,107 @@ module(`Integration | realm indexing and querying`, function (hooks) {
}
});

test('can index a card that has a linksTo relationship to itself', async function (assert) {
let { realm, adapter } = await setupIntegrationTestRealm({
loader,
contents: {
'Friend/hassan.json': {
data: {
id: `${testRealmURL}Friend/hassan`,
attributes: {
firstName: 'Hassan',
description: 'Dog owner',
},
relationships: {
friend: {
links: {
self: `${testRealmURL}Friend/hassan`,
},
},
},
meta: {
adoptsFrom: {
module: 'http://localhost:4202/test/friend',
name: 'Friend',
},
},
},
},
},
});
let indexer = realm.realmIndexQueryEngine;
let hassan = await indexer.cardDocument(
new URL(`${testRealmURL}Friend/hassan`),
{
loadLinks: true,
},
);
if (hassan?.type === 'doc') {
assert.deepEqual(hassan.doc, {
data: {
id: `${testRealmURL}Friend/hassan`,
type: 'card',
links: { self: `${testRealmURL}Friend/hassan` },
attributes: {
firstName: 'Hassan',
title: 'Hassan',
description: 'Dog owner',
thumbnailURL: null,
},
relationships: {
friend: {
links: {
self: `./hassan`,
},
data: {
type: 'card',
id: `${testRealmURL}Friend/hassan`,
},
},
},
meta: {
adoptsFrom: {
module: 'http://localhost:4202/test/friend',
name: 'Friend',
},
lastModified: adapter.lastModifiedMap.get(
`${testRealmURL}Friend/hassan.json`,
),
realmInfo: testRealmInfo,
realmURL: 'http://test-realm/test/',
resourceCreatedAt: adapter.resourceCreatedAtMap.get(
`${testRealmURL}Friend/hassan.json`,
),
},
},
});
} else {
assert.ok(false, `search entry was an error: ${hassan?.error.detail}`);
}

let hassanEntry = await getInstance(
realm,
new URL(`${testRealmURL}Friend/hassan`),
);
if (hassanEntry) {
assert.deepEqual(hassanEntry.searchDoc, {
_cardType: 'Friend',
id: `${testRealmURL}Friend/hassan`,
firstName: 'Hassan',
description: 'Dog owner',
friend: {
id: `${testRealmURL}Friend/hassan`,
},
title: 'Hassan',
});
} else {
assert.ok(
false,
`could not find ${testRealmURL}Friend/hassan in the index`,
);
}
});

test('can index a field with a cycle in the linksToMany field', async function (assert) {
let hassanID = `${testRealmURL}Friends/hassan`;
let mangoID = `${testRealmURL}Friends/mango`;
Expand Down
9 changes: 6 additions & 3 deletions packages/runtime-common/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,22 @@ export function relativeURL(
return url.pathname;
}

let lastPart: string | undefined;
while (
ourParts[0] === theirParts[0] &&
ourParts.length > 0 &&
theirParts.length > 0
) {
ourParts.shift();
lastPart = ourParts.shift();
theirParts.shift();
}
if (theirParts.length > 1) {
theirParts.shift();
return [...theirParts.map(() => '..'), ...ourParts].join('/');
let relative = [...theirParts.map(() => '..'), ...ourParts].join('/');
return relative === '.' && lastPart ? `./${lastPart}` : relative;
} else {
return ['.', ...ourParts].join('/');
let relative = ['.', ...ourParts].join('/');
return relative === '.' && lastPart ? `./${lastPart}` : relative;
}
}

Expand Down

0 comments on commit a7d4a87

Please sign in to comment.