Merge pull request #13331 from strapi/fix/deep-populate-components

Remove limit on deep populate on components
This commit is contained in:
Alexandre BODIN 2022-05-31 15:57:55 +02:00 committed by GitHub
commit c006f74a4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,6 @@
'use strict';
const { assoc, has, prop, omit } = require('lodash/fp');
const { assoc, has, prop, omit, merge } = require('lodash/fp');
const strapiUtils = require('@strapi/utils');
const { ApplicationError } = require('@strapi/utils').errors;
@ -40,15 +40,11 @@ const findCreatorRoles = entity => {
};
// TODO: define when we use this one vs basic populate
const getDeepPopulate = (uid, populate, depth = 0) => {
const getDeepPopulate = (uid, populate) => {
if (populate) {
return populate;
}
if (depth > 2) {
return {};
}
const { attributes } = strapi.getModel(uid);
return Object.keys(attributes).reduce((populateAcc, attributeName) => {
@ -60,7 +56,7 @@ const getDeepPopulate = (uid, populate, depth = 0) => {
if (attribute.type === 'component') {
populateAcc[attributeName] = {
populate: getDeepPopulate(attribute.component, null, depth + 1),
populate: getDeepPopulate(attribute.component, null),
};
}
@ -71,7 +67,7 @@ const getDeepPopulate = (uid, populate, depth = 0) => {
if (attribute.type === 'dynamiczone') {
populateAcc[attributeName] = {
populate: (attribute.components || []).reduce((acc, componentUID) => {
return Object.assign(acc, getDeepPopulate(componentUID, null, depth + 1));
return merge(acc, getDeepPopulate(componentUID, null));
}, {}),
};
}