@@ -509,6 +509,22 @@ def test_show_single_with_includes
509509 assert_equal 2 , json_response [ 'included' ] . size
510510 end
511511
512+ def test_show_with_filtered_includes_when_not_eager_loaded
513+ # tags are not eagerly loaded on a post but may still be filtered
514+ get :show , params : { id : '1' , include : 'tags' , filter : { 'tags.name' => [ 'whiny' ] } }
515+ assert_response :success
516+ assert_equal 1 , json_response [ 'included' ] . size
517+ end
518+
519+ def test_show_with_filtered_includes_when_not_eager_loaded_and_all_filtered_out
520+ get :show , params : { id : '1' , include : 'tags' , filter : { 'tags.name' => [ 'no-tag-with-this-name' ] } }
521+ assert_response :success
522+ assert json_response [ 'data' ] . is_a? ( Hash )
523+ assert_equal 'New post' , json_response [ 'data' ] [ 'attributes' ] [ 'title' ]
524+ assert_equal 'A body!!!' , json_response [ 'data' ] [ 'attributes' ] [ 'body' ]
525+ assert_nil json_response [ 'included' ]
526+ end
527+
512528 def test_show_single_with_include_disallowed
513529 JSONAPI . configuration . allow_include = false
514530 assert_cacheable_get :show , params : { id : '1' , include : 'comments' }
@@ -2577,6 +2593,50 @@ def test_index_with_caching_enabled_uses_context
25772593 end
25782594end
25792595
2596+ class Api ::V5 ::PaintersControllerTest < ActionController ::TestCase
2597+ def test_index_with_included_resources_with_filters
2598+ # There are two painters, but by filtering the included relationship, the
2599+ # painters are limited due to the join, thus only the painter with oil
2600+ # paintings is returned.
2601+ get :index , params : { include : 'paintings' , filter : { 'paintings.category' => 'oil' } }
2602+ assert_response :success
2603+ assert_equal 1 , json_response [ 'data' ] . size , 'Size of data is wrong'
2604+ assert_equal '1' , json_response [ 'data' ] [ 0 ] [ 'id' ]
2605+ assert_equal 2 , json_response [ 'included' ] . size , 'Size of included data is wrong'
2606+ assert_equal '4' , json_response [ 'included' ] [ 0 ] [ 'id' ]
2607+ assert_equal '5' , json_response [ 'included' ] [ 1 ] [ 'id' ]
2608+ end
2609+
2610+ def test_index_with_filters_and_included_resources_with_filters
2611+ get :index , params : { include : 'paintings' , filter : { 'name' => 'Wyspianski' , 'paintings.category' => 'oil' } }
2612+
2613+ assert_response :success
2614+ assert_equal 1 , json_response [ 'data' ] . size
2615+ assert_equal '1' , json_response [ 'data' ] [ 0 ] [ 'id' ]
2616+ assert_equal 2 , json_response [ 'included' ] . size
2617+ assert_equal '4' , json_response [ 'included' ] [ 0 ] [ 'id' ]
2618+ end
2619+
2620+ def test_index_with_filters_and_included_resources_with_multiple_filters
2621+ # Painting 5 is the genuine, but painting 6 is a fake. Verify that multiple nested filters are merged and only the oil painting is returned.
2622+ get :index , params : { include : 'paintings' , filter : { 'name' => 'Wyspianski' , 'paintings.category' => 'oil' , 'paintings.title' => 'Motherhood' } }
2623+
2624+ assert_response :success
2625+ assert_equal 1 , json_response [ 'data' ] . size
2626+ assert_equal '1' , json_response [ 'data' ] [ 0 ] [ 'id' ]
2627+ assert_equal 1 , json_response [ 'included' ] . size
2628+ assert_equal '5' , json_response [ 'included' ] [ 0 ] [ 'id' ]
2629+ end
2630+
2631+ def test_show_with_filters_and_included_resources_with_filters
2632+ get :show , params : { id : 1 , include : 'paintings' , filter : { 'paintings.category' => 'oil' } }
2633+ assert_response :success
2634+ assert_equal '1' , json_response [ 'data' ] [ 'id' ]
2635+ assert_equal 2 , json_response [ 'included' ] . size
2636+ assert_equal '4' , json_response [ 'included' ] [ 0 ] [ 'id' ]
2637+ end
2638+ end
2639+
25802640class Api ::V5 ::AuthorsControllerTest < ActionController ::TestCase
25812641 def test_get_person_as_author
25822642 assert_cacheable_get :index , params : { filter : { id : '1' } }
0 commit comments