fix: handle circular references in denormalize function#44
fix: handle circular references in denormalize function#44garrettg123 wants to merge 1 commit intoklis87:masterfrom
Conversation
Prevents infinite loops when denormalizing data with circular references by tracking seen entity references during traversal. - Added seenRefs parameter to track visited entities - Returns reference string when circular reference detected - Includes comprehensive test suite for various circular scenarios Fixes issue where entities referencing each other would cause stack overflow
|
Thank you so much for the PR. I am glad you were able to patch it yourself, but also I would like to put such fixes into the core I will try to analyze the problem by running your tests and understanding the failure first, I might have some questions then, because usually we do not call Anyway, I cannot wait to analyze it, but for your info, first I need to finish #1 (already finished, just need to work on documentation and improving types) - I will highly appreciate your feedback, cause I remember you use Normy for quite a while :) Generally it will allow us to have full declarative data updates, even for arrays. I also added custom array operations, so it should handle any possible case.
I am always super happy when I hear that Normy is used in production :) I wonder, do you think we could add a section to readme like |
|
I analyzed the PR, thank you again for creating it. However, before I can merge it, I need to understand the root cause, because you typically should not call const denormalizedData = {
id: '1',
bestFrield: {
id: '2',
bestFriend: {
id: '1',
},
},
}Notice, that this is recursive, but because this is response from api, if is not recursive infinitely. The leaf node has just Then, once normalized, this object structure will be saved in Perhaps you use sth like If you have yet another case I did not think about, please give me the full picture, as |
Problem
The
denormalizefunction causes infinite loops and stack overflow errors when processing data with circular references. This is a critical issue that can crash applications.Example scenario:
Solution
Added circular reference detection by tracking visited entity references during traversal.
Changes (only 2 files):
seenRefsparameter to track visited referencesImplementation:
Testing
Added test coverage for:
Breaking Changes
None - the new parameter has a default value, maintaining backward compatibility.
Real-world Impact
We discovered this in production at Pulse where bidirectional relationships were causing crashes. This fix has been successfully running in our patched version.
Thank you for maintaining this excellent library! We rely heavily on normy for our React Query data management.