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

Parsing null values results in property being set despite reader property present #234

Open
oliverguenther opened this issue Oct 9, 2019 · 0 comments · Fixed by opf/openproject#9090

Comments

@oliverguenther
Copy link

oliverguenther commented Oct 9, 2019

The reader: option is documented as follows:

With :reader, parsing is completely up to you. Representable will only invoke the function and do nothing else.

I have the following representer that sets a different property on represented after processing it. I do not want to have status_code being set automatically by representable, which is why I'm using a reader. Actually, I could have lived with a setter: but that again is not called when passing { status: nil } into the representer.

class MyRepresenter < Representable::Decorator
        property :status,
                 getter: ->(*) { status&.code },
                 reader: ->(doc:, represented:, **) {
                   status = doc['status']
                   # (omitted custom processing of status)
                   represented.status = status
                 }  
end

I would suspect the following code:

object = OpenStruct.new
hash = { status: nil }
MyRepresenter.new(object).from_hash(hash)

to output

{ status_code: "whatever is being returned by my reader function" }

But actually the call outputs

{ status_code: "whatever is being returned by my reader function", status: nil }

How should one avoid the status property being written and bypassing any setter / reader ?

ulferts pushed a commit to opf/openproject that referenced this issue Mar 23, 2021
* Add Users form endpoints

* Avoid cache_if as it caches properties for new_record? as well

* Add specs

* Add missing custom_field_values method

* Fix missing firstname lastname api conversions

* Disable cached_representer on user payload

* Add patch to allow using our custom setter in case of nil values

trailblazer/representable#234

* Add test for changing status

* Fix expectation with changed attribute mapping

* Disable rendering name property for users

The name property is not accessible directly for users

* Fix attribute names for first and lastname for writable

* Ensure password is marked writable even though its not an attribute

* Avoid explicitly setting to_ar_name conversion

* Reuse AssignableCustomFieldValues concern

* Fix structure of users.apib to be flat

* Extend requests with custom fields

* Output name, but dont mark as writable attribute

* Fix id indentation

* Rename schema email property

* Fix param for user update form

* Fix schema docs
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 a pull request may close this issue.

1 participant