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

Disallowed NaN and Infinity values in JSONs sent to the Apify API #87

Merged
merged 1 commit into from
Dec 6, 2022

Conversation

fnesveda
Copy link
Member

@fnesveda fnesveda commented Dec 1, 2022

If you tried to push an item which contained a NaN to a dataset, it would return an error from the Apify API, that the JSON you sent to it is invalid.

>>> client.dataset('~nan-testing-dataset').push_items([{ 'a': float('NaN')}])
ApifyApiError: Cannot parse POST payload as JSON: Unexpected token N in JSON at position 7

That is because in Python, by default, json.dumps() stringifies NaN and Infinity values. This is against the JSON standard, and JSON.parse on the API fails to parse the value.

This fixes it by adding the allow_nan=False parameter to json.dumps(), so that the error is caught before sending the invalid JSON to the API, and throws an error early.

>>> client.dataset('~nan-testing-dataset').push_items([{ 'a': float('NaN')}])
ValueError: Out of range float values are not JSON compliant: nan

By the way, when fixing this, I made my first fix to CPython: python/cpython#99926

@fnesveda fnesveda added bug Something isn't working. adhoc Ad-hoc unplanned task added during the sprint. labels Dec 1, 2022
@fnesveda fnesveda added this to the 52nd sprint - Platform team milestone Dec 1, 2022
@fnesveda fnesveda self-assigned this Dec 1, 2022
@github-actions github-actions bot added the t-platform Issues with this label are in the ownership of the platform team. label Dec 1, 2022
@fnesveda fnesveda merged commit dbf483f into master Dec 6, 2022
@fnesveda fnesveda deleted the fix/disallow-nans branch December 6, 2022 15:16
@fnesveda fnesveda added the validated Issues that are resolved and their solutions fulfill the acceptance criteria. label May 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
adhoc Ad-hoc unplanned task added during the sprint. bug Something isn't working. t-platform Issues with this label are in the ownership of the platform team. validated Issues that are resolved and their solutions fulfill the acceptance criteria.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants