active_model_serializers icon indicating copy to clipboard operation
active_model_serializers copied to clipboard

Compatibility of Rails 5 API-only mode and AMS 0.9x

Open oyeanuj opened this issue 7 years ago • 11 comments

Hi folks, I was trying to upgrade my current project to Rails 5, API-only mode while continuing to use AMS 0.9 (due to pending #1845 being blocker for us) and ran into the following issue which goes away on upgrading to 0.10x (which unfortunately, I can't do for my project).

While generating scaffolds with a Rails 5 API project, the project complains about api_controller.rb:

→ rails g scaffold User
Running via Spring preloader in process 20090
      invoke  active_record
      create    db/migrate/20160911170034_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/models/user_test.rb
      create      test/fixtures/users.yml
      invoke  resource_route
       route    resources :users
      create  app/serializers/user_serializer.rb
      invoke  scaffold_controller
Could not find "api_controller.rb" in any of your source paths. Your current source paths are:
/Users/anuj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/active_model_serializers-0.9.5/lib/active_model/serializer/generators/serializer/templates

Is this expected behavior and is there any workaround for this? Are there any other issues that I should expect to encounter in this process? I'd love to be a part of the Rails 5 party :)

The only issue related to Rails 5 and AMS 0.9 that I found was #600, but that is slightly unconnected to this one (and I've added their fix for the other problem).

Thank you!

oyeanuj avatar Sep 11 '16 17:09 oyeanuj

It's undesirable, but not entirely unexpected. This is a symptom of development efforts shifting to 0.10.x and support for 0.9.x being reduced and deprecated. Problems like these will become more frequent as 0.9 ages. I'd recommend migrating to 0.10.x and would be interested to hear why you're currently unable to do so.

remear avatar Sep 15 '16 17:09 remear

@remear Thanks for the response! While I totally understand not wanting to invest more on the 0.9x and 0.8x versions, I am hoping that atleast having them compatible with Rails-5 would be worth the effort and fits under 'reduced support'.

For our case, I'd love to upgrade to 0.10x but #1845 is a blocker which I know @bf4 is mulling over and working on. (In general, and please correct me if I'm wrong, 0.10x doesn't have the same feature compatibility with 0.8/0.9 especially in terms of supporting Rails as_json behavior). Until some of those issues are resolved, I would propose that 0.8 and 0.9 <-> Rails 5 be something that the team looks into.

oyeanuj avatar Sep 15 '16 17:09 oyeanuj

Even fixing all known issues with 0.9 and Rails 5 would bring an increased support burden because issues will continue to come up. The expectation would then become that AMS support 0.9.x on Rails 5.x. Support has to end at some point. I know that's a bummer.

With that being said, if this is the only issue to get 0.9 working with Rails 5, we could probably consider a PR that fixes the issue. I think it unlikely any of the team will have time to focus on this or any other non-0.10 issue. Therefore, someone still on 0.9 experiencing this issue would need to do the research and propose a PR with the fix.

Is that something that interests you?

remear avatar Sep 15 '16 21:09 remear

@remear I'd be happy to give it a shot if I can get some guidance on the right direction. Given that you all have a plethora of knowledge, if you can help point me to the right direction, I can report back with the PR/steps/solution.

The expectation would then become that AMS support 0.9.x on Rails 5.x.

I hear you. Maybe putting a big disclaimer on the README? :)

I just think that the AMS change is the hardest on those on 0.9x and this disconnects upgrading AMS from upgrading Rails. But also it is easier for me to say, since you guys end up maintaining the library.

oyeanuj avatar Sep 15 '16 21:09 oyeanuj

Great, thanks for your willingness to contribute!

I suspect a good place to start would be comparing the differences between the generators:

0.9 - https://github.com/rails-api/active_model_serializers/tree/0-9-stable/lib/active_model/serializer/generators 0.10 - https://github.com/rails-api/active_model_serializers/tree/master/lib/generators/rails

It could be something like https://github.com/rails-api/active_model_serializers/blob/0-9-stable/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb is no longer necessary.

remear avatar Sep 16 '16 00:09 remear

@remear Sorry for the long silence, I have been wanting to come back to this whenever I had a moment. Anyways, an update:

  1. No luck yet, even by removing the code in that file. I still see the error.

  2. On running the test suite, I see the following errors with Rails 5.0.1

Finished in 0.270133s, 640.4253 runs/s, 1036.5265 assertions/s.

  1) Error:
ActionController::SerializationsAssertions::RenderSerializerTest#test_does_not_overwrite_notification_subscriptions:
NoMethodError: assert_template has been extracted to a gem. To continue using it,
        add `gem 'rails-controller-testing'` to your Gemfile.
    /Users/anuj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.1/lib/action_controller/template_assertions.rb:4:in `assert_template'
    /Users/anuj/Github/active_model_serializers/test/integration/action_controller/serialization_test_case_test.rb:67:in `test_does_not_overwrite_notification_subscriptions'


  2) Error:
ResourceGeneratorTest#test_serializer_file_is_generated:
NoMethodError: undefined method `hook_for' for #<Rails::Generators::ResourceGenerator:0x007ff492b90288>
    /Users/anuj/Github/active_model_serializers/lib/active_model/serializer/generators/resource_override.rb:10:in `add_serializer'
    /Users/anuj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor/command.rb:27:in `run'
    /Users/anuj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor/invocation.rb:126:in `invoke_command'
    /Users/anuj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor/invocation.rb:133:in `block in invoke_all'
    /Users/anuj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor/invocation.rb:133:in `each'
    /Users/anuj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor/invocation.rb:133:in `map'
    /Users/anuj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor/invocation.rb:133:in `invoke_all'
    /Users/anuj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor/group.rb:232:in `dispatch'
    /Users/anuj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/thor-0.19.4/lib/thor/base.rb:444:in `start'
    /Users/anuj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.1/lib/rails/generators/testing/behaviour.rb:68:in `block in run_generator'
    /Users/anuj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/testing/stream.rb:31:in `capture'
    /Users/anuj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.1/lib/rails/generators/testing/behaviour.rb:66:in `run_generator'
    /Users/anuj/Github/active_model_serializers/test/integration/generators/resource_generator_test.rb:14:in `test_serializer_file_is_generated'

173 runs, 280 assertions, 0 failures, 2 errors, 0 skips
rake aborted!
Command failed with status (1): [ruby -w -I"lib:test" -I"/Users/anuj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib" "/Users/anuj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rake-12.0.0/lib/rake/rake_test_loader.rb" "test/integration/action_controller/namespaced_serialization_test.rb" "test/integration/action_controller/serialization_test.rb" "test/integration/action_controller/serialization_test_case_test.rb" "test/integration/active_record/active_record_test.rb" "test/integration/generators/resource_generator_test.rb" "test/integration/generators/scaffold_controller_generator_test.rb" "test/integration/generators/serializer_generator_test.rb" "test/unit/active_model/array_serializer/except_test.rb" "test/unit/active_model/array_serializer/key_format_test.rb" "test/unit/active_model/array_serializer/meta_test.rb" "test/unit/active_model/array_serializer/only_test.rb" "test/unit/active_model/array_serializer/options_test.rb" "test/unit/active_model/array_serializer/root_test.rb" "test/unit/active_model/array_serializer/scope_test.rb" "test/unit/active_model/array_serializer/serialization_test.rb" "test/unit/active_model/default_serializer_test.rb" "test/unit/active_model/serializer/associations/build_serializer_test.rb" "test/unit/active_model/serializer/associations_test.rb" "test/unit/active_model/serializer/attributes_test.rb" "test/unit/active_model/serializer/config_test.rb" "test/unit/active_model/serializer/filter_test.rb" "test/unit/active_model/serializer/has_many_polymorphic_test.rb" "test/unit/active_model/serializer/has_many_test.rb" "test/unit/active_model/serializer/has_one_and_has_many_test.rb" "test/unit/active_model/serializer/has_one_polymorphic_test.rb" "test/unit/active_model/serializer/has_one_test.rb" "test/unit/active_model/serializer/key_format_test.rb" "test/unit/active_model/serializer/meta_test.rb" "test/unit/active_model/serializer/options_test.rb" "test/unit/active_model/serializer/root_test.rb" "test/unit/active_model/serializer/scope_test.rb" "test/unit/active_model/serializer/url_helpers_test.rb" ]

Not sure if that is helpful, but thought I'd post it here incase anyone else who is in the Rails 5 + AMS 0.9 predicament comes to this issue.

I'll keep digging but it seems challenging never having been exposed to the internals of rail generators before. Any tips on other possible problems - if the test suite result was helpful?

Thanks!

oyeanuj avatar Dec 21 '16 18:12 oyeanuj

@remear Any thoughts based on the update in the previous comment?

oyeanuj avatar Jan 05 '17 19:01 oyeanuj

@oyeanuj Do you have your AMS changes pushed to a fork and branch somewhere myself and others could check out?

remear avatar Jan 06 '17 03:01 remear

Hi @remear! Apologies for the delay, for some reason, I didn't see the notification. Only saw this message checking in today.

I've published a test project, AMS fork with a few of the more relevant changes and given you collaborator access as well.

  1. Rails 5 Project
  2. AMS fork (using the 0-9-stable branch)

Please keep me posted, I'll remember to check this everyday now and not rely on notifications!

Thank you!

oyeanuj avatar Jan 28 '17 22:01 oyeanuj

@remear Just checking in to see if you've had a chance to look over the above links? Thank you!

oyeanuj avatar Feb 24 '17 04:02 oyeanuj

@remear I know you are probably busy but in case you have a few minutes to look at the above links. Would appreciate any help here! Thank you!

oyeanuj avatar May 14 '17 00:05 oyeanuj