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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ when 'master'
gem 'arel', { git: 'https://github.com/rails/arel.git' }
when 'default'
gem 'railties', '>= 6.0'
gem 'csv' # Required by Rails 8.1+ (removed from standard library)
else
gem 'railties', "~> #{version}"
end
16 changes: 14 additions & 2 deletions lib/jsonapi/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ def errors
raise NotImplementedError, "Subclass of Error must implement errors method"
# :nocov:
end

private

# Rails 8.0+ deprecated :unprocessable_entity in favor of :unprocessable_content
# This helper maintains backward compatibility with earlier Rails versions
def unprocessable_status
if Rails::VERSION::MAJOR >= 8
:unprocessable_content
else
:unprocessable_entity
end
end
end

class Errors < Error
Expand Down Expand Up @@ -498,7 +510,7 @@ def errors

def json_api_error(attr_key, message)
create_error_object(code: JSONAPI::VALIDATION_ERROR,
status: :unprocessable_entity,
status: unprocessable_status,
title: message,
detail: detail(attr_key, message),
source: { pointer: pointer(attr_key) },
Expand Down Expand Up @@ -532,7 +544,7 @@ def general_error?(attr_key)
class SaveFailed < Error
def errors
[create_error_object(code: JSONAPI::SAVE_FAILED,
status: :unprocessable_entity,
status: unprocessable_status,
title: I18n.translate('jsonapi-resources.exceptions.save_failed.title',
default: 'Save failed or was cancelled'),
detail: I18n.translate('jsonapi-resources.exceptions.save_failed.detail',
Expand Down
18 changes: 9 additions & 9 deletions test/controllers/controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ def test_create_link_to_missing_object
}
}

assert_response :unprocessable_entity
assert_response unprocessable_status
# TODO: check if this validation is working
assert_match /author - can't be blank/, response.body
assert_nil response.location
Expand Down Expand Up @@ -864,7 +864,7 @@ def test_create_with_invalid_data
}
}

assert_response :unprocessable_entity
assert_response unprocessable_status

assert_equal "/data/relationships/author", json_response['errors'][0]['source']['pointer']
assert_equal "can't be blank", json_response['errors'][0]['title']
Expand Down Expand Up @@ -2019,7 +2019,7 @@ def test_delete_with_validation_error_base

assert_equal "can't destroy me", json_response['errors'][0]['title']
assert_equal "/data", json_response['errors'][0]['source']['pointer']
assert_response :unprocessable_entity
assert_response unprocessable_status
end

def test_delete_with_validation_error_attr
Expand All @@ -2028,7 +2028,7 @@ def test_delete_with_validation_error_attr

assert_equal "is locked", json_response['errors'][0]['title']
assert_equal "/data/attributes/title", json_response['errors'][0]['source']['pointer']
assert_response :unprocessable_entity
assert_response unprocessable_status
end

def test_delete_single
Expand Down Expand Up @@ -2631,7 +2631,7 @@ def test_create_validations_missing_attribute
}
}

assert_response :unprocessable_entity
assert_response unprocessable_status
assert_equal 2, json_response['errors'].size
assert_equal JSONAPI::VALIDATION_ERROR, json_response['errors'][0]['code']
assert_equal JSONAPI::VALIDATION_ERROR, json_response['errors'][1]['code']
Expand All @@ -2653,7 +2653,7 @@ def test_update_validations_missing_attribute
}
}

assert_response :unprocessable_entity
assert_response unprocessable_status
assert_equal 1, json_response['errors'].size
assert_equal JSONAPI::VALIDATION_ERROR, json_response['errors'][0]['code']
assert_match /name - can't be blank/, response.body
Expand Down Expand Up @@ -3183,7 +3183,7 @@ def test_create_with_invalid_data
}
}

assert_response :unprocessable_entity
assert_response unprocessable_status

assert_equal "/data/attributes/spouse-name", json_response['errors'][0]['source']['pointer']
assert_equal "can't be blank", json_response['errors'][0]['title']
Expand Down Expand Up @@ -3781,7 +3781,7 @@ def test_save_model_callbacks_fail
}
}

assert_response :unprocessable_entity
assert_response unprocessable_status
assert_match /Save failed or was cancelled/, json_response['errors'][0]['detail']
end
end
Expand Down Expand Up @@ -4079,7 +4079,7 @@ def test_delete_with_validation_error_base_on_resource

assert_equal "can't destroy me", json_response['errors'][0]['title']
assert_equal "/data/attributes/base", json_response['errors'][0]['source']['pointer']
assert_response :unprocessable_entity
assert_response unprocessable_status
end
end

Expand Down
10 changes: 10 additions & 0 deletions test/helpers/functional_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,15 @@ module FunctionalHelpers
def json_response
JSON.parse(@response.body)
end

# Rails 8.0+ deprecated :unprocessable_entity in favor of :unprocessable_content
# This helper maintains backward compatibility in tests
def unprocessable_status
if Rails::VERSION::MAJOR >= 8
:unprocessable_content
else
:unprocessable_entity
end
end
end
end
10 changes: 10 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,16 @@ def assert_jsonapi_response(expected_status, msg = nil)
assert_equal expected_status, status, msg
end

# Rails 8.0+ deprecated :unprocessable_entity in favor of :unprocessable_content
# This helper maintains backward compatibility in tests
def unprocessable_status
if Rails::VERSION::MAJOR >= 8
:unprocessable_content
else
:unprocessable_entity
end
end

def assert_jsonapi_get(url, msg = "GET response must be 200")
get url, headers: { 'Accept' => JSONAPI::MEDIA_TYPE }
assert_jsonapi_response 200, msg
Expand Down
14 changes: 1 addition & 13 deletions test/unit/active_relation_resource_finder/join_manager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,7 @@
class JoinTreeTest < ActiveSupport::TestCase

def db_true
case ActiveRecord::Base.connection.adapter_name
when 'SQLite'
if Rails::VERSION::MAJOR >= 8 && Rails::VERSION::MINOR >= 1
# Rails 8.1+ SQLite uses TRUE instead of 1
"TRUE"
elsif Rails::VERSION::MAJOR >= 6 || (Rails::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 2)
"1"
else
"'t'"
end
when 'PostgreSQL'
'TRUE'
end
ActiveRecord::Base.connection.quoted_true
end

def test_no_added_joins
Expand Down