Add test + update logic

This commit is contained in:
Convly 2022-11-21 11:02:38 +01:00
parent 981c918dcc
commit 7c1837bbe1
3 changed files with 73 additions and 5 deletions

View File

@ -490,7 +490,7 @@ const morphToMany = async (input, ctx) => {
const qb = db.entityManager.createQueryBuilder(type);
const rows = await qb
.init(on && type in on ? on[type] : typePopulate)
.init(on?.[type] ?? typePopulate)
.addSelect(`${qb.alias}.${idColumn.referencedColumn}`)
.where({ [idColumn.referencedColumn]: ids })
.execute({ mapResults: false });
@ -561,7 +561,7 @@ const morphToOne = async (input, ctx) => {
const qb = db.entityManager.createQueryBuilder(type);
const rows = await qb
.init(on && type in on ? on[type] : typePopulate)
.init(on?.[type] ?? typePopulate)
.addSelect(`${qb.alias}.${idColumn.referencedColumn}`)
.where({ [idColumn.referencedColumn]: ids })
.execute({ mapResults: false });

View File

@ -315,4 +315,64 @@ describe('Populate filters', () => {
expect(body.data[0].attributes.third).toBeUndefined();
});
});
describe('Populate a dynamic zone', () => {
test('Populate every components in the dynamic zone', async () => {
const qs = {
populate: {
dz: '*',
},
};
const { status, body } = await rq.get(`/${schemas.contentTypes.b.pluralName}`, { qs });
expect(status).toBe(200);
expect(body.data).toHaveLength(2);
expect(body.data[0].attributes.dz).toHaveLength(3);
expect(body.data[1].attributes.dz).toHaveLength(1);
});
test('Populate only one component type using fragment', async () => {
const qs = {
populate: {
dz: {
on: {
'default.foo': true,
},
},
},
};
const { status, body } = await rq.get(`/${schemas.contentTypes.b.pluralName}`, { qs });
expect(status).toBe(200);
expect(body.data).toHaveLength(2);
expect(body.data[0].attributes.dz).toHaveLength(2);
expect(body.data[1].attributes.dz).toHaveLength(0);
});
test('Populate the dynamic zone with filters in fragments', async () => {
const qs = {
populate: {
dz: {
on: {
'default.foo': {
filters: { number: { $lt: 2 } },
},
'default.bar': {
filters: { title: { $contains: 'another' } },
},
},
},
},
};
const { status, body } = await rq.get(`/${schemas.contentTypes.b.pluralName}`, { qs });
expect(status).toBe(200);
expect(body.data).toHaveLength(2);
expect(body.data[0].attributes.dz).toHaveLength(1);
expect(body.data[1].attributes.dz).toHaveLength(1);
});
});
});

View File

@ -186,7 +186,15 @@ const convertPopulateObject = (populate, schema) => {
return acc;
}
if (typeof subPopulate === 'object' && 'on' in subPopulate) {
// Allow adding a 'on' strategy to populate queries for polymorphic relations, media and dynamic zones
const isAllowedAttributeForFragmentPopulate =
attribute.type === 'dynamiczone' ||
attribute.type === 'media' ||
(attribute.relation && attribute.relation.startsWith('morphTo'));
const hasFragmentPopulateDefined = typeof subPopulate === 'object' && 'on' in subPopulate;
if (isAllowedAttributeForFragmentPopulate && hasFragmentPopulateDefined) {
return {
...acc,
[key]: {
@ -201,8 +209,8 @@ const convertPopulateObject = (populate, schema) => {
};
}
// TODO: Deprecated way of handling dynamic zone populate queries. It's kept as is,
// as removing it could break existing user queries but should be removed in V5.
// TODO: This is a query's populate fallback for DynamicZone and is kept for legacy purpose.
// Removing it could break existing user queries but it should be removed in V5.
if (attribute.type === 'dynamiczone') {
const populates = attribute.components
.map((uid) => strapi.getModel(uid))