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

Fixes encoding of circular referenced object / complex object #1525

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

DavidWeiss2
Copy link

@DavidWeiss2 DavidWeiss2 commented Feb 20, 2024

Description

This PR enhances Firebase functions v2 by fixing an issue related to encoding objects with self-references, which previously led to "Maximum call stack size exceeded" errors. The change is to throw a clear error on what is the error (not JSON serializable, with object ref in console).

For a practical demonstration, consider a class instance with self-referencing properties. The updated encoding function now throws Data cannot be encoded in JSON: [object: object] in such cases, preventing stack overflow errors and ensuring the encoded output reflects the object's structure accurately, without infinite recursion. This improvement is verified through comprehensive test case included in the PR.

It fixes #1527

Code sample

See test cases.
The code before the change will give "Maximum call stack size exceeded" even on the basic case:

    class TestClass {
      foo: string;
      bar: number;
      self: TestClass;
      constructor(foo: string, bar: number) {
        this.foo = foo;
        this.bar = bar;
        this.self = this;
      }
    }
    const testObject = new TestClass("hello", 1);
    expect(()=>https.encode(testObject)).to.throw.(`Data cannot be encoded in JSON: ${testObject}`);

@DavidWeiss2 DavidWeiss2 changed the title Add encoding tests And implementation for circular references Fixes #1527 - Add encoding tests And implementation for circular references Feb 20, 2024
@DavidWeiss2 DavidWeiss2 changed the title Fixes #1527 - Add encoding tests And implementation for circular references Fixes encoding of circular referenced object / complex object Feb 20, 2024
@inlined inlined self-requested a review March 13, 2024 03:49
@DavidWeiss2
Copy link
Author

Refactored to reflect team decision on throwing in the case of self ref.
#1527 (comment)
@inlined

@DavidWeiss2
Copy link
Author

@inlined Can we get update on this PR status?

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.

Firebase Functions Encoding Failure for Self-Referencing Objects Leads to Maximum call stack size exceeded
2 participants