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

RangeError: Invalid String Length at JSON.stringify() #422

Closed
jmikrut opened this issue Nov 17, 2021 · 5 comments
Closed

RangeError: Invalid String Length at JSON.stringify() #422

jmikrut opened this issue Nov 17, 2021 · 5 comments
Assignees
Labels

Comments

@jmikrut
Copy link

jmikrut commented Nov 17, 2021

First off—thank you all very much for the work you do in this package. We're attempting to use it in Payload CMS in an effort to auto-generate types from our users' configs.

We've been working with a proof of concept, testing through a few different projects here and there.

Some Payload configs can be moderately large when converted to a JSON schema: 10-15K lines or so. We're seeing that when configs near that size, compiling JSON to TS with this package just crashes with the following error:

(node:18811) UnhandledPromiseRejectionWarning: RangeError: Invalid string length
    at JSON.stringify (<anonymous>)
    at stringify (/www/payload/node_modules/json-stringify-safe/stringify.js:5:15)
    at /www/payload/node_modules/json-schema-to-typescript/dist/src/optimizer.js:34:204

I believe this error comes from JS running out of memory. We have attempted turning off format as the docs instruct, but this did not help anything.

Here is an example of a decently large JSON schema that is causing the error above.

Looking at the stack trace, line 34 of optimizer.js looks to have to do with unions. In the example schema, we have a lot of unions, many generated from a single Payload "field" called link. You can see an example of the JSON schema for a link field on line 44 of the example above.

Is there a potential performance enhancement or bug here?

As it stands we can't compile the schema linked above.

Any ideas?

@jmikrut
Copy link
Author

jmikrut commented Nov 17, 2021

This might actually end up being a duplicate of #323

@jmikrut
Copy link
Author

jmikrut commented Nov 17, 2021

Update: I simply commented out the de-duplication measures that happens within the INTERSECTION and UNION optimization.ts and this has completely fixed my performance issues. Took me from ~120 seconds to compile down to 5.

Types look great—nice and succinct. In my case, I have control elsewhere over avoiding duplicates within intersections and unions. Could we create an option that allows for this package to skip that optimization?

@bcherny
Copy link
Owner

bcherny commented May 15, 2022

Sorry for the delay!

Thanks for the patch, but this is a bug that we should fix, and not something that consumers should be able to bail out of to avoid hitting the bug.

Root cause fix, which will be included in the next JSTT release: #447.

bcherny added a commit that referenced this issue May 15, 2022
Optimize optimizer to avoid deep JSON serialization (fix #422)
@bcherny
Copy link
Owner

bcherny commented Jun 29, 2022

Published as part of v11.0.0.

@jmikrut
Copy link
Author

jmikrut commented Jun 29, 2022

Beautiful @bcherny — thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants