Repository navigation
Conversation
Entries whose link fields point at each other (A -> B -> A, or an entry linking to itself) were converted to arrays forever, because ArrayableLink::toArray() returns the full augmented array of the linked entry and AugmentedCollection::toArray() evaluates it. Track the linked items that are currently being expanded and only fall back to the shallow array when a link leads back to one of them. The output is unchanged for links that don't form a cycle. Refs statamic#9105
Member
|
This seems to fix the same problem as #15288 🤔 |
Author
Thanks for flagging @duncanmcclean. I do think these differ though: #15288 guards cycles in an entry’s origin (translations) at save time, while this PR fixes link fields that point back to each other (valid content) while converting an entry to an array/JSON in I ran both on current 6.x: with only #15288, 4 of the 5 tests in |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request fixes an infinite recursion when entries have link fields that lead back to each other (A → B → A, a longer cycle, or an entry linking to itself) and the entry is converted to an array or JSON, e.g. via
Entry::toArray(),json_encode($entry)or{{ entry | to_json }}.Fixes #9105. Also reported in #14696.
Cause
AugmentedCollection::toArray()evaluates nested arrays with (new self($value))->withEvaluation()->toArray(). When it meets anArrayableLinkit callsArrayableLink::toArray(), which returnstoAugmentedArray()of the linked entry: the full augmented entry, including its own links. If one of those links leads back to an entry that is already being expanded, it never ends.Fix
AugmentedCollection keeps track of the linked items that are currently being expanded. A link that leads back to one of them is represented by the shallow array (what
ArrayableLink::jsonSerialize()already returns), all other links are expanded in full as before.This is the approach suggested in #15386, which was closed because always using the shallow array would change the output for links that don't form a cycle. With the guard, the output is unchanged for everyone without a cycle. The tracking state is removed in a finally block, so an exception during evaluation doesn't leave anything behind.
Tests
tests/Data/Entries/CyclicLinkTest.phpconverts real entries to arrays:How to test
Automated:
vendor/bin/phpunit tests/Data/Entries/CyclicLinkTest.phpTo see the tests fail without the fix, restore
src/Data/AugmentedCollection.phpto its state on 6.x (git checkout 6.x -- src/Data/AugmentedCollection.php) and run the same command: four of the five tests fail with "Links are being augmented recursively.".Manually:
{{ collection:pages as="pages" }}{{ pages | to_json }}{{ /collection:pages }}in a template, orjson_encode(Entry::find($id))in tinker.Before this change the request runs until PHP's time or memory limit is hit, after this change it returns, with the link that leads back to an entry that is already being expanded as its shallow array. I also ran this change against a real-world site where the REST API ran into this (a page linking to an event with a taxonomy terms field, which brought the cycle back to the page): the recursion no longer occurs.