Skip to content

Commit

Permalink
fix: pass emulated entry to getCollection filter function (#12875)
Browse files Browse the repository at this point in the history
* fix: pass emulated entry to getCollection filter function

* Add test
  • Loading branch information
ascorbic authored Jan 2, 2025
1 parent ac00c64 commit e109002
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-baboons-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes a bug in emulated legacy collections where the entry passed to the getCollection filter function did not include the legacy entry fields.
18 changes: 11 additions & 7 deletions packages/astro/src/content/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,20 @@ export function createGetCollection({
for (const rawEntry of store.values<DataEntry>(collection)) {
const data = updateImageReferencesInData(rawEntry.data, rawEntry.filePath, imageAssetMap);

const entry = {
let entry = {
...rawEntry,
data,
collection,
};

if (entry.legacyId) {
entry = emulateLegacyEntry(entry);
}

if (hasFilter && !filter(entry)) {
continue;
}
result.push(entry.legacyId ? emulateLegacyEntry(entry) : entry);
result.push(entry);
}
return result;
} else {
Expand Down Expand Up @@ -272,19 +277,18 @@ type DataEntryResult = {

type EntryLookupObject = { collection: string; id: string } | { collection: string; slug: string };

function emulateLegacyEntry(entry: DataEntry) {
function emulateLegacyEntry({ legacyId, ...entry }: DataEntry & { collection: string }) {
// Define this first so it's in scope for the render function
const legacyEntry = {
...entry,
id: entry.legacyId!,
id: legacyId!,
slug: entry.id,
};
delete legacyEntry.legacyId;
return {
...legacyEntry,
// Define separately so the render function isn't included in the object passed to `renderEntry()`
render: () => renderEntry(legacyEntry),
};
} as ContentEntryResult;
}

export function createGetEntry({
Expand Down Expand Up @@ -334,7 +338,7 @@ export function createGetEntry({
const { default: imageAssetMap } = await import('astro:asset-imports');
entry.data = updateImageReferencesInData(entry.data, entry.filePath, imageAssetMap);
if (entry.legacyId) {
return { ...emulateLegacyEntry(entry), collection } as ContentEntryResult;
return emulateLegacyEntry({ ...entry, collection });
}
return {
...entry,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@ export async function GET() {
const withUnionSchema = stripAllRenderFn(await getCollection('with-union-schema'));
const withSymlinkedContent = stripAllRenderFn(await getCollection('with-symlinked-content'));
const withSymlinkedData = stripAllRenderFn(await getCollection('with-symlinked-data'));
const filtered = stripAllRenderFn(await getCollection('without-config', (entry) => entry.slug));

return new Response(
devalue.stringify({ withoutConfig, withSchemaConfig, withSlugConfig, withUnionSchema, withSymlinkedContent, withSymlinkedData }),
devalue.stringify({
withoutConfig,
withSchemaConfig,
withSlugConfig,
withUnionSchema,
withSymlinkedContent,
withSymlinkedData,
filtered,
}),
);
}
6 changes: 6 additions & 0 deletions packages/astro/test/legacy-content-collections.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ describe('Legacy Content Collections', () => {
);
});

it('Passes legacy entry to filter function', async () => {
assert.ok(json.hasOwnProperty('filtered'));
assert.ok(Array.isArray(json.filtered));
assert.ok(json.filtered.length > 0);
});

it('Returns `with schema` collection', async () => {
assert.ok(json.hasOwnProperty('withSchemaConfig'));
assert.equal(Array.isArray(json.withSchemaConfig), true);
Expand Down

0 comments on commit e109002

Please sign in to comment.