When using ?populate=1, ?populate=2, etc. when querying a collection with collectionlink fields, the populate query value also doubles as a max depth parameter. However, the index is off by 1, meaning that ?populate=1 will populate 2 depths and ?populate=0 will not populate anything meaning that it is currently impossible to populate only a depth of 1.
The issue is most apparent when you have a cyclical relationship between two collections. When you enable populate, it will automatically fill A->B->A instead of just A->B
Before
?populate=0 gives: {"a": {link}}
not possible: {"a": {"b": {link}}}
?populate=1 gives: {"a": {"b": {"a": {link}}}}
After
?populate=0 gives: {"a": {link}}
?populate=1 gives: {"a": {"b": {link}}}
?populate=2 gives: {"a": {"b": {"a": {link}}}}
What this PR does:
It updates the logic of the populate function so that the value that the user passes actually aligns with the depth that they want to populate
Additionally, this also fixes infinite recursion for cyclical collections when ?populate=-1 by tracking the _id and collection values when populating.
Fixes: #1450
What is this about?
When using
?populate=1
,?populate=2
, etc. when querying a collection with collectionlink fields, the populate query value also doubles as a max depth parameter. However, the index is off by 1, meaning that?populate=1
will populate 2 depths and?populate=0
will not populate anything meaning that it is currently impossible to populate only a depth of 1.The issue is most apparent when you have a cyclical relationship between two collections. When you enable
populate
, it will automatically fillA->B->A
instead of justA->B
Before
?populate=0
gives:{"a": {link}}
{"a": {"b": {link}}}
?populate=1
gives:{"a": {"b": {"a": {link}}}}
After
?populate=0
gives:{"a": {link}}
?populate=1
gives:{"a": {"b": {link}}}
?populate=2
gives:{"a": {"b": {"a": {link}}}}
What this PR does:
It updates the logic of the populate function so that the value that the user passes actually aligns with the depth that they want to populate
Additionally, this also fixes infinite recursion for cyclical collections when
?populate=-1
by tracking the_id
andcollection
values when populating.Example Outputs using this PR:
Relationships:
Example Setup
Setup collections:
Setup entries:
sample
docker-compose.yml