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

Fix coercion for array type: do not coerce array value if type is the same as specified #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

evheny0
Copy link

@evheny0 evheny0 commented Jul 12, 2019

Hi!

I've noticed some inconsistency in array type coercion. Right now if you specify attribute type

attribute :some_attribute, Array, of: SomeClass

it will coerce array values no matter of their class, even if they are the same class as specified in of param. This is a bit different from other types, which will not coerce if class is the same as declared in type.

Here is an example of current behavior:

class Person
  include ShallowAttributes

  attribute :addresses_array, Array, of: Hash
  attribute :address_hash, Hash
end

Person.new(address_hash: { street: "abc" })
# <Person address_hash={:street=>"abc"} addresses_array=[]>

Person.new(addresses_array: [{ street: "abc" }])
# NoMethodError: undefined method `coerce' for {}:Hash

And after fix behavior:

class Person
  include ShallowAttributes

  attribute :addresses_array, Array, of: Hash
  attribute :address_hash, Hash
end

Person.new(address_hash: { street: "abc" })
# <Person address_hash={:street=>"abc"} addresses_array=[]>

Person.new(addresses_array: [{ street: "abc" }])
# #<Person addresses_array=[{:street=>"abc"}]>

I hope this way there will be less confusion 😄

@coveralls
Copy link

coveralls commented Jul 12, 2019

Coverage Status

Coverage increased (+0.0002%) to 99.778% when pulling f299bd0 on evheny0:fix-array-subtypes-coercion into 51f5c1b on davydovanton:master.

Copy link
Owner

@davydovanton davydovanton left a comment

Choose a reason for hiding this comment

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

The fix looks good for me. Could you please fix CI? I'll merge this PR after this. Thanks! 👏

@evheny0 evheny0 force-pushed the fix-array-subtypes-coercion branch 3 times, most recently from f7563ee to ea23b07 Compare July 14, 2019 13:01
@lmmendes
Copy link

Hi @davydovanton when can we expect this bug fix to get merged and a release pushed to rubygems?

@evheny0
Copy link
Author

evheny0 commented Nov 7, 2019

Hi @davydovanton! CI is successful except for the jruby head, which fails on master as well. I believe the branch is ready to be merged :)

@evheny0
Copy link
Author

evheny0 commented Nov 7, 2019

Sorry for the delay 😄

@javierseixas
Copy link

Hi! Waiting for this fix to be released! ☝️

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

Successfully merging this pull request may close these issues.

5 participants