@@ -4,6 +4,10 @@ module ActionController
44 module Serialization
55 class Json
66 class IncludeTest < ActionController ::TestCase
7+ INCLUDE_STRING = 'posts.comments' . freeze
8+ INCLUDE_HASH = { posts : :comments } . freeze
9+ DEEP_INCLUDE = 'posts.comments.author' . freeze
10+
711 class IncludeTestController < ActionController ::Base
812 def setup_data
913 ActionController ::Base . cache_store . clear
@@ -38,17 +42,28 @@ def render_without_include
3842
3943 def render_resource_with_include_hash
4044 setup_data
41- render json : @author , include : { posts : :comments } , adapter : :json
45+ render json : @author , include : INCLUDE_HASH , adapter : :json
4246 end
4347
4448 def render_resource_with_include_string
4549 setup_data
46- render json : @author , include : 'posts.comments' , adapter : :json
50+ render json : @author , include : INCLUDE_STRING , adapter : :json
4751 end
4852
4953 def render_resource_with_deep_include
5054 setup_data
51- render json : @author , include : 'posts.comments.author' , adapter : :json
55+ render json : @author , include : DEEP_INCLUDE , adapter : :json
56+ end
57+
58+ def render_without_recursive_relationships
59+ # testing recursive includes ('**') can't have any cycles in the
60+ # relationships, or we enter an infinite loop.
61+ author = Author . new ( id : 11 , name : 'Jane Doe' )
62+ post = Post . new ( id : 12 , title : 'Hello World' , body : 'My first post' )
63+ comment = Comment . new ( id : 13 , body : 'Commentary' )
64+ author . posts = [ post ]
65+ post . comments = [ comment ]
66+ render json : author
5267 end
5368 end
5469
@@ -77,34 +92,90 @@ def test_render_without_include
7792 def test_render_resource_with_include_hash
7893 get :render_resource_with_include_hash
7994 response = JSON . parse ( @response . body )
80- expected = {
81- 'author' => {
82- 'id' => 1 ,
83- 'name' => 'Steve K.' ,
95+
96+ assert_equal ( expected_include_response , response )
97+ end
98+
99+ def test_render_resource_with_include_string
100+ get :render_resource_with_include_string
101+
102+ response = JSON . parse ( @response . body )
103+
104+ assert_equal ( expected_include_response , response )
105+ end
106+
107+ def test_render_resource_with_deep_include
108+ get :render_resource_with_deep_include
109+
110+ response = JSON . parse ( @response . body )
111+
112+ assert_equal ( expected_deep_include_response , response )
113+ end
114+
115+ def test_render_with_empty_default_includes
116+ with_default_includes '' do
117+ get :render_without_include
118+ response = JSON . parse ( @response . body )
119+ expected = {
120+ 'author' => {
121+ 'id' => 1 ,
122+ 'name' => 'Steve K.'
123+ }
124+ }
125+ assert_equal ( expected , response )
126+ end
127+ end
128+
129+ def test_render_with_recursive_default_includes
130+ with_default_includes '**' do
131+ get :render_without_recursive_relationships
132+ response = JSON . parse ( @response . body )
133+
134+ expected = {
135+ 'id' => 11 ,
136+ 'name' => 'Jane Doe' ,
137+ 'roles' => nil ,
138+ 'bio' => nil ,
84139 'posts' => [
85140 {
86- 'id' => 42 , 'title' => 'New Post' , 'body' => 'Body' ,
141+ 'id' => 12 ,
142+ 'title' => 'Hello World' ,
143+ 'body' => 'My first post' ,
87144 'comments' => [
88145 {
89- 'id' => 1 , 'body' => 'ZOMG A COMMENT'
90- } ,
91- {
92- 'id ' => 2 , 'body' => 'ZOMG ANOTHER COMMENT'
146+ 'id' => 13 ,
147+ 'body' => 'Commentary' ,
148+ 'post' => nil , # not set to avoid infinite recursion
149+ 'author ' => nil , # not set to avoid infinite recursion
93150 }
94- ]
151+ ] ,
152+ 'blog' => {
153+ 'id' => 999 ,
154+ 'name' => 'Custom blog' ,
155+ 'writer' => nil ,
156+ 'articles' => nil
157+ } ,
158+ 'author' => nil # not set to avoid infinite recursion
95159 }
96160 ]
97161 }
98- }
162+ assert_equal ( expected , response )
163+ end
164+ end
99165
100- assert_equal ( expected , response )
166+ def test_render_with_includes_overrides_default_includes
167+ with_default_includes '' do
168+ get :render_resource_with_include_hash
169+ response = JSON . parse ( @response . body )
170+
171+ assert_equal ( expected_include_response , response )
172+ end
101173 end
102174
103- def test_render_resource_with_include_string
104- get :render_resource_with_include_string
175+ private
105176
106- response = JSON . parse ( @response . body )
107- expected = {
177+ def expected_include_response
178+ {
108179 'author' => {
109180 'id' => 1 ,
110181 'name' => 'Steve K.' ,
@@ -123,15 +194,10 @@ def test_render_resource_with_include_string
123194 ]
124195 }
125196 }
126-
127- assert_equal ( expected , response )
128197 end
129198
130- def test_render_resource_with_deep_include
131- get :render_resource_with_deep_include
132-
133- response = JSON . parse ( @response . body )
134- expected = {
199+ def expected_deep_include_response
200+ {
135201 'author' => {
136202 'id' => 1 ,
137203 'name' => 'Steve K.' ,
@@ -158,8 +224,21 @@ def test_render_resource_with_deep_include
158224 ]
159225 }
160226 }
227+ end
161228
162- assert_equal ( expected , response )
229+ def with_default_includes ( include_tree )
230+ original = ActiveModelSerializers . config . default_includes
231+ ActiveModelSerializers . config . default_includes = include_tree
232+ clear_include_tree_cache
233+ yield
234+ ensure
235+ ActiveModelSerializers . config . default_includes = original
236+ clear_include_tree_cache
237+ end
238+
239+ def clear_include_tree_cache
240+ ActiveModelSerializers
241+ . instance_variable_set ( :@default_include_tree , nil )
163242 end
164243 end
165244 end
0 commit comments