Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 [RUMF-1410] Allow serialization of objects with cyclic references #1783

Merged
merged 5 commits into from
Oct 26, 2022

Conversation

yannickadam
Copy link
Contributor

Motivation

Fix for #807

Changes

Added a replacer within the jsonStringify function that removes cyclic references while serializing.

Testing

  • Local
  • Staging
  • Unit
  • End to end

I have gone over the contributing documentation.

@yannickadam yannickadam requested a review from a team as a code owner October 20, 2022 23:23
@bits-bot
Copy link

bits-bot commented Oct 20, 2022

CLA assistant check
All committers have signed the CLA.

@yannickadam yannickadam force-pushed the yannick/rumf-1410-serialize-cyclic-object branch 2 times, most recently from 2000bb1 to 14cd826 Compare October 20, 2022 23:37
@codecov-commenter
Copy link

codecov-commenter commented Oct 20, 2022

Codecov Report

Merging #1783 (146da85) into main (90e29bd) will increase coverage by 0.17%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main    #1783      +/-   ##
==========================================
+ Coverage   90.92%   91.10%   +0.17%     
==========================================
  Files         129      129              
  Lines        5038     5037       -1     
  Branches     1133     1132       -1     
==========================================
+ Hits         4581     4589       +8     
+ Misses        457      448       -9     
Impacted Files Coverage Δ
...kages/core/src/domain/console/consoleObservable.ts 100.00% <100.00%> (ø)
packages/core/src/tools/utils.ts 85.37% <100.00%> (+2.69%) ⬆️
packages/core/src/transport/batch.ts 94.18% <0.00%> (+1.16%) ⬆️
packages/core/src/tools/timeUtils.ts 100.00% <0.00%> (+2.56%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@yannickadam yannickadam force-pushed the yannick/rumf-1410-serialize-cyclic-object branch from 14cd826 to c56f53f Compare October 21, 2022 06:21
Copy link
Contributor

@bcaudan bcaudan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Comment on lines 172 to 177
const isValidObject = (v: any): v is object => typeof v === 'object' && v !== null
const getCyclicReplacer = <T>() => {
// Using a weakmap instead of a weakset to support IE11
const visited = new WeakMap<object, boolean>()
return (_k: string, v: T) => {
if (isValidObject(v)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥜 nitpick: ‏using a meaningful word instead of a letter could be easier to follow, something like candidate or value instead of v.

@amortemousque
Copy link
Contributor

Looks great!

// subclasses.
const restoreObjectPrototypeToJson = detachToJsonMethod(Object.prototype)
const restoreArrayPrototypeToJson = detachToJsonMethod(Array.prototype)
const restoreValuePrototypeToJson = detachToJsonMethod(Object.getPrototypeOf(value))
const restoreValueToJson = detachToJsonMethod(value)

// Line below can be removed when migrating to TS4.8+
const isValidObject = (v: any): v is object => typeof v === 'object' && v !== null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💬 suggestion: ‏use getType(destination) === 'object' instead

// subclasses.
const restoreObjectPrototypeToJson = detachToJsonMethod(Object.prototype)
const restoreArrayPrototypeToJson = detachToJsonMethod(Array.prototype)
const restoreValuePrototypeToJson = detachToJsonMethod(Object.getPrototypeOf(value))
const restoreValueToJson = detachToJsonMethod(value)

// Line below can be removed when migrating to TS4.8+
const isValidObject = (v: any): v is object => typeof v === 'object' && v !== null
const getCyclicReplacer = <T>() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥜 nitpick: ‏the type parameter T isn't useful here, and could be replaced by unknown in the argument list

🥜 nitpick: ‏FMU you create a closure just to keep the circularReferenceChecker variable private. IMO this is not worth it and the following would be a bit easier to read:

const circularReferenceChecker = createCircularReferenceChecker()
const cyclicReplacer = (_key: string, value: unknown) => {
  if (isValidObject(value) && circularReferenceChecker.hasAlreadyBeenSeen(value)) {
    return '<warning: cyclic reference not serialized>'
  }
  return value
}

}
const array: any[] = []
// Using a weakmap instead of a weakset to support IE11
const map: WeakMap<any, boolean> = new WeakMap()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏 praise: ‏great, love it :)

@yannickadam
Copy link
Contributor Author

/to-staging

@temporal-github-worker-1
Copy link

🚂 MergeFlow

Merge branch is merge-1783-to-staging-44

@temporal-github-worker-1
Copy link

🚂 MergeFlow

Checks have failed on ab758f7!
Details: gitlab pipeline not succeed link
You can use this resolution PR: #1794 to fix the errors if they are specific to the current state of the staging branch.

@yannickadam yannickadam force-pushed the yannick/rumf-1410-serialize-cyclic-object branch from 5b254b4 to 146da85 Compare October 25, 2022 18:56
@yannickadam yannickadam merged commit 41e3dfa into main Oct 26, 2022
@yannickadam yannickadam deleted the yannick/rumf-1410-serialize-cyclic-object branch October 26, 2022 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants