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

Allow accessing preferences on models that do not have any set #3998

Merged

Conversation

kennyadsl
Copy link
Member

@kennyadsl kennyadsl commented Mar 17, 2021

Description

We may have models that are supposed to have preferences but are not defining them explicitly because they are not needed.

For example, when defining a custom calculator that does not need any preference. Core code expects that preferences still responds with a Hash instead of nil because that's how it worked before b947015.

That commit is needed because otherwise Rails would serialize the object differently on models that do not use preferences, because seralize is now lazy executed. Example of the wrong serialization without it:

   expected: #<Spree::Address id: 4, firstname: "John", lastname: "Von Doe", address1: "10 Lovely Street", address...00 +0000", updated_at: "2021-03-17 08:09:00.244459000 +0000", name: "John Von Doe", preferences: {}>
        got: #<Spree::Address id: 4, firstname: "John", lastname: "Von Doe", address1: "10 Lovely Street", address...0 +0000", updated_at: "2021-03-17 08:09:00.244459000 +0000", name: "John Von Doe", preferences: nil>

This commit introduces a hack to have both things. When preferences is empty at database level, it's safe to always return a Hash, because that's how the data would have been deserialized anyway. This allows us to call preferences[:something] on models that do not explicitly define any preference.

NOTE: This is kind of a hack just to allow backward compatibility. #3997 probably provides a better solution to this problem but it is not backward compatible. Long-term it's maybe better to adopt that approach but, in my opinion, it needs a deprecation cycle to allow all stores and extensions to explicitly set the serialization methods when needed.

Checklist:

  • I have followed Pull Request guidelines
  • I have added a detailed description into each commit message
  • [ ] I have updated Guides and README accordingly to this change (if needed)
  • I have added tests to cover this change (if needed)
  • [ ] I have attached screenshots to this PR for visual changes (if needed)

@kennyadsl kennyadsl added type:bug Error, flaw or fault Needs Backport labels Mar 17, 2021
@kennyadsl kennyadsl added this to the 2.11 milestone Mar 17, 2021
@kennyadsl kennyadsl self-assigned this Mar 17, 2021
We may have models that are supposed to have preferences but are not
defining them explicitly because they are not needed.

For example, when defining a custom calculator that does not need any
preference. Core code expects that preferences still responds with a
Hash instead of nil because that's how it worked before b947015.

That commit is needed because otherwise Rails would serialize the object
differently on models that do not use preferences, because seralize is
now lazy executed. Example of the wrong serialization without it:

       expected: #<Spree::Address id: 4, firstname: "John", lastname: "Von Doe", address1: "10 Lovely Street", address...00 +0000", updated_at: "2021-03-17 08:09:00.244459000 +0000", name: "John Von Doe", preferences: {}>
            got: #<Spree::Address id: 4, firstname: "John", lastname: "Von Doe", address1: "10 Lovely Street", address...0 +0000", updated_at: "2021-03-17 08:09:00.244459000 +0000", name: "John Von Doe", preferences: nil>

This commit introduces a hack to have both things. When preferences is
empty at database level, it's safe to always return a Hash, because
that's how the data would have been deserialized anyway. This allows
us to call `preferences[:something]` on models that do not explicitly
define any preference.
@kennyadsl kennyadsl force-pushed the kennyadsl/fix-preferences-serialization branch from 814cb81 to 6a0e60a Compare March 17, 2021 10:31
@kennyadsl kennyadsl marked this pull request as ready for review March 17, 2021 11:36
Copy link
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

Would like to hear from @mamhoff as well.

Copy link
Contributor

@mamhoff mamhoff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, and thanks for responding so quickly!

Copy link
Member

@spaghetticode spaghetticode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kennyadsl thanks 👍

@kennyadsl kennyadsl merged commit bcea9e1 into solidusio:master Mar 18, 2021
@kennyadsl kennyadsl deleted the kennyadsl/fix-preferences-serialization branch March 18, 2021 12:45
mamhoff added a commit to mamhoff/solidus that referenced this pull request Mar 28, 2021
…preferences-serialization"

This reverts commit bcea9e1, reversing
changes made to a9a7a95.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Error, flaw or fault
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants