From 94e7c706604bb9422c0ca5abc05c1d4237b85ab6 Mon Sep 17 00:00:00 2001 From: chk97 <36267076+ChrisKru97@users.noreply.github.com> Date: Mon, 5 Jun 2023 13:30:55 +0000 Subject: [PATCH] fix: self-recursion --- server/bootstrap.js | 2 +- server/helpers/index.js | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/server/bootstrap.js b/server/bootstrap.js index 6d8a0e9..cb357c6 100644 --- a/server/bootstrap.js +++ b/server/bootstrap.js @@ -10,7 +10,7 @@ module.exports = ({ strapi }) => { if (populate && populate[0] === 'deep') { const depth = populate[1] ?? defaultDepth - const modelObject = getFullPopulateObject(event.model.uid, depth); + const modelObject = getFullPopulateObject(event.model.uid, depth, []); event.params.populate = modelObject.populate } } diff --git a/server/helpers/index.js b/server/helpers/index.js index fac6201..7f026b8 100644 --- a/server/helpers/index.js +++ b/server/helpers/index.js @@ -9,7 +9,7 @@ const getModelPopulationAttributes = (model) => { return model.attributes; }; -const getFullPopulateObject = (modelUid, maxDepth = 20) => { +const getFullPopulateObject = (modelUid, maxDepth = 20, ignore) => { const skipCreatorFields = strapi.plugin('strapi-plugin-populate-deep')?.config('skipCreatorFields'); if (maxDepth <= 1) { @@ -21,9 +21,11 @@ const getFullPopulateObject = (modelUid, maxDepth = 20) => { const populate = {}; const model = strapi.getModel(modelUid); + if (ignore && !ignore.includes(model.collectionName)) ignore.push(model.collectionName) for (const [key, value] of Object.entries( getModelPopulationAttributes(model) )) { + if (ignore?.includes(key)) continue if (value) { if (value.type === "component") { populate[key] = getFullPopulateObject(value.component, maxDepth - 1); @@ -36,7 +38,8 @@ const getFullPopulateObject = (modelUid, maxDepth = 20) => { } else if (value.type === "relation") { const relationPopulate = getFullPopulateObject( value.target, - (key === 'localizations') && maxDepth > 2 ? 1 : maxDepth - 1 + (key === 'localizations') && maxDepth > 2 ? 1 : maxDepth - 1, + ignore ); if (relationPopulate) { populate[key] = relationPopulate;