Jsonapi-resources: Rails 5 Support?

Created on 29 Sep 2015  路  26Comments  路  Source: cerebris/jsonapi-resources

Is anyone working on Rails 5 support for JR? I just tried it out and it's got a few problems due to using some of Rails' internal API. The two that I've hit so far are that ActionDispatch::ParamsParser::DEFAULT_PARSERS is no longer a thing so mime_types.rb explodes and @scope[:jsonapi_resource] = @resource_type no longer works (the []= setter was removed by @tenderlove 10 days ago or so).

I've got a fork where I'm working on fixing some of these problems but was curious if anyone else has already started down this path?

Most helpful comment

@jopotts I think the advantage of this gem (JR) over AMS is that it handles both the request and the response sides of JSON API where as AMS currently only handles serializing a response. I believe there has been some talk of making AMS handle deserialization as well, but I don't know if that will be included in Rails 5. JR is also only focused on JSON API compliance where as AMS supports serializing in other formats as well.

Some history: One of the things I like most about JSON API is the constraints on creating and updating resources in addition to the response format. Before JR was conceived we were looking at AMS. The one sided focus on serialization was what finally tipped us over to writing this gem. The other major factor was the existing user base of AMS has requirements for backwards compatibility. I think it will be harder to satisfy both full JSON API compliance and and backwards compatibility in the same gem. I'm sure it can be done (and I hope it is), but I thought starting fresh would be more efficient.

All 26 comments

I've been chasing assorted bugs of this flavor for a few months now. Current state of play:

  • #384 to resolve the ActionDispatch::Routing::Mapper::Scope change to []= (which was rails/rails@5e81e6c in early August),
  • #465 resolved the deprecations,
  • and I have a half-written, not-yet-tested patch that fixes the ActionDispatch::{ParamsParser => Http::Parameters}::DEFAULT_PARSERS issue (which was rails/rails@b93c226).

I hope this is a pertinent question here. Forgive me if not.

Rails 5 will have https://github.com/rails-api/active_model_serializers included which has a jsonapi adapter. What would be the advantage of using this gem (jsonapi-resources) over AMS? This gem seems cleaner, but built in possibly trumps that.

I'm currently using AMS 0.9 (non-jsonapi) and am looking at options to move to the jsonapi standard.

@jopotts I think the advantage of this gem (JR) over AMS is that it handles both the request and the response sides of JSON API where as AMS currently only handles serializing a response. I believe there has been some talk of making AMS handle deserialization as well, but I don't know if that will be included in Rails 5. JR is also only focused on JSON API compliance where as AMS supports serializing in other formats as well.

Some history: One of the things I like most about JSON API is the constraints on creating and updating resources in addition to the response format. Before JR was conceived we were looking at AMS. The one sided focus on serialization was what finally tipped us over to writing this gem. The other major factor was the existing user base of AMS has requirements for backwards compatibility. I think it will be harder to satisfy both full JSON API compliance and and backwards compatibility in the same gem. I'm sure it can be done (and I hope it is), but I thought starting fresh would be more efficient.

All good points. I'd not realised it was also a deserializer. The https://github.com/cerebris/jsonapi-resources#client-libraries section of the readme kind of implied otherwise. Thanks.

Sorry, I think my response may be confusing. When I said "deserialization" I was talking about the server deserializing a request into an action for the server to take, as opposed to a client deserializing a response from an API server. I hope that's a little clearer.

Oh right! :-) I see what you mean now. I had indeed misinterpreted. Thanks for the clarification.

I have some issues debugging the failing spec(s) with Rails 5.0.0.beta1 - using my branch, I get the following error from ActionDispatch::Routing::Mapper::Resources

Testing With RAILS VERSION 5.0.0.beta1
rake aborted!
NoMethodError: undefined method `singleton?' for nil:NilClass
/Users/lars/Code/jsonapi-resources/lib/jsonapi/routing_ext.rb:94:in `jsonapi_resources'
/Users/lars/Code/jsonapi-resources/test/test_helper.rb:119:in `block in <top (required)>'
/Users/lars/Code/jsonapi-resources/test/test_helper.rb:117:in `<top (required)>'
/Users/lars/Code/jsonapi-resources/Rakefile:4:in `require'
/Users/lars/Code/jsonapi-resources/Rakefile:4:in `<top (required)>'

@jashank discovered this in august. Anyone got time to take a look?

@ovelar I didn't take the time to figure out what any of this is doing, but this line of code works for me (instead of @scope = @scope ...)

@scope.instance_variable_get(:@hash)[:jsonapi_resource] = @resource_type

Thanks @quinn, it seems to do the same thing with less magic. Updated the PR #579

I get /home/ivan/.rvm/gems/ruby-2.2.3/gems/bundler-1.11.2/lib/bundler/runtime.rb:80:inrescue in block (2 levels) in require': There was an error while trying to load the gem 'jsonapi-resources'. (Bundler::GemRequireError)` when I try to generate a resource.

I'm using Rails 5.0.0-beta1 API. It's basically the same as #575.

In my case, #575 is fixed with PR #579 and adding gem 'active_model_serializers' to Gemfile. Thanks @ovelar!

I just setup a Rails 5 project and am also getting an error. I'm using jsonapi-resources 0.7.1.beta1, ruby 2.2.3, rails edge (setup project with --api --edge, using rails v5.0.0.beta2)

There was an error while trying to load the gem 'jsonapi-resources'. (Bundler::GemRequireError)

After adding gem 'jsonapi-resources', :git => 'https://github.com/cerebris/jsonapi-resources.git' , running bundle I ran bin/rails server and got the error above

Try using the rails5 branch of JR. I'm trying to keep it updated with the changes from master so it should be current.

I'm working on a rails5 beta 2 projects and the rails5 branch of JR has been working great for me. Thanks @lgebhardt

@lgebhardt this week I tried adding a couple custom errors using new classes in JSONAPI::Exceptions and it worked fine when I defined those in the Resource file, but when I moved them to my lib directory the app worked and custom errors worked, but rspec blew up. Same error Bundler::GemRequireError. This was in a Rails v4.2. app not a v5 beta.

has this changed? when doing gem 'jsonapi-resources', github: 'cerebris/jsonapi-resources', branch: 'rails5' I get a 'does not exist'.

@Bergrebell Sorry, the rails 5 branch was merged into master. I should have left it around for people who were using it. There are a few other changes in master as well. I'm hoping we can get a beta release with the new features soon. So for now change rails5 to master in your Gemfile.

@lgebhardt Also getting error There was an error while trying to load the gem 'jsonapi-resources'. Using Rails 5.0.0.rc1 and jsonapi-resources master.

I haven't tested against RC1. I'm away so it might take me a little bit to look into this, but I'll try to get to it in the next couple of days. If anyone else wants to dive into it before then it would be most appreciated.

Just noticed the last note in #712. Does this look to be the issue? If so maybe you could build against this PR.

@lgebhardt No, that issue was only in the tests, shouldn't affect the gem in operation.

For what it's worth, I had this issue with Rails 5.0.0.rc1 and it went away when I used master from GitHub. The actual error in my case was "NameError: uninitialized constant JSONAPI::ActionController".

@pixelhandler Did you get to the bottom of your issue with rspec and custom jsonapi errors? I'm having the same issue with minitest

I am still getting this error.
There was an error while trying to load the gem 'jsonapi-resources'. (Bundler::GemRequireError)

Gem Load Error is: undefined method env for JSONAPI::Rails:Module

I am using Rails 5.1.4 with:
jsonapi-resources 0.10.0.beta1 from https://github.com/cerebris/jsonapi-resources.git (at master@69b1d73)

Is anyone else facing this issue?

I pinned down the error. It seems like the gem does not have access to some rails methods like Rails.env so I had to modify gems/jsonapi-resources-0.9.5/lib/jsonapi/configuration.rb:75 and in
gems/jsonapi-resources-69b1d7396eed/lib/jsonapi/configuration.rb:84 changed self.include_backtraces_in_errors = !Rails.env.production? to self.include_backtraces_in_errors = true and did the same for the other file just as a fix.

Works for now but, Is there a better way?

@Anyoks

Is anyone else facing this issue?

I have the exact same issue using Rails 5.1.4 after bundling and starting the server

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mrcasals picture mrcasals  路  4Comments

adambedford picture adambedford  路  5Comments

blmundie picture blmundie  路  4Comments

evolve2k picture evolve2k  路  3Comments

d3crypt3d picture d3crypt3d  路  3Comments