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

stripe.Account.external_accounts deserializes into StripeObject instead of ListObject

Open sazlin opened this issue 8 years ago • 4 comments

Bug: When getting a JSON representation of a stripe.Account object, the external_accounts ListObject is serialized as an empty JSON object. This is a problem when the JSON is later deserialized back into a stripe.Account object because the external_accounts JSON object is missing the object key-value that is required to deserialize external_accounts back into a ListObject. Instead, external_accounts is deserialized into a generic StripeObject that lacks critical capabilities, such as the ability to create a new external account for the deserialized stripe.Account. Attempting to call external_accounts.create, for instance, will raise an AttributeError: create exception.

This bug was encountered when upgrading our Stripe lib version from 1.27.1 to 1.84.0. It affects how we cache stripe.Account objects.

Expected: When getting a JSON representation of a stripe.Account object, the external_accounts object should be serialized in such a way that it can be deserialized back into a ListObject.

FWIW, the serialized external_accounts object use to be serialized like the following back in lib version 1.27.1:

    ...
    "external_accounts": {
        "data": [
        ],
        "has_more": false,
        "object": "list",
        "total_count": 0,
        "url": "/v1/accounts/<account id>/external_accounts"
    },
    ...

This was much better, and such an object seems to be properly deserialized back into a ListObject and behaviors such as external_accounts.create(...) behave as expected.

Repro:

import json
import stripe
stripe.api_key = <an api key>

account_id = <an account id>

account = stripe.Account.retrieve(account_id)
account_serialized = str(account)
account_deserialized = stripe.Account.construct_from(json.loads(account_serialized), stripe.api_key)

account.external_accounts  # <ListObject list at 0x1140ae618> JSON: {}
account_deserialized.external_accounts  # <StripeObject at 0x113eb6178> JSON: {}

account.external_accounts.create  # <bound method ListObject.create of <ListObject list at 0x1140ae618> JSON: {}>
account_deserialized.external_accounts.create  # AttributeError: create

This was encountered w/ Python 2.7.14

sazlin avatar Jul 03 '18 21:07 sazlin

Hi @sazlin, thanks for the report!

We've added some magic to make our list objects behave more like lists (despite the fact that they're really objects that contain a list). I think this issue is caused by the __len__ method. If I comment it out, empty lists are serialized correctly.

When the method is present, non-empty list objects are still serialized correctly even if the method doesn't return the number of attributes of the list object but rather the length of the data attribute, so I suspect that somewhere in the JSON serialization code, there is a if len(obj) == 0 check.

I'll investigate a bit more and update with my findings here.

ob-stripe avatar Jul 04 '18 09:07 ob-stripe

Okay, I was fairly close. This is the code in the JSON serializer that caused empty list objects to be serialized as {}: https://github.com/python/cpython/blob/2812d3d99287c50bab99625d7240bcf1c2e32369/Lib/json/encoder.py#L334-L336.

We could define a __nonempty__ / __bool__ method on ListObject that always returns True, but that seems fairly hacky. Instead, I added a special case in our existing custom JSONEncoder class. Patch is in #439.

ob-stripe avatar Jul 04 '18 10:07 ob-stripe

Fixed in 1.84.1.

ob-stripe avatar Jul 04 '18 17:07 ob-stripe

thanks for the awesome turnaround on this, but the bug does not appear fixed in 1.84.1. I logged a fresh issue to track: https://github.com/stripe/stripe-python/issues/440

sazlin avatar Jul 05 '18 18:07 sazlin