Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dappiu opened this issue Jul 27, 2018 · 1 comment

Comments

@dappiu
Copy link

dappiu commented Jul 27, 2018

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

@xD8A
Copy link

xD8A commented Aug 19, 2019

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):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants