Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/jsonapi/active_relation_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ def find_fragments(filters, options = {})
end
end

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

fragments
end

Expand Down
4 changes: 4 additions & 0 deletions lib/jsonapi/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Configuration
:raise_if_parameters_not_allowed,
:warn_on_route_setup_issues,
:warn_on_missing_routes,
:warn_on_performance_issues,
:default_allow_include_to_one,
:default_allow_include_to_many,
:allow_sort,
Expand Down Expand Up @@ -60,6 +61,7 @@ def initialize

self.warn_on_route_setup_issues = true
self.warn_on_missing_routes = true
self.warn_on_performance_issues = true

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

attr_writer :warn_on_missing_routes

attr_writer :warn_on_performance_issues

attr_writer :use_relationship_reflection

attr_writer :resource_cache
Expand Down
23 changes: 23 additions & 0 deletions test/controllers/controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3625,6 +3625,29 @@ def test_book_comments_exclude_unapproved_context_based
end
end

class Api::V4::PostsControllerTest < ActionController::TestCase
def test_warn_on_joined_to_many
original_config = JSONAPI.configuration.dup

JSONAPI.configuration.warn_on_performance_issues = true
_out, err = capture_subprocess_io do
get :index, params: {fields: {posts: 'id,title'}}
assert_response :success
end
assert_equal(err, "Performance issue detected: `Api::V4::PostResource.records` returned non-normalized results in `Api::V4::PostResource.find_fragments`.\n")

JSONAPI.configuration.warn_on_performance_issues = false
_out, err = capture_subprocess_io do
get :index, params: {fields: {posts: 'id,title'}}
assert_response :success
end
assert_empty err

ensure
JSONAPI.configuration = original_config
end
end

class Api::V4::BooksControllerTest < ActionController::TestCase
def setup
JSONAPI.configuration.json_key_format = :camelized_key
Expand Down
10 changes: 9 additions & 1 deletion test/fixtures/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1989,7 +1989,15 @@ class PreferencesResource < PreferencesResource; end

module Api
module V4
class PostResource < PostResource; end
class PostResource < PostResource
class << self
def records(options = {})
# Sets up a performance issue for testing
super(options).joins(:comments)
end
end
end

class PersonResource < PersonResource; end
class ExpenseEntryResource < ExpenseEntryResource; end
class IsoCurrencyResource < IsoCurrencyResource
Expand Down