Skip to content

Commit bcbadc9

Browse files
author
Yohan Robert
committed
Prevent loading association when include_data is set to false
This should fix #1707.
1 parent cbca135 commit bcbadc9

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Breaking changes:
77
Features:
88

99
Fixes:
10+
- [#1710](https://github.com/rails-api/active_model_serializers/pull/1710) Prevent association loading when `include_data` option
11+
is set to `false`. (@groyoh)
1012

1113
Misc:
1214
- [#1734](https://github.com/rails-api/active_model_serializers/pull/1734) Adds documentation for conditional attribute (@lambda2)

lib/active_model/serializer/reflection.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ def value(serializer)
7575

7676
if block
7777
block_value = instance_exec(serializer, &block)
78-
if block_value == :nil
79-
serializer.read_attribute_for_serialization(name)
80-
else
78+
if block_value != :nil
8179
block_value
80+
elsif @_include_data
81+
serializer.read_attribute_for_serialization(name)
8282
end
8383
else
8484
serializer.read_attribute_for_serialization(name)

test/adapter/json_api/relationships_test.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ class Serializer
55
module Adapter
66
class JsonApi
77
class RelationshipTest < ActiveSupport::TestCase
8-
RelationshipAuthor = Class.new(::Model)
8+
class RelationshipAuthor < ::Model; end
9+
910
class RelationshipAuthorSerializer < ActiveModel::Serializer
1011
has_one :bio do
1112
link :self, '//example.com/link_author/relationships/bio'
@@ -71,7 +72,6 @@ def cached_roles
7172

7273
def setup
7374
@post = Post.new(id: 1337, comments: [], author: nil)
74-
@blog = Blog.new(id: 1337, name: 'extra')
7575
@bio = Bio.new(id: 1337)
7676
@like = Like.new(id: 1337)
7777
@role = Role.new(id: 'from-record')
@@ -82,7 +82,6 @@ def setup
8282
@author = RelationshipAuthor.new(
8383
id: 1337,
8484
posts: [@post],
85-
blog: @blog,
8685
reviewer: @reviewer,
8786
bio: @bio,
8887
likes: [@like],
@@ -158,10 +157,16 @@ def test_relationship_meta
158157
end
159158

160159
def test_relationship_not_including_data
160+
@author.define_singleton_method(:read_attribute_for_serialization) do |attr|
161+
fail 'should not be called' if attr == :blog
162+
super(attr)
163+
end
161164
expected = {
162165
links: { self: '//example.com/link_author/relationships/blog' }
163166
}
164-
assert_relationship(:blog, expected)
167+
assert_nothing_raised do
168+
assert_relationship(:blog, expected)
169+
end
165170
end
166171

167172
def test_relationship_including_data_explicit

0 commit comments

Comments
 (0)