davydovanton / shallow_attributes

Simple and lightweight Virtus analog.
MIT License
101 stars 18 forks source link

Fix coercion for embedded shallow attributes type, receiving unexpected parameters #34

Open javierseixas opened 4 years ago

javierseixas commented 4 years ago

PROBLEM

Having two classes related like follow:

class Address
  include ShallowAttributes

  attribute :street,  String
end

class Person
  include ShallowAttributes

  attribute :name,      String
  attribute :address,   Address
end

... when the embedded class receives parameters that doesn't match the defined attributes:

{
    name: 'John',
        address: {
            street: 'Street',
            number: '12'
        }
}

... an error raises because doesn't find the definition in the class.

When a class including ShallowAttribues is initialized, during the initialization parameters non matching attributes are dismissed. But it doesn't happen during the lower levels in the hierarchy, because the objects are creating according to the type.

SOLUTION

Adding some logic during the type coercion and initialization avoid the conflict.

coveralls commented 4 years ago

Coverage Status

Coverage increased (+0.003%) to 99.781% when pulling c4c8b43af52cf8d9cc41f0207ccc786b3926da8c on LemontechSA:fix-nested-shallow-attributes-with-unexpected-parameters into 51f5c1b017e6d389446a87bf02598fee844605fd on davydovanton:master.

coveralls commented 4 years ago

Coverage Status

Coverage increased (+0.005%) to 99.784% when pulling 80990d91671b2ca5005eca0a06090bc6411171ae on LemontechSA:fix-nested-shallow-attributes-with-unexpected-parameters into 51f5c1b017e6d389446a87bf02598fee844605fd on davydovanton:master.