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

Escape semicolons #148

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions stream_framework/serializers/aggregated_activity_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from stream_framework.utils import epoch_to_datetime, datetime_to_epoch
from stream_framework.serializers.base import BaseAggregatedSerializer
import six
import re


class AggregatedActivitySerializer(BaseAggregatedSerializer):
Expand All @@ -23,7 +24,6 @@ class AggregatedActivitySerializer(BaseAggregatedSerializer):
#: indicates if dumps returns dehydrated aggregated activities
dehydrate = True
identifier = 'v3'
reserved_characters = [';', ',', ';;']
date_fields = ['created_at', 'updated_at', 'seen_at', 'read_at']

activity_serializer_class = ActivitySerializer
Expand Down Expand Up @@ -55,7 +55,8 @@ def dumps(self, aggregated):
else:
for activity in aggregated.activities:
serialized = activity_serializer.dumps(activity)
check_reserved(serialized, [';', ';;'])
# we use semicolons as delimiter, so need to escape
serialized = serialized.replace(";", "\;")
serialized_activities.append(serialized)

serialized_activities_part = ';'.join(serialized_activities)
Expand Down Expand Up @@ -86,15 +87,20 @@ def loads(self, serialized_aggregated):
date_value = epoch_to_datetime(float(v))
setattr(aggregated, k, date_value)

# looks for ; not \;
unescaped_semicolons_regex = re.compile("(?<=[^\\\]);")
# write the activities
serializations = parts[5].split(';')
serializations = unescaped_semicolons_regex.split(parts[5])
if self.dehydrate:
activity_ids = list(map(int, serializations))
aggregated._activity_ids = activity_ids
aggregated.dehydrated = True
else:
activities = [activity_serializer.loads(s)
for s in serializations]
activities = []
for s in serializations:
s = s.replace("\;", ";")
deserialized = activity_serializer.loads(s)
activities.append(deserialized)
aggregated.activities = activities
aggregated.dehydrated = False

Expand Down
2 changes: 1 addition & 1 deletion stream_framework/tests/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ActivitySerializationTest(unittest.TestCase):
serialization_class = BaseSerializer
serialization_class_kwargs = {
'activity_class': Activity, 'aggregated_activity_class': AggregatedActivity}
activity_extra_context = {'xxx': 'yyy'}
activity_extra_context = {'xxx': 'yyy', 'with_semicolons': 'foo;bar;;'}

def setUp(self):
from stream_framework.verbs.base import Love as LoveVerb
Expand Down