Skip to content

Commit

Permalink
fix(core): retrieve only ObjectIds when populate array is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
minenwerfer committed Aug 22, 2024
1 parent 2474504 commit 694c987
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-squids-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@aeriajs/core": patch
---

Retrieve only ObjectIds when `populate` array is empty
4 changes: 4 additions & 0 deletions packages/core/src/collection/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export const getReferences = async (properties: FixedObjectProperty['properties'
const description = throwIfError(await getCollectionAsset(refProperty.$ref, 'description'))

if( refProperty.populate ) {
if( refProperty.populate.length === 0 ) {
continue
}

const deepReferences = await getReferences(description.properties, {
depth: depth + 1,
maxDepth: refProperty.populateDepth || maxDepth,
Expand Down
4 changes: 4 additions & 0 deletions packages/core/tests/fixtures/aeriaMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ exports.default = init({
$id: 'project',
required: [],
properties: {
user_id: {
$ref: 'user',
populate: [],
},
created_by: {
$ref: 'person',
populate: [
Expand Down
1 change: 1 addition & 0 deletions packages/core/tests/fixtures/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const documents = (async () => {

const project = throwIfError(await insert({
what: {
user_id: user1,
created_by: person1,
stakeholders: {
owner: person1,
Expand Down
9 changes: 9 additions & 0 deletions packages/core/tests/reference.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,18 @@ test('populates top level references', async () => {
} = await documents

expect(person1.equals(project.created_by._id)).toBe(true)
expect(user1.equals(project.user_id)).toBe(true)
expect(user1.equals(project.created_by.user._id)).toBe(true)
expect(file1.equals(project.created_by.user.picture_file._id)).toBe(true)
})

test('respects the "populate" property', async () => {
const {
user1,
project,
} = await documents

expect(user1.equals(project.user_id)).toBe(true)
})

test('populates deep-nested references', async () => {
Expand Down

0 comments on commit 694c987

Please sign in to comment.