Jsonapi-resources: undefined method serialize_to_hash ... what's happend

Created on 6 Feb 2018  路  19Comments  路  Source: cerebris/jsonapi-resources

This issue is a (choose one):

  • [ ] Problem/bug report.
  • [ ] Feature request.
  • [#] Request for support. Note: Please try to avoid submitting issues for support requests. Use Gitter instead.

Checklist before submitting:

  • [ ] I've searched for an existing issue.
  • [ ] I've asked my question on Gitter and have not received a satisfactory answer.
  • [ ] I've included a complete bug report template. This step helps us and allows us to see the bug without trying to reproduce the problem from your description. It helps you because you will frequently detect if it's a problem specific to your project.
  • [ # ] The feature I'm asking for is compliant with the JSON:API spec.

Description

Choose one section below and delete the other:

Bug reports:

Please review Did you find a bug? and replace this content with a brief summary of your issue. If you can't submit a bug report template please be as thorough as possible when describing your your description. It's helpful to indicate which version of ruby and the JR gem you are using.

Features:

Please replace this line with a clear writeup of your feature request. Features that break compliance with the JSON:API spec will probably be closed.

Most helpful comment

In the documentation under serializer it has JSONAPI::ResourceSerializer.new(PostResource).serialize_to_hash(PostResource.new(post, context))

Try it. You now get this error on master branch:

undefined methodserialize_to_hash' for #`

It looks like this commit (bdcbf615155b8c6b3f34136d56d77254ed7d9cbf) completely removes the method from the ResourceSerializer? It also comments out / removes all the unit tests associated with the method.

It's not clear what new method (if any) replaces the old one either...

More than 5000+ lines of changes with no unit tests and comments that say: # ToDo: Revisit these tests. and # ToDo: Rework these tests

I would not consider master stable after seeing this :)

Any update to how this is supposed to work @lgebhardt ?

All 19 comments

@ohimy we can't help you if you don't provide more details

In the documentation under serializer it has JSONAPI::ResourceSerializer.new(PostResource).serialize_to_hash(PostResource.new(post, context))

Try it. You now get this error on master branch:

undefined methodserialize_to_hash' for #`

It looks like this commit (bdcbf615155b8c6b3f34136d56d77254ed7d9cbf) completely removes the method from the ResourceSerializer? It also comments out / removes all the unit tests associated with the method.

It's not clear what new method (if any) replaces the old one either...

More than 5000+ lines of changes with no unit tests and comments that say: # ToDo: Revisit these tests. and # ToDo: Rework these tests

I would not consider master stable after seeing this :)

Any update to how this is supposed to work @lgebhardt ?

Today i have this is error on version 0.10.0

Yep, just ran into this when trying to upgrade. The docs and the code definitely don't seem to agree.

Thank you for creating jsonapi-resources. Can you give us any guidance on how to upgrade, if we are dependent on the old serialize_to_hash method which is documented here: https://jsonapi-resources.com/v0.10/guide/serializer.html ?

It seems, we can still accomplish something similar by using serialize_resource_set_to_hash and passing in a resource_set? If I have a JSONAPI::Resource object, how do I got about getting that into a JSONAPI::ResourceSet? It looks like I can do it, but I need a resource_id_tree?

It's quite a nice thing to be able to use jsonapi-resources to serialize to JSON since, sometimes I want to be able to do things with my objects' JSON on the server, apart from the API. Since jsonapi-resources combines the API and the serializing, this has always been an attractive aspect of your library.

This is how I used to do it:

module Api
  class ApiSerializer

    def initialize(stuff, context = {})
      @context = context

      if stuff.is_a?(ActiveRecord::Relation)
        @models = stuff
      elsif stuff.is_a?(ActiveRecord::Base)
        @models = [stuff]
      else
        @models = stuff
      end
    end

    def resources
      if @resources
        @resources
      else
        @models.collect do |p|
          resource_klass.new(p, @context)
        end
      end
    end

    def json
      json = { data: [] }

      resources.each do |resource|

        cache_key = "api-serializers/#{resource_klass_name}-#{resource._model.id}/#{resource._model.updated_at.try(:to_s, :db)}"
        resource_json = Rails.cache.fetch(cache_key) do
          JSONAPI::ResourceSerializer.new(resource_klass).serialize_to_hash(resource)
        end

        json[:data].push resource_json[:data]
      end

      json
    end

    private

    def resource_klass
      resource_klass_name.constantize
    end

    # Mirroring JSONAPI::Resources -- see https://github.com/cerebris/jsonapi-resources/blob/v0.7.0/lib/jsonapi/acts_as_resource_controller.rb#L94
    def resource_klass_name
      @resource_klass_name ||= "#{self.class.name.underscore.sub(/\/serializers/, "").
        sub(/_serializer$/, "")}_resource".camelize.
        sub("Api::", "Api::Jsonapi::")
    end
  end
end

This would then allow me to do stuff like this, to get the actual JSON:

Api::Serializers::User.new(current_user, { current_user: current_user }).json.to_json

Thanks!
Kevin Trowbridge

Any suggestions here? Again docs don't line up and my google-fu is failing me. 馃槥

Ok I think figured it out from the test cases: https://github.com/cerebris/jsonapi-resources/blob/master/test/unit/serializer/serializer_test.rb#L23-L37

Please forgive my ignorance but this seems way more complicated than the previous version's implementation. Am I missing something here? Feels like slicing up watermelon till we get water...

Is this issue completely abandoned?

I am impacted by this as well. I switched to version 0.9.11 so I could use this feature, until this is fixed.

This issue is causing our project tons of problems. We had a bunch of code that followed the pattern described in the docs, which no longer works.

Ideally the code from the docs should continue to work, and show a deprecation message if necessary with instructions on how to migrate.

I think the docs might just need updating for v0.10+. There's an object_hash method on JSONAPI::ResourceSerializer that takes the resource and a relationship_data hash that does the trick as far as I can tell.

https://github.com/cerebris/jsonapi-resources/blob/5517f173a55ba2960c4f0852e8c51f8dd76194cc/lib/jsonapi/resource_serializer.rb#L144

Specifically, when rendering a resource in a non-JSONAPI::ActsAsResource controller:

def show
  post = Post.find(params[:id])

  # ... business logic ...
  resource = PostResource.new(post, { :current_user => current_user })
  json = JSONAPI::ResourceSerializer.new(PostResource).object_hash(post, { :comments => post.approved_comments })
  render :json => { :data => json }
end

NOTE: The above isn't setting the Content-Type header appropriately; it should be set to application/vnd.api+json to conform to the JSONAPI spec.

Bumped into this issue today trying to upgrade from 0.9.6. Any updates here?

I'm having the same issue.

@lgebhardt can you provide any insight on this?

@jamesdixon et al: I asked on the Gitter channel, here's his reply.

Thank you @lgebhardt for maintaining this library, and please ping me if I can help test things for this use-case in v0.11!

@tobias-grasse thank you!

hoisting the answer from gitter here for convenience

 Larry Gebhardt @lgebhardt Oct 27 11:06
Yes, v0.10 has made just serializing a resource a difficult task. I just wasn't aware of how many people were using the library that way. Restoring this functionality for v0.11 is a priority

speaking for myself, we had a test that was like

      context = { current_user: user }  
      resource_class = V1::JobResource  
      resource_options = {  
        include:  ["job_template",],    
        meta: { "job" => "current_user_can_actions" },  
      } 
      serializer = JSONAPI::ResourceSerializer.new(resource_class, resource_options)    
      resource_instance = resource_class.new(model, context)    
      as_json = serializer.serialize_to_hash(resource_instance) 

Temporary can use this
```
def jsonapi_serialize(ids)
serializer = JSONAPI::ResourceSerializer.new(resource_klass)
resource_set = get_resource_set(ids)
resource_set.populate!(serializer, context, {})
if ids.is_a?(Array)
serializer.serialize_resource_set_to_hash_plural(resource_set)
else
serializer.serialize_resource_set_to_hash_single(resource_set)
end
end

def get_resource_set(ids)
id_tree = JSONAPI::PrimaryResourceIdTree.new
directives = JSONAPI::IncludeDirectives.new(resource_klass, ['']).include_directives
Array(ids).each do |id|
identity = JSONAPI::ResourceIdentity.new(resource_klass, id)
fragment = JSONAPI::ResourceFragment.new(identity)
id_tree.add_resource_fragment(fragment, directives[:include_related])
end
JSONAPI::ResourceSet.new(id_tree)
end
```

@Lifehack043 this is working for me so far on v0.10.5

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chadwtaylor picture chadwtaylor  路  4Comments

balinterdi picture balinterdi  路  5Comments

mrcasals picture mrcasals  路  4Comments

lgebhardt picture lgebhardt  路  5Comments

ashaniray picture ashaniray  路  3Comments