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

Update fromAttribute for Arrays and Objects when no valid value is set #723

Closed
wants to merge 1 commit into from

Conversation

f-santos
Copy link

@f-santos f-santos commented Jul 2, 2019

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

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.
@googlebot
Copy link

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. I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@f-santos
Copy link
Author

f-santos commented Jul 2, 2019

I signed it!

@googlebot
Copy link

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

Copy link
Contributor

@justinfagnani justinfagnani left a 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!);
Copy link
Contributor

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().

@felipeplets
Copy link

@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

@justinfagnani
Copy link
Contributor

In Lit 2 we catch all JSON parse exceptions.

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

Successfully merging this pull request may close these issues.

Uncaught SyntaxError: Unexpected token g in JSON at position 0
4 participants