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

Reserved keywords in serializer #23

Open
floriandejonckheere opened this issue Mar 17, 2020 · 1 comment
Open

Reserved keywords in serializer #23

floriandejonckheere opened this issue Mar 17, 2020 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@floriandejonckheere
Copy link
Contributor

Due to the way the serialization mechanism is written, attributes in a serializer cannot be named either model or options, because these methods are already defined on CacheCrispies::Base. No warning or error is thrown either.

The following example demonstrates this:

#!/usr/bin/env ruby

require "cache_crispies"

class MySerializer < CacheCrispies::Base
  key :my_serializer

  serialize :id

  serialize :options do |model, options|
    # the passed `model` will not be the model, but rather the instance
    # of the serializer. To retrieve the model, we have to call
    # `model.model`.
    model.model.options
  end
end

model = OpenStruct.new(id: "my_id", options: { my_option: true })

puts MySerializer.new(model, {}).as_json

=> {:id=>"my_id", :options=>{}}

The logic that exhibits this can be found on https://github.com/codenoble/cache-crispies/blob/master/lib/cache_crispies/hash_builder.rb#L63.

@adamcrown adamcrown added the bug Something isn't working label Mar 21, 2020
@adamcrown adamcrown self-assigned this Mar 21, 2020
@adamcrown
Copy link
Member

That's a good point. Thanks for bringing that up. I'll see if I can come up with a fix for this, or at the very least a warning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants