-
Notifications
You must be signed in to change notification settings - Fork 319
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
Update fromAttribute for Arrays and Objects when no valid value is set #723
Conversation
Currently if attribute is an empty attribute for the type Array or Object Chrome will raise the error `Uncaught SyntaxError: Unexpected end of JSON input`. This happens because `JSON.parse('')` will be fired and empty string is not a valid JSON. My changes make an empty attribute to be converted to an empty array or an empty object what seems the expected behaviour in these cases not breaking internal methods.
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed (or fixed any issues), please reply here (e.g. What to do if you already signed the CLAIndividual signers
Corporate signers
ℹ️ Googlers: Go here for more info. |
I signed it! |
CLAs look good, thanks! ℹ️ Googlers: Go here for more info. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In addition to the code change, we'll need a test that fails without it, and an update to the changelog.
@@ -151,8 +151,9 @@ export const defaultConverter: ComplexAttributeConverter = { | |||
case Number: | |||
return value === null ? null : Number(value); | |||
case Object: | |||
return value === '' ? {} : JSON.parse(value!); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think rather than this, which wouldn't fix the demo linked in the issue, we want to put a try/catch around the JSON.parse()
.
@justinfagnani @f-santos is an old user of mine which was deleted, so feel free to close this PR and consider #888 to fix the issue |
In Lit 2 we catch all JSON parse exceptions. |
For better understanding on the issue please check #722
This happens because
JSON.parse('')
will be fired and empty string is not a valid JSON.My changes make an empty attribute to be converted to an empty array or an empty object what seems the expected behaviour in these cases not breaking internal methods.
Alternative solution
Change the default value to return
undefined
instead of the initial value as in my current solution.Reference Issue
Fixes #722