jsonapi-deserializable icon indicating copy to clipboard operation
jsonapi-deserializable copied to clipboard

`hash` is not getting populated?

Open bunnymatic opened this issue 1 year ago • 1 comments

Hi

I'm working in a little Sinatra app and found that after deserializing with the Resource class defined I here, I can't seem to get to the attributes because the to_h method give me nothing.

The get_jsonapi method was defined elsewhere and does the http call and returns the response data that lives under "data" key.

As you can see, the deserializer properly pulls the attribute out but does not fill the @hash attribute so the to_h doesn't give me any data back.


# in a console

> jsonapi = get_jsonapi("studios/1890-bryant-street-studios.json")
=> {"id"=>"3",
 "type"=>"studio",
 "attributes"=>
  {"name"=>"1890 Bryant Street Studios",
   "street_address"=>"1890 bryant st",
   "city"=>"New Ginaberg",
   "map_url"=>"http://maps.google.com/maps?q=1890+bryant+st%2C+New+Ginaberg%2C+Kansas%2C+87776",
   "url"=>"/studios/1890-bryant-street-studios",
   "slug"=>"1890-bryant-street-studios"},
"relationships"=>{"artists"=>{"meta"=>{"count"=>27}}}}

> studio = JSONAPI::Deserializable::Resource.new(jsonapi)
=> #<JSONAPI::Deserializable::Resource:0x00007fc8663fcab0
 @attributes=
  {"name"=>"1890 Bryant Street Studios",
   "street_address"=>"1890 bryant st",
   "city"=>"New Ginaberg",
   "map_url"=>"http://maps.google.com/maps?q=1890+bryant+st%2C+New+Ginaberg%2C+Kansas%2C+87776",
   "url"=>"/studios/1890-bryant-street-studios",
   "slug"=>"1890-bryant-street-studios"},
 @data=
  {"id"=>"3",
   "type"=>"studio",
   "attributes"=>
    {"name"=>"1890 Bryant Street Studios",
     "street_address"=>"1890 bryant st",
     "city"=>"New Ginaberg",
     "map_url"=>"http://maps.google.com/maps?q=1890+bryant+st%2C+New+Ginaberg%2C+Kansas%2C+87776",
     "url"=>"/studios/1890-bryant-street-studios",
     "slug"=>"1890-bryant-street-studios"},
   "relationships"=>{"artists"=>{"meta"=>{"count"=>27}}}},
 @hash={},
 @id="3",
 @relationships={"artists"=>{"meta"=>{"count"=>27}}},
 @reverse_mapping={},
 @root="/data",
 @type="studio">


> studio.to_h
=> {}

Am I mis-using things? Or is this a bug?

I did a tiny bit of digging and it seems that in resource.rb where you define

      def deserialize_attr(key, val)
        block = self.class.attr_blocks[key] || self.class.default_attr_block
        return {} unless block

the block is actually nil. Is that expected? Am I supposed to define a default_attr_block?

bunnymatic avatar Sep 06 '23 15:09 bunnymatic

I found that if I make a little thing I can subclass from, and I define the default_attr_block the same way it's defined in the deserializable/resource/dsl.rb in this gem, things work out.

With this

require 'jsonapi/deserializable'

class JsonApiResource < JSONAPI::Deserializable::Resource
  self.default_attr_block = proc { |v, k| { k.to_sym => v } }
end

Then I can do this

studio = JsonApiResource.new(jsonapi)
studio.to_h

and get the expected hash of attributes like

{:name=>"1890 Bryant Street Studios", :street_address=>"1890 bryant st", :city=>"New Ginaberg", :map_url=>"http://maps.google.com/maps?q=1890+bryant+st%2C+New+Ginaberg%2C+Kansas%2C+87776", :url=>"/studios/1890-bryant-street-studios", :slug=>"1890-bryant-street-studios"}

Is this the recommended use? it kind of looks like in the DSL, you try to define the defaults but they don't seem to be coming through.

bunnymatic avatar Sep 08 '23 01:09 bunnymatic