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

Docs: Update changelog for 0.10.3 #2048

Closed
wants to merge 1 commit into from

Conversation

jaredbeck
Copy link
Contributor

I hope this will help someone else to upgrade.

Demonstration of breaking change

class Banana < ApplicationRecord
end
# There is no BananaSerializer

0.10.2

render json: @banana # => {"id":1,"name":"a banana"}

0.10.3

render json: @banana # => {}

@mention-bot
Copy link

@jaredbeck, thanks for your PR! By analyzing the history of the files in this pull request, we identified @bf4, @groyoh and @CorainChicago to be potential reviewers.

@bf4
Copy link
Member

bf4 commented Feb 8, 2017

@jaredbeck Thanks for the bug report. That would be a good thing to get fixed before 0.10.5

@jaredbeck
Copy link
Contributor Author

@jaredbeck Thanks for the bug report. That would be a good thing to get fixed before 0.10.5

Oh! I didn't know I was writing a bug report! I thought this change was intentional. Maybe we could add still add it to the changelog, but under a "Known issues" heading instead of under "Breaking changes"?

@bf4
Copy link
Member

bf4 commented Feb 8, 2017

Yup!

@bf4
Copy link
Member

bf4 commented Feb 8, 2017

@jaredbeck was there a reason you thought it was intentional?

@jaredbeck
Copy link
Contributor Author

Yup!

OK, I'll push a new commit with that change.

@jaredbeck was there a reason you thought it was intentional?

Nah, no reason.

@jaredbeck
Copy link
Contributor Author

@jaredbeck was there a reason you thought it was intentional?

Nah, no reason.

Oh wait, yeah, there was a reason. The docs (howto/upgrade_from_0_8_to_0_10.md) say:

No default serializer when serializer doesn't exist
https://github.com/rails-api/active_model_serializers/blob/master/docs/howto/upgrade_from_0_8_to_0_10.md

@jaredbeck
Copy link
Contributor Author

Maybe we could add still add it to the changelog, but under a "Known issues" heading instead of under "Breaking changes"?

Yup!

OK, I'll push a new commit with that change.

New commit pushed, thanks.

@bf4
Copy link
Member

bf4 commented Feb 8, 2017

@jaredbeck

ah, what that should mean, is that we just call as_json on anything that doesn't have a serializer.

# From a comment in https://github.com/rails-api/active_model_serializers/pull/2026/files
     # BUG: per #2027, JSON API resource relationships are only id and type, and hence either
     # *require* a serializer or we need to be a little clever about figuring out the id/type.
     # In either case, returning the raw virtual value will almost always be incorrect.
     options[:virtual_value] = object.try(:as_json) || object

I hope this will help someone else to upgrade.

```ruby
class Banana < ApplicationRecord
end
```

```ruby
render json: @banana # => {"id":1,"name":"a banana"}
```

```ruby
render json: @banana # => {}
```

[ci skip]
@jaredbeck
Copy link
Contributor Author

ah, what that should mean, is that we just call as_json on anything that doesn't have a serializer.

I've added that, thanks.

Copy link
Member

@bf4 bf4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you could add a failing test, that would probably make the case easier to understand. I have an idea what might have causes the regression, but haven't tried to duplicate it.

- [#2048](https://github.com/rails-api/active_model_serializers/pull/2048)
The output of `render json: MyModel.new` has changed when no serializer
is found for `MyModel`. Now, we just call `as_json` on anything that doesn't
have a serializer.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling as_json when no serializer is found isn't new. What's new, is that you've describe a regression/change in render json: MyModel.new

@jaredbeck
Copy link
Contributor Author

I got the test suite running, but I'm having trouble writing a test that reproduces this issue. Here's what I tried.

diff --git a/test/action_controller/serialization_test.rb b/test/action_controller/serialization_test.rb
index dfd72b4..07fc4c2 100644
--- a/test/action_controller/serialization_test.rb
+++ b/test/action_controller/serialization_test.rb
@@ -5,6 +5,10 @@ module ActionController
     class ImplicitSerializerTest < ActionController::TestCase
       class ImplicitSerializationTestController < ActionController::Base
         include SerializationTesting
+        def render_model_without_serializer
+          render json: ModelWithoutSerializer.new(id: 1, name: 'Alice')
+        end
+
         def render_using_implicit_serializer
           @profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
           render json: @profile
@@ -144,6 +148,13 @@ module ActionController
 
       tests ImplicitSerializationTestController
 
+      def test_render_model_without_serializer
+        get :render_model_without_serializer
+        expected = { id: 1, name: 'Alice' }
+        assert_equal 'application/json', @response.content_type
+        assert_equal expected.to_json, @response.body
+      end
+
       # We just have Null for now, this will change
       def test_render_using_implicit_serializer
         get :render_using_implicit_serializer
diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb
index 6245ad2..563703c 100644
--- a/test/fixtures/poro.rb
+++ b/test/fixtures/poro.rb
@@ -223,3 +223,12 @@ class PaginatedSerializer < ActiveModel::Serializer::CollectionSerializer
     'paginated'
   end
 end
+
+# This model has no serializer. Specifically, there is no constant defined
+# named `ModelWithoutSerializerSerializer`
+class ModelWithoutSerializer < Model
+  attributes :id, :name
+end
+if defined?(ModelWithoutSerializerSerializer)
+  fail 'Expected to be undefined: ModelWithoutSerializerSerializer'
+end

Maybe this is only a problem in 0.10.3 and was fixed in 0.10.4? Or maybe there's something wrong with my test?

@bf4
Copy link
Member

bf4 commented Feb 20, 2017

@jaredbeck maybe better to make an AR class first to test comparison? What Rails and Ruby versions was this on? What JSON version?

@bf4
Copy link
Member

bf4 commented Feb 20, 2017

I tried making a test, but couldn't duplicate on earlier versions bundle exec m test/action_controller/regression_test.rb

require "test_helper"

module ActionController
  module Serialization
    class ImplicitSerializerTest < ActionController::TestCase
      class PostWithoutSerializer < ActiveRecord::Base
        self.table_name = :posts
      end
      class ImplicitSerializationTestController < ActionController::Base
        def render_record_without_serializer
          @post = PostWithoutSerializer.new(title: "Title", body: "Body")
          render json: @post
        end
      end

      tests ImplicitSerializationTestController

      def test_record_without_serializer
        with_adapter :json do
          get :render_record_without_serializer
        end

        expected = {}
        PostWithoutSerializer.column_names.each do |field| expected[field] ||= nil end
        expected.update(
          title: "Title",
          body: "Body"
        )

        assert_equal "application/json", @response.content_type
        assert_equal expected.to_json, @response.body
      end
    end
  end
end

I also failed to find a failure via bisecting git bisect run bundle check || bundle >/dev/null; COVERAGE_MINIMUM=0.0 bundle exec ruby -Ilib:test test/action_controller/regression_test.rb

or even

git rev-list --reverse v0.10.2..v0.10.4 | while read rev; do echo "Running $rev"; bundle check || bundle >/dev/null; COVERAGE_MINIMUM=0.0 bundle exec ruby -Ilib:test test/action_controller/regression_test.rb || exit 1; done

@jaredbeck
Copy link
Contributor Author

jaredbeck commented Feb 20, 2017

Benjamin, thanks for looking into this. I'm convinced now that my initial description was over-simplistic; there's something else going on. For now, I have a work-around: always define a serializer, and that's good enough for now. I'm going to close this, thanks again for your help.

@jaredbeck jaredbeck closed this Feb 20, 2017
@jaredbeck jaredbeck deleted the patch-1 branch February 20, 2017 20:12
@bf4
Copy link
Member

bf4 commented Feb 20, 2017

@jaredbeck I'll be happy to help debug if it's reproducible, if you want to screen share/ pair sometime.

@jaredbeck
Copy link
Contributor Author

@jaredbeck I'll be happy to help debug if it's reproducible, if you want to screen share/ pair sometime.

Thanks Benjamin, that's very generous. I can probably find a point in my app's commit history where it's reproducible, but let me spend some time making sure it's not my app doing something weird before I take up any more of your time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants