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

Attributes with accessor are serialized with their raw value and adding the attribute on __appends__ causes internal state issues #683

Closed
federicoparroni opened this issue May 24, 2022 · 1 comment · Fixed by #692
Labels
bug An existing feature is not working as intended

Comments

@federicoparroni
Copy link
Contributor

Describe the bug
The serialize method modifies the internal __attributes__ dict when an attribute is in __appends__ and has an accessor.
I noticed this bug when I added the attribute to the __appends__ because I wanted that attribute to be serialized using the accessor, otherwise the attribute is serialized using the its raw value. Is there any way to achieve this?

To Reproduce

class User(Model):
    __table__ = 'users'
    __timestamps__ = False
    __hidden__ = [
        'id',
    ]
    __fillable__ = [
        'name',
    ]

    def get_name_attribute(self):
        name = self.get_raw_attribute('name')
        if name is not None:
            return "Mr. " + name.strip()

def __main__():
   user = User.first_or_fail()
   print(user.name)             # correctly prints "Mr. John"
   print(user.serialize())      # correctly prints { "name": "Mr. John" }
   print(user.name)             # wrongly prints "Mr. Mr. John"

Expected behavior
The serialized attribute should be the one returned from the accessor and the internal attribute should not be altered after the serialize call

Desktop (please complete the following information):

  • OS: Ubuntu
  • Version: 21.04

What database are you using?

  • Type: MySQL
  • Version: 14.14 Distrib 5.7.30, for Linux (x86_64)
  • Masonite ORM 2.6.1
@federicoparroni federicoparroni added the bug An existing feature is not working as intended label May 24, 2022
@federicoparroni
Copy link
Contributor Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An existing feature is not working as intended
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant