potion icon indicating copy to clipboard operation
potion copied to clipboard

Inline field for None returns an object with every key set to null instead of just null

Open dappiu opened this issue 6 years ago • 1 comments

Maybe not an issue and I'm just missing something. I'll try to explain me with some code

I have a situation like this:

class UserStatusResource(ModelResource):
    class Meta:
        model = UserStatus


class UserResource(ModelResource):
    class Meta:
        model = User

    class Schema:
        # id_user_status is an Integer ForeignKey referencing a single UserStatus row
        # user_status is a sqlalchemy.orm.relationship()
        # Here I want the UserStatus object embedded in the User repr, and not just a $ref
        user_status = fields.Inline('user_status')

Now, I'm expecting that when I fetch a user instance (GET /user/), if the instance has the id_user_status set to None (and consequently user_status relationship also returns None), the value of the key user_status to be None. Instead I get an object with every field of the user-status resource set to None (exception made for the $uri that is set to /user-status/None)

I tried passing nullable=True and default=None to Inline(), but nothing changes.

Is this the correct behaviour? Is there a way in which I can change this behaviour and simply get a null value as a representation for None?

Thanks

dappiu avatar Jul 27 '18 16:07 dappiu

It seems to me that it's not correct behavior, cause resource bound to invalid uri. You can get a null value with that patch:

--- schema.py.old       2019-08-19 15:12:04.712622200 +0400
+++ schema.py   2019-08-19 15:07:01.547122200 +0400
@@ -213,6 +213,8 @@
         return all(((i.default is not None) or i.nullable for i in (self.fields or {}).values()))

     def format(self, item):
+        if item is None:
+            return None
         return OrderedDict((key, field.output(key, item)) for key, field in self.fields.items() if 'r' in field.io)

     def convert(self, instance, update=False, pre_resolved_properties=None, patchable=False, strict=False):

xD8A avatar Aug 19 '19 11:08 xD8A