From b5e4890b179318bc43a6bbf87fef539189bf3dce Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Wed, 15 Nov 2017 16:33:58 +0100 Subject: [PATCH] Fix items serialization when items is a dict --- stripe/api_resources/subscription.py | 15 ++++++--- tests/api_resources/test_subscription.py | 40 +++++++++++++++++++++++- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/stripe/api_resources/subscription.py b/stripe/api_resources/subscription.py index 2a9ca8662..86ce4d02f 100644 --- a/stripe/api_resources/subscription.py +++ b/stripe/api_resources/subscription.py @@ -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) diff --git a/tests/api_resources/test_subscription.py b/tests/api_resources/test_subscription.py index 66bc567a0..73bed35e0 100644 --- a/tests/api_resources/test_subscription.py +++ b/tests/api_resources/test_subscription.py @@ -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))