Skip to content

Commit

Permalink
Fix items serialization when items is a dict
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Jul 18, 2018
1 parent 77113d2 commit b5e4890
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
15 changes: 10 additions & 5 deletions stripe/api_resources/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ def create(cls, **params):
return super(Subscription, cls).create(**params)

def serialize(self, previous):
updated_params = super(UpdateableAPIResource, self).serialize(previous)
if "items" in updated_params:
updated_params["items"] = util.convert_array_to_dict(
updated_params["items"])
return updated_params
if self._unsaved_values and "items" in self._unsaved_values:
self["items"] = util.convert_array_to_dict(self["items"])

# Ignore the previous values of `items` since it's likely the list
# object returned by the API and we want to treat `items` as a
# standalone parameter, not an update of the list object.
if self._previous and "items" in self._previous:
del self._previous["items"]

return super(UpdateableAPIResource, self).serialize(previous)
40 changes: 39 additions & 1 deletion tests/api_resources/test_subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,42 @@ def test_is_modifiable_with_items(self, request_mock):
# )
# assert isinstance(resource, stripe.Subscription)

# TODO: Test serialize
def test_serializes_when_items_is_a_list(self):
resource = stripe.Subscription.construct_from({
'object': 'subscription',
'items': {
'object': 'list',
'data': [],
}
}, stripe.api_key)
resource.items = [
{'id': 'si_foo', 'deleted': True},
{'plan': 'plan_bar'},
]
expected = {
'items': {
'0': {'id': 'si_foo', 'deleted': True},
'1': {'plan': 'plan_bar'},
}
}
self.assertEquals(expected, resource.serialize(None))

def test_serializes_when_items_is_a_dict(self):
resource = stripe.Subscription.construct_from({
'object': 'subscription',
'items': {
'object': 'list',
'data': [],
}
}, stripe.api_key)
resource.items = {
'0': {'id': 'si_foo', 'deleted': True},
'1': {'plan': 'plan_bar'},
}
expected = {
'items': {
'0': {'id': 'si_foo', 'deleted': True},
'1': {'plan': 'plan_bar'},
}
}
self.assertEquals(expected, resource.serialize(None))

0 comments on commit b5e4890

Please sign in to comment.