Skip to content
Open
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
24 changes: 22 additions & 2 deletions lib/jsonapi/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions test/fixtures/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Binary file added test/test_db-shm
Binary file not shown.
Binary file added test/test_db-wal
Binary file not shown.
61 changes: 40 additions & 21 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down