Skip to content

Value can change when type mismatch handler runs #5

@christoomey

Description

@christoomey

Hello 👋 First and foremost, thanks for this fantastic library!

We ran into an issue where we had attributes(id: string) configured for a serializer which was incorrect. Most models in our app have UUID / string IDs, but this model had an integer ID.

Per the note on Type Checking we had configured the type_mismatch_handler to Rails.logger.warn(...) in our case. Unfortunately, this caused our ID value to come out as true, which was very surprising.

Looking into the code, I believe the relevant line is this call to the type_mismatch_handler which ends up returning the value of calling the provided proc (true in our case from the logger call).

I'm wondering if this is intentional, but if so I'd recommend updating the docs to highlight this subtlety.

In our app we've updated or type_mismatch_handler proc to return the value, and I'd think this would likely be the expectation for most and thus maybe worth a breaking change to update? I'm imaging something like the following:

# replacing https://github.com/jgaskins/primalize/blob/7d08bb45ddb4fb58a301dca2938762b12efdff4d/lib/primalize/single.rb#L124-L133
hash[attr] = value
if type !== value
  self.class.type_mismatch_handler.call(
    self.class,
    attr,
    type,
    value,
  )
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions