mirror of
https://github.com/strapi/strapi.git
synced 2025-08-31 12:23:05 +00:00
Add a test for the wildcard populate & fix the query populate
This commit is contained in:
parent
773d6ff5ad
commit
16a9567c9e
@ -23,15 +23,59 @@ const schemas = {
|
||||
cover: { type: 'media' },
|
||||
},
|
||||
},
|
||||
b: {
|
||||
kind: 'collectionType',
|
||||
displayName: 'b',
|
||||
singularName: 'b',
|
||||
pluralName: 'bs',
|
||||
attributes: {
|
||||
name: { type: 'string' },
|
||||
number: { type: 'integer' },
|
||||
relA: { type: 'relation', relation: 'oneToOne', target: 'api::a.a' },
|
||||
cp: { type: 'component', repeatable: false, component: 'default.cp-a' },
|
||||
dz: { type: 'dynamiczone', components: ['default.cp-a', 'default.cp-b'] },
|
||||
img: { type: 'media', multiple: false },
|
||||
},
|
||||
},
|
||||
},
|
||||
components: {
|
||||
cpA: {
|
||||
displayName: 'cp-a',
|
||||
attributes: {
|
||||
name: {
|
||||
type: 'string',
|
||||
},
|
||||
},
|
||||
},
|
||||
cpB: {
|
||||
displayName: 'cp-b',
|
||||
attributes: {
|
||||
title: {
|
||||
type: 'string',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const getFixtures = (file) => {
|
||||
return [
|
||||
{
|
||||
cover: file.id,
|
||||
},
|
||||
];
|
||||
const fixtures = {
|
||||
a: (file) => [{ cover: file.id }],
|
||||
b:
|
||||
(file) =>
|
||||
({ a }) =>
|
||||
[
|
||||
{
|
||||
name: 'one',
|
||||
number: 1,
|
||||
relA: a[0].id,
|
||||
cp: { name: 'cp_one' },
|
||||
dz: [
|
||||
{ __component: 'default.cp-a', name: 'cp_two' },
|
||||
{ __component: 'default.cp-b', title: 'cp_three' },
|
||||
],
|
||||
img: file.id,
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const uploadFile = async () => {
|
||||
@ -56,8 +100,11 @@ describe('Sanitize populated entries', () => {
|
||||
const file = await uploadFile();
|
||||
|
||||
await builder
|
||||
.addComponent(schemas.components.cpA)
|
||||
.addComponent(schemas.components.cpB)
|
||||
.addContentTypes(Object.values(schemas.contentTypes))
|
||||
.addFixtures(schemas.contentTypes.a.singularName, getFixtures(file))
|
||||
.addFixtures(schemas.contentTypes.a.singularName, fixtures.a(file))
|
||||
.addFixtures(schemas.contentTypes.b.singularName, fixtures.b(file))
|
||||
.build();
|
||||
|
||||
strapi = await createStrapiInstance();
|
||||
@ -87,4 +134,21 @@ describe('Sanitize populated entries', () => {
|
||||
expect(body.data[0].attributes.cover.data.attributes.updatedBy).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Wildcard Populate', () => {
|
||||
test('Wildcard populate is transformed to ane exhaustive list of populatable fields', async () => {
|
||||
const findManyMock = jest.spyOn(strapi.entityService, 'findMany');
|
||||
|
||||
const { status } = await rq.get(`/${schemas.contentTypes.b.pluralName}`, {
|
||||
qs: { fields: ['id'], populate: '*' },
|
||||
});
|
||||
|
||||
expect(status).toBe(200);
|
||||
// Make sure the wildcard populate is transformed to an exhaustive list
|
||||
expect(findManyMock).toHaveBeenCalledWith(
|
||||
'api::b.b',
|
||||
expect.objectContaining({ populate: { relA: true, cp: true, dz: true, img: true } })
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -145,6 +145,10 @@ const populate = traverseFactory()
|
||||
})
|
||||
// Handle populate on relation
|
||||
.onRelation(async ({ key, value, attribute, visitor, path, schema }, { set, recurse }) => {
|
||||
if (isNil(value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isMorphToRelationalAttribute(attribute)) {
|
||||
// Don't traverse values that cannot be parsed
|
||||
if (!isObject(value) || !('on' in value && isObject(value?.on))) {
|
||||
@ -166,6 +170,10 @@ const populate = traverseFactory()
|
||||
})
|
||||
// Handle populate on media
|
||||
.onMedia(async ({ key, path, visitor, value }, { recurse, set }) => {
|
||||
if (isNil(value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const targetSchemaUID = 'plugin::upload.file';
|
||||
const targetSchema = strapi.getModel(targetSchemaUID);
|
||||
|
||||
@ -175,6 +183,10 @@ const populate = traverseFactory()
|
||||
})
|
||||
// Handle populate on components
|
||||
.onComponent(async ({ key, value, visitor, path, attribute }, { recurse, set }) => {
|
||||
if (isNil(value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const targetSchema = strapi.getModel(attribute.component);
|
||||
|
||||
const newValue = await recurse(visitor, { schema: targetSchema, path }, value);
|
||||
@ -183,6 +195,10 @@ const populate = traverseFactory()
|
||||
})
|
||||
// Handle populate on dynamic zones
|
||||
.onDynamicZone(async ({ key, value, attribute, schema, visitor, path }, { set, recurse }) => {
|
||||
if (isNil(value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (isObject(value)) {
|
||||
const { components } = attribute;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user