Skip to content

Commit 5e53504

Browse files
takaokoujiclaude
andcommitted
Complete Rails 7.1.6 support with Rack 3.0 compatibility
Fix status code handling for Rack 3.0+ compatibility: Root cause: - Rack 3.0+ deprecated :unprocessable_entity symbol in favor of :unprocessable_content - When JSONAPI::Error looked up :unprocessable_entity in Rack::Utils::SYMBOL_TO_STATUS_CODE, it returned nil, causing response status to be 0 instead of 422 - This broke 10 tests that expected proper error status codes Solution: - Add DEPRECATED_STATUS_SYMBOLS mapping in JSONAPI::Error - Implement status_code_for() helper method that: 1. Tries the symbol directly first (works for all non-deprecated symbols) 2. Falls back to new symbol if deprecated symbol not found 3. Returns nil-safe string result - Apply helper to both initialize() and update_with_overrides() methods This maintains full backward compatibility: - Rails 6.1 with Rack 2.x: :unprocessable_entity works directly - Rails 7.0 with Rack 2.x: :unprocessable_entity works directly - Rails 7.1+ with Rack 3.x: :unprocessable_entity maps to :unprocessable_content Test results: - Rails 6.1.7.10: ✅ 674/674 tests passing - Rails 7.0.10: ✅ 674/674 tests passing - Rails 7.1.6: ✅ 674/674 tests passing 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 1f84070 commit 5e53504

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

lib/jsonapi/error.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,26 @@ module JSONAPI
44
class Error
55
attr_accessor :title, :detail, :id, :href, :code, :source, :links, :status, :meta
66

7+
# Rack 3.0+ deprecated :unprocessable_entity in favor of :unprocessable_content
8+
# This mapping ensures compatibility across Rack versions
9+
DEPRECATED_STATUS_SYMBOLS = {
10+
unprocessable_entity: :unprocessable_content
11+
}.freeze
12+
13+
def self.status_code_for(status_symbol)
14+
return nil if status_symbol.nil?
15+
16+
# Try the symbol directly first
17+
code = Rack::Utils::SYMBOL_TO_STATUS_CODE[status_symbol]
18+
19+
# If not found and it's a deprecated symbol, try the new symbol
20+
if code.nil? && DEPRECATED_STATUS_SYMBOLS.key?(status_symbol)
21+
code = Rack::Utils::SYMBOL_TO_STATUS_CODE[DEPRECATED_STATUS_SYMBOLS[status_symbol]]
22+
end
23+
24+
code&.to_s
25+
end
26+
727
def initialize(options = {})
828
@title = options[:title]
929
@detail = options[:detail]
@@ -17,7 +37,7 @@ def initialize(options = {})
1737
@source = options[:source]
1838
@links = options[:links]
1939

20-
@status = Rack::Utils::SYMBOL_TO_STATUS_CODE[options[:status]].to_s
40+
@status = self.class.status_code_for(options[:status])
2141
@meta = options[:meta]
2242
end
2343

@@ -48,7 +68,7 @@ def update_with_overrides(error_object_overrides)
4868

4969
if error_object_overrides[:status]
5070
# :nocov:
51-
@status = Rack::Utils::SYMBOL_TO_STATUS_CODE[error_object_overrides[:status]].to_s
71+
@status = self.class.status_code_for(error_object_overrides[:status])
5272
# :nocov:
5373
end
5474
@meta = error_object_overrides[:meta] || @meta

test/test_db-shm

0 Bytes
Binary file not shown.

test/test_db-wal

-8.05 KB
Binary file not shown.

0 commit comments

Comments
 (0)