stripe-python icon indicating copy to clipboard operation
stripe-python copied to clipboard

stripe.Account.external_accounts still deserializes into StripeObject instead of ListObject

Open sazlin opened this issue 8 years ago • 3 comments

Bug: The recent fix for https://github.com/stripe/stripe-python/issues/438 does not fix the repro provided in that bug. See the same issue here:

...
account = stripe.Account.retrieve(account_id)
account_serialized = str(account)
print account_serialized  # Note that the `external accounts` key has a value of `{}`. Expecting at least an 'object' key in there with value 'list'.
account_deserialized = stripe.Account.construct_from(json.loads(account_serialized), stripe.api_key)
account_deserialized.external_accounts  # <StripeObject at 0x113deb050> JSON: {}  <-- wrong type
account_deserialized.external_accounts.create  # raises AttributeError

Repros with Stripe lib version 1.84.1 Python version 2.7.14

sazlin avatar Jul 05 '18 18:07 sazlin

Argh. The fix only works when serializing the ListObject directly, but not when serializing an object that contains a ListObject:

>>> account = stripe.Account.retrieve('acct_...')
>>> str(account.external_accounts)
'{\n  "data": [], \n  "has_more": false, \n  "object": "list", \n  "total_count": 0, \n  "url": "/v1/accounts/acct_.../external_accounts"\n}'
>>> str(account)
'... \n  "external_accounts": {}, \n...'

We'll work on another fix. Sorry for the inconvenience @sazlin!

ob-stripe avatar Jul 05 '18 18:07 ob-stripe

Thanks for looking. FWIW, same issue occurs for stripe.Customer.sources and probably other lists. Ideally a fix here will work for all cases.

Here's an example of how we are working around the issue right now:

def deserialize(cached_value):  # cached_value is basically str(account)
    raw_seller_account = json.loads(cached_value)
    if 'object' not in raw_seller_account['external_accounts']:
        raw_seller_account['external_accounts']['object'] = stripe.ListObject.OBJECT_NAME
    return stripe.Account.construct_from(raw_seller_account, stripe.api_key)

Update: This workaround isn't enough. ListObject.create assumes an instance has a url key here as well: https://github.com/stripe/stripe-python/blob/0e31981f9fbf0808b3873b06d578c108756ec460/stripe/api_resources/list_object.py#L42

sazlin avatar Jul 05 '18 20:07 sazlin

Alright, hopefully this is fixed in 1.84.2.

>>> account = stripe.Account.retrieve('acct_...')

>>> str(account.external_accounts)
'{\n  "data": [], \n  "has_more": false, \n  "object": "list", \n  "total_count": 0, \n  "url": "/v1/accounts/acct_.../external_accounts"\n}'

>>> str(account)
'{...\n  "external_accounts": {\n    "data": [], \n    "has_more": false, \n    "object": "list", \n    "total_count": 0, \n    "url": "/v1/accounts/acct_.../external_accounts"\n  }, \n ...}'

Let me know if you're still having issues with this though!

ob-stripe avatar Jul 06 '18 15:07 ob-stripe