diff --git a/lib/jsonapi/error.rb b/lib/jsonapi/error.rb index 12d65f58..cfc04eed 100644 --- a/lib/jsonapi/error.rb +++ b/lib/jsonapi/error.rb @@ -4,6 +4,26 @@ module JSONAPI class Error attr_accessor :title, :detail, :id, :href, :code, :source, :links, :status, :meta + # Rack 3.0+ deprecated :unprocessable_entity in favor of :unprocessable_content + # This mapping ensures compatibility across Rack versions + DEPRECATED_STATUS_SYMBOLS = { + unprocessable_entity: :unprocessable_content + }.freeze + + def self.status_code_for(status_symbol) + return nil if status_symbol.nil? + + # Try the symbol directly first + code = Rack::Utils::SYMBOL_TO_STATUS_CODE[status_symbol] + + # If not found and it's a deprecated symbol, try the new symbol + if code.nil? && DEPRECATED_STATUS_SYMBOLS.key?(status_symbol) + code = Rack::Utils::SYMBOL_TO_STATUS_CODE[DEPRECATED_STATUS_SYMBOLS[status_symbol]] + end + + code&.to_s + end + def initialize(options = {}) @title = options[:title] @detail = options[:detail] @@ -17,7 +37,7 @@ def initialize(options = {}) @source = options[:source] @links = options[:links] - @status = Rack::Utils::SYMBOL_TO_STATUS_CODE[options[:status]].to_s + @status = self.class.status_code_for(options[:status]) @meta = options[:meta] end @@ -48,7 +68,7 @@ def update_with_overrides(error_object_overrides) if error_object_overrides[:status] # :nocov: - @status = Rack::Utils::SYMBOL_TO_STATUS_CODE[error_object_overrides[:status]].to_s + @status = self.class.status_code_for(error_object_overrides[:status]) # :nocov: end @meta = error_object_overrides[:meta] || @meta diff --git a/test/fixtures/active_record.rb b/test/fixtures/active_record.rb index f8959317..85d764fd 100644 --- a/test/fixtures/active_record.rb +++ b/test/fixtures/active_record.rb @@ -52,7 +52,7 @@ end create_table :posts, force: true do |t| - t.string :title, length: 255 + t.string :title, limit: 255 t.text :body t.integer :author_id t.integer :parent_post_id @@ -324,8 +324,8 @@ create_table :related_things, force: true do |t| t.string :name - t.references :from, references: :thing - t.references :to, references: :thing + t.references :from, foreign_key: false + t.references :to, foreign_key: false t.timestamps null: false end diff --git a/test/test_db-shm b/test/test_db-shm new file mode 100644 index 00000000..930f51a1 Binary files /dev/null and b/test/test_db-shm differ diff --git a/test/test_db-wal b/test/test_db-wal new file mode 100644 index 00000000..3e6b47c1 Binary files /dev/null and b/test/test_db-wal differ diff --git a/test/test_helper.rb b/test/test_helper.rb index 953c8be7..01f90e20 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -47,29 +47,10 @@ ENV['DATABASE_URL'] ||= "sqlite3:test_db" require 'active_record/railtie' -require 'rails/test_help' -require 'minitest/mock' -require 'jsonapi-resources' -require 'pry' - -require File.expand_path('../helpers/value_matchers', __FILE__) -require File.expand_path('../helpers/assertions', __FILE__) -require File.expand_path('../helpers/functional_helpers', __FILE__) -require File.expand_path('../helpers/configuration_helpers', __FILE__) +# Rails 7.1+ requires the application to be defined before requiring rails/test_help Rails.env = 'test' -I18n.load_path += Dir[File.expand_path("../../locales/*.yml", __FILE__)] -I18n.enforce_available_locales = false - -JSONAPI.configure do |config| - config.json_key_format = :camelized_key -end - -ActiveSupport::Deprecation.silenced = true - -puts "Testing With RAILS VERSION #{Rails.version}" - class TestApp < Rails::Application config.eager_load = false config.root = File.dirname(__FILE__) @@ -91,6 +72,39 @@ class TestApp < Rails::Application end end +# Rails 7.1+ requires the application to be initialized before requiring rails/test_help +# Earlier versions require the opposite order +if Rails::VERSION::MAJOR >= 8 || (Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR >= 1) + TestApp.initialize! +end + +require 'rails/test_help' + +# Initialize app for Rails < 7.1 (for Rails 7.1+ it was already initialized above) +unless Rails::VERSION::MAJOR >= 8 || (Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR >= 1) + TestApp.initialize! +end + +require 'minitest/mock' +require 'jsonapi-resources' +require 'pry' + +require File.expand_path('../helpers/value_matchers', __FILE__) +require File.expand_path('../helpers/assertions', __FILE__) +require File.expand_path('../helpers/functional_helpers', __FILE__) +require File.expand_path('../helpers/configuration_helpers', __FILE__) + +I18n.load_path += Dir[File.expand_path("../../locales/*.yml", __FILE__)] +I18n.enforce_available_locales = false + +JSONAPI.configure do |config| + config.json_key_format = :camelized_key +end + +ActiveSupport::Deprecation.silenced = true + +puts "Testing With RAILS VERSION #{Rails.version}" + DatabaseCleaner.allow_remote_database_url = true DatabaseCleaner.strategy = :transaction @@ -214,10 +228,15 @@ def show_queries end end -TestApp.initialize! +# TestApp.initialize! already called earlier for Rails 7.1+ compatibility require File.expand_path('../fixtures/active_record', __FILE__) +# Disable foreign key constraints for SQLite in test environment (Rails 7.1+) +# This allows force: true to drop and recreate tables without constraint errors +# Must be called after active_record.rb which sets up the schema +ActiveRecord::Base.connection.execute("PRAGMA foreign_keys = OFF") if ActiveRecord::Base.connection.adapter_name == 'SQLite' + module Pets module V1 class CatsController < JSONAPI::ResourceController