Skip to content

skip_parse does not work if property is nil #192

@denisovlev

Description

@denisovlev

Option skip_parse on property is ignored when when corresponding value on input hash is nil.
Expected: when skip_parse: true representer does not try to set any value in represented model
Actual: representer tries to set value if value is nil

it 'representable skip_parse nil' do
  class TestRepresenter < Representable::Decorator
    include ::Representable::Hash
    property :property1
    property :property2, skip_parse: true
  end

  class TestModel
    attr_accessor :property1
    attr_reader :property2

    def initialize
      @property1 = '123'
      @property2 = '456'
    end
  end

  m = TestModel.new
  r = TestRepresenter.new(m)

  expected_hash = {property1: '123', property2: '456'}.stringify_keys
  expect(r.to_hash).to eq(expected_hash)

  new_hash = {property1: '789', property2: nil}.stringify_keys
  r.from_hash(new_hash) # << fails here when trying to set readonly property2
  expect(m.property1).to eq('789')
  expect(m.property2).to eq('456')
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions