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

Circular References on i18n default export cause JSON.stringify(i18n) to throw. #1322

Closed
elInfidel opened this issue May 21, 2021 · 6 comments

Comments

@elInfidel
Copy link

🐛 Bug Report

The i18n object has circular references causing JSON.stringify to fail. This breaks error
tracking solutions that rely on JSON.stringify to build data about exceptions that may be throwing
from i18n.

To Reproduce

import i18n from "i18next";
JSON.stringify(i18n);

Expected behavior

JSON.stringify should not throw.

Your Environment

running the following to patch this in my own project.

i18n["toJSON"] = () => `{ "i18n": "Function removed, see i18n.ts for more details." }`;
@elInfidel elInfidel changed the title Circular Dependancies on i18n default export cause toJSON to throw. Circular Dependancies on i18n default export cause JSON.stringify(i18n) to throw. May 21, 2021
@elInfidel elInfidel changed the title Circular Dependancies on i18n default export cause JSON.stringify(i18n) to throw. Circular References on i18n default export cause JSON.stringify(i18n) to throw. May 21, 2021
@adrai
Copy link
Member

adrai commented May 21, 2021

Why should someone want to stringify i18next?

@elInfidel
Copy link
Author

elInfidel commented May 22, 2021

Why should someone want to stringify i18next?

Error tracking solutions like Sentry and New Relic use stringify when encountering exceptions. I'd follow that question up by posing my own. Why expose a toJSON function on i18next.

While this is easily patched around I've seen similar issues raised against the behaviour. The existence of toJSON exposes this edge case unnecessarily I would argue. At the least, toJSON shouldn't be outputting fields that reference back.

#440

@jamuhl
Copy link
Member

jamuhl commented May 22, 2021

That does not reply the question. i18next is a class not a data object and therefore is not serizable. why should it be stringified if it can‘t be parsed back.

@adrai
Copy link
Member

adrai commented May 22, 2021

At most we could do something like that:
image
But I still don't get the use case. 🤷‍♂️

@elInfidel
Copy link
Author

But I still don't get the use case. 🤷‍♂️

To explain my specific use case, we use Sentry in one of our applications, it is encountering an exception that includes i18n in the event data. Sentry uses JSON.stringify() to serialize the event and send it up to their server so we can process errors and squash bugs. When sentry hits i18n it throws an exception which results in the data sent to the server being about the circular dependency, not the actual error that caused Sentry to get involved in the first place.

While my situation was unfortunate, it's not that big of a deal, it can always be worked around.

That does not reply the question. i18next is a class not a data object and therefore is not serizable. why should it be stringified if it can‘t be parsed back.

Agreed, it's a class. It's not serializable and in an ideal world no one should be trying to serialize it. What purpose is toJSON actually serving on this object? I would argue that toJSON should only return fields that are serializable.

As per MDN documentation for stringify
"If the value has a toJSON() method, it's responsible to define what data will be serialized."

I mostly just wanted to raise a discussion around the value of the toJSON function here. Can anyone define a use case for toJSON that doesn't involve some sort of serialization/transmission. Is there value in adhering to the expectations of the specification for stringify in your eyes.

@adrai
Copy link
Member

adrai commented May 22, 2021

I don't know Sentry, but if it really tries to JSON.stringify everything it is a mess I can imagine...
just releasing a new i18next version v20.3.0

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

No branches or pull requests

3 participants