mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Prevent loading association when include_data is set to false (#1710)
This should fix #1707.
This commit is contained in:
parent
cbca1350b9
commit
a701777bd5
@ -7,6 +7,8 @@ Breaking changes:
|
|||||||
Features:
|
Features:
|
||||||
|
|
||||||
Fixes:
|
Fixes:
|
||||||
|
- [#1710](https://github.com/rails-api/active_model_serializers/pull/1710) Prevent association loading when `include_data` option
|
||||||
|
is set to `false`. (@groyoh)
|
||||||
|
|
||||||
Misc:
|
Misc:
|
||||||
- [#1734](https://github.com/rails-api/active_model_serializers/pull/1734) Adds documentation for conditional attribute (@lambda2)
|
- [#1734](https://github.com/rails-api/active_model_serializers/pull/1734) Adds documentation for conditional attribute (@lambda2)
|
||||||
|
|||||||
@ -75,10 +75,10 @@ module ActiveModel
|
|||||||
|
|
||||||
if block
|
if block
|
||||||
block_value = instance_exec(serializer, &block)
|
block_value = instance_exec(serializer, &block)
|
||||||
if block_value == :nil
|
if block_value != :nil
|
||||||
serializer.read_attribute_for_serialization(name)
|
|
||||||
else
|
|
||||||
block_value
|
block_value
|
||||||
|
elsif @_include_data
|
||||||
|
serializer.read_attribute_for_serialization(name)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
serializer.read_attribute_for_serialization(name)
|
serializer.read_attribute_for_serialization(name)
|
||||||
|
|||||||
@ -5,7 +5,8 @@ module ActiveModel
|
|||||||
module Adapter
|
module Adapter
|
||||||
class JsonApi
|
class JsonApi
|
||||||
class RelationshipTest < ActiveSupport::TestCase
|
class RelationshipTest < ActiveSupport::TestCase
|
||||||
RelationshipAuthor = Class.new(::Model)
|
class RelationshipAuthor < ::Model; end
|
||||||
|
|
||||||
class RelationshipAuthorSerializer < ActiveModel::Serializer
|
class RelationshipAuthorSerializer < ActiveModel::Serializer
|
||||||
has_one :bio do
|
has_one :bio do
|
||||||
link :self, '//example.com/link_author/relationships/bio'
|
link :self, '//example.com/link_author/relationships/bio'
|
||||||
@ -71,7 +72,6 @@ module ActiveModel
|
|||||||
|
|
||||||
def setup
|
def setup
|
||||||
@post = Post.new(id: 1337, comments: [], author: nil)
|
@post = Post.new(id: 1337, comments: [], author: nil)
|
||||||
@blog = Blog.new(id: 1337, name: 'extra')
|
|
||||||
@bio = Bio.new(id: 1337)
|
@bio = Bio.new(id: 1337)
|
||||||
@like = Like.new(id: 1337)
|
@like = Like.new(id: 1337)
|
||||||
@role = Role.new(id: 'from-record')
|
@role = Role.new(id: 'from-record')
|
||||||
@ -82,7 +82,6 @@ module ActiveModel
|
|||||||
@author = RelationshipAuthor.new(
|
@author = RelationshipAuthor.new(
|
||||||
id: 1337,
|
id: 1337,
|
||||||
posts: [@post],
|
posts: [@post],
|
||||||
blog: @blog,
|
|
||||||
reviewer: @reviewer,
|
reviewer: @reviewer,
|
||||||
bio: @bio,
|
bio: @bio,
|
||||||
likes: [@like],
|
likes: [@like],
|
||||||
@ -158,11 +157,17 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_relationship_not_including_data
|
def test_relationship_not_including_data
|
||||||
|
@author.define_singleton_method(:read_attribute_for_serialization) do |attr|
|
||||||
|
fail 'should not be called' if attr == :blog
|
||||||
|
super(attr)
|
||||||
|
end
|
||||||
expected = {
|
expected = {
|
||||||
links: { self: '//example.com/link_author/relationships/blog' }
|
links: { self: '//example.com/link_author/relationships/blog' }
|
||||||
}
|
}
|
||||||
|
assert_nothing_raised do
|
||||||
assert_relationship(:blog, expected)
|
assert_relationship(:blog, expected)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_relationship_including_data_explicit
|
def test_relationship_including_data_explicit
|
||||||
expected = {
|
expected = {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user