Skip to content

Commit

Permalink
Fix nan values (#2665)
Browse files Browse the repository at this point in the history
* Fix nan values

* unused imports
  • Loading branch information
timgl committed Dec 4, 2020
1 parent ba2a32b commit d60799c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
4 changes: 3 additions & 1 deletion ee/clickhouse/models/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ def get_properties(self, event):
prop_vals = [res.strip('"') for res in event[9]]
return dict(zip(event[8], prop_vals))
else:
props = json.loads(event[2])
# parse_constants gets called for any NaN, Infinity etc values
# we just want those to be returned as None
props = json.loads(event[2], parse_constant=lambda x: None)
unpadded = {key: value.strip('"') if isinstance(value, str) else value for key, value in props.items()}
return unpadded

Expand Down
13 changes: 13 additions & 0 deletions posthog/api/test/test_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from urllib.parse import quote

import lzstring
import numpy as np
from django.utils import timezone
from freezegun import freeze_time

Expand Down Expand Up @@ -495,3 +496,15 @@ def test_incorrect_json(self):
"/capture/", '{"event": "incorrect json with trailing comma",}', content_type="application/json"
)
self.assertEqual(response.json()["code"], "validation")

@patch("posthog.api.capture.celery_app.send_task")
def test_nan(self, patch_process_event_with_plugins):
self.client.post(
"/track/",
data={
"data": json.dumps([{"event": "beep", "properties": {"distinct_id": np.nan}}]),
"api_key": self.team.api_token,
},
)
arguments = self._to_arguments(patch_process_event_with_plugins)
self.assertEqual(arguments["data"]["properties"]["distinct_id"], None)
5 changes: 4 additions & 1 deletion posthog/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,10 @@ def load_data_from_request(request):

# Is it plain json?
try:
data = json.loads(data)
# parse_constant gets called in case of NaN, Infinity etc
# default behaviour is to put those into the DB directly
# but we just want it to return None
data = json.loads(data, parse_constant=lambda x: None)
except json.JSONDecodeError:
# if not, it's probably base64 encoded from other libraries
data = base64_to_json(data)
Expand Down

0 comments on commit d60799c

Please sign in to comment.