@@ -4,7 +4,8 @@ import type { UID } from "@strapi/strapi";
44const queryCache : Partial < Record < UID . Schema , any > > = { } ;
55
66export const getPopulateQuery = (
7- modelUid : UID . Schema
7+ modelUid : UID . Schema ,
8+ parentsModelUids : UID . Schema [ ] = [ ]
89) : { populate : object | true } | undefined => {
910 try {
1011 // return cached query
@@ -13,6 +14,16 @@ export const getPopulateQuery = (
1314 return queryCache [ modelUid ] ;
1415 }
1516
17+ // prevent infinite loop
18+ if ( parentsModelUids . includes ( modelUid ) ) {
19+ strapi . log . debug (
20+ `[populate-all] loop detected skipping population: ${ modelUid } `
21+ ) ;
22+ return { populate : { } } ;
23+ } else {
24+ parentsModelUids . push ( modelUid ) ;
25+ }
26+
1627 // build query
1728 const query = { populate : { } } ;
1829 const model = strapi . getModel ( modelUid ) ;
@@ -31,7 +42,7 @@ export const getPopulateQuery = (
3142 const components = Object . fromEntries (
3243 attribute . components . map ( ( component ) => [
3344 component ,
34- getPopulateQuery ( component ) ,
45+ getPopulateQuery ( component , parentsModelUids ) ,
3546 ] )
3647 ) ;
3748 query . populate [ fieldName ] = { on : components } ;
@@ -40,7 +51,10 @@ export const getPopulateQuery = (
4051
4152 // component
4253 if ( attribute . type === "component" ) {
43- query . populate [ fieldName ] = getPopulateQuery ( attribute . component ) ;
54+ query . populate [ fieldName ] = getPopulateQuery (
55+ attribute . component ,
56+ parentsModelUids
57+ ) ;
4458 continue ;
4559 }
4660
@@ -57,14 +71,20 @@ export const getPopulateQuery = (
5771 . config < boolean | string [ ] > ( "relations" ) ;
5872
5973 if ( relations === true ) {
60- // @ts -expect-error target actually exists on attribute
61- query . populate [ fieldName ] = getPopulateQuery ( attribute . target ) ;
74+ query . populate [ fieldName ] = getPopulateQuery (
75+ // @ts -expect-error target actually exists on attribute
76+ attribute . target ,
77+ parentsModelUids
78+ ) ;
6279 continue ;
6380 }
6481 // @ts -expect-error target actually exists on attribute
6582 if ( Array . isArray ( relations ) && relations . includes ( attribute . target ) ) {
66- // @ts -expect-error target actually exists on attribute
67- query . populate [ fieldName ] = getPopulateQuery ( attribute . target ) ;
83+ query . populate [ fieldName ] = getPopulateQuery (
84+ // @ts -expect-error target actually exists on attribute
85+ attribute . target ,
86+ parentsModelUids
87+ ) ;
6888 continue ;
6989 }
7090 }
0 commit comments