Skip to content

Commit bc783b5

Browse files
authored
Merge pull request #1271 from cerebris/warn_on_performance_issues
Warn on performance issues: non-normalized result set
2 parents defc701 + 8424271 commit bc783b5

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

lib/jsonapi/active_relation_resource.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ def find_fragments(filters, options = {})
181181
end
182182
end
183183

184+
if JSONAPI.configuration.warn_on_performance_issues && (rows.length > fragments.length)
185+
warn "Performance issue detected: `#{self.name.to_s}.records` returned non-normalized results in `#{self.name.to_s}.find_fragments`."
186+
end
187+
184188
fragments
185189
end
186190

lib/jsonapi/configuration.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Configuration
1010
:raise_if_parameters_not_allowed,
1111
:warn_on_route_setup_issues,
1212
:warn_on_missing_routes,
13+
:warn_on_performance_issues,
1314
:default_allow_include_to_one,
1415
:default_allow_include_to_many,
1516
:allow_sort,
@@ -60,6 +61,7 @@ def initialize
6061

6162
self.warn_on_route_setup_issues = true
6263
self.warn_on_missing_routes = true
64+
self.warn_on_performance_issues = true
6365

6466
# :none, :offset, :paged, or a custom paginator name
6567
self.default_paginator = :none
@@ -272,6 +274,8 @@ def allow_include=(allow_include)
272274

273275
attr_writer :warn_on_missing_routes
274276

277+
attr_writer :warn_on_performance_issues
278+
275279
attr_writer :use_relationship_reflection
276280

277281
attr_writer :resource_cache

test/controllers/controller_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3625,6 +3625,29 @@ def test_book_comments_exclude_unapproved_context_based
36253625
end
36263626
end
36273627

3628+
class Api::V4::PostsControllerTest < ActionController::TestCase
3629+
def test_warn_on_joined_to_many
3630+
original_config = JSONAPI.configuration.dup
3631+
3632+
JSONAPI.configuration.warn_on_performance_issues = true
3633+
_out, err = capture_subprocess_io do
3634+
get :index, params: {fields: {posts: 'id,title'}}
3635+
assert_response :success
3636+
end
3637+
assert_equal(err, "Performance issue detected: `Api::V4::PostResource.records` returned non-normalized results in `Api::V4::PostResource.find_fragments`.\n")
3638+
3639+
JSONAPI.configuration.warn_on_performance_issues = false
3640+
_out, err = capture_subprocess_io do
3641+
get :index, params: {fields: {posts: 'id,title'}}
3642+
assert_response :success
3643+
end
3644+
assert_empty err
3645+
3646+
ensure
3647+
JSONAPI.configuration = original_config
3648+
end
3649+
end
3650+
36283651
class Api::V4::BooksControllerTest < ActionController::TestCase
36293652
def setup
36303653
JSONAPI.configuration.json_key_format = :camelized_key

test/fixtures/active_record.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1989,7 +1989,15 @@ class PreferencesResource < PreferencesResource; end
19891989

19901990
module Api
19911991
module V4
1992-
class PostResource < PostResource; end
1992+
class PostResource < PostResource
1993+
class << self
1994+
def records(options = {})
1995+
# Sets up a performance issue for testing
1996+
super(options).joins(:comments)
1997+
end
1998+
end
1999+
end
2000+
19932001
class PersonResource < PersonResource; end
19942002
class ExpenseEntryResource < ExpenseEntryResource; end
19952003
class IsoCurrencyResource < IsoCurrencyResource

0 commit comments

Comments
 (0)