Skip to content

Commit a7e6dc9

Browse files
lukemeliaclaude
andcommitted
Patch jsonapi-resources 0.9.12 for Rails 7.2 compatibility
Port fixes from speee PR JSONAPI-Resources#1480 (cerebris/jsonapi-resources): - Fix Rack 3.0+ status code lookup: :unprocessable_entity was removed in favor of :unprocessable_content. Add Error.status_code_for class method with fallback mapping. - Fix ActiveSupport::Deprecation.warn becoming private in Rails 7.2. Add JSONAPI.warn_deprecated helper that falls back to Kernel#warn. - Fix lazy initialization of JSONAPI.configuration to use proper accessor pattern. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 8cdc9dd commit a7e6dc9

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

lib/jsonapi/configuration.rb

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,26 @@ def default_processor_klass=(default_processor_klass)
261261
end
262262

263263
class << self
264-
attr_accessor :configuration
265-
end
264+
attr_writer :configuration
266265

267-
@configuration ||= Configuration.new
266+
def configuration
267+
@configuration ||= Configuration.new
268+
end
269+
end
268270

269271
def self.configure
270-
yield(@configuration)
272+
yield(configuration)
273+
end
274+
275+
# Rails 7.2+ made ActiveSupport::Deprecation.warn a private method
276+
# This helper provides backward-compatible deprecation warnings
277+
def self.warn_deprecated(message)
278+
if defined?(ActiveSupport::Deprecation) && ActiveSupport::Deprecation.respond_to?(:warn)
279+
# Rails < 7.2
280+
ActiveSupport::Deprecation.warn(message)
281+
else
282+
# Rails 7.2+ or fallback
283+
warn "[DEPRECATION] #{message}"
284+
end
271285
end
272286
end

lib/jsonapi/error.rb

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

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

18-
@status = Rack::Utils::SYMBOL_TO_STATUS_CODE[options[:status]].to_s
38+
@status = self.class.status_code_for(options[:status])
1939
@meta = options[:meta]
2040
end
2141

lib/jsonapi/resource.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ def attribute(attribute_name, options = {})
541541
check_reserved_attribute_name(attr)
542542

543543
if (attr == :id) && (options[:format].nil?)
544-
ActiveSupport::Deprecation.warn('Id without format is no longer supported. Please remove ids from attributes, or specify a format.')
544+
JSONAPI.warn_deprecated('Id without format is no longer supported. Please remove ids from attributes, or specify a format.')
545545
end
546546

547547
check_duplicate_attribute_name(attr) if options[:format].nil?
@@ -581,11 +581,11 @@ def has_one(*attrs)
581581
end
582582

583583
def belongs_to(*attrs)
584-
ActiveSupport::Deprecation.warn "In #{name} you exposed a `has_one` relationship "\
585-
" using the `belongs_to` class method. We think `has_one`" \
586-
" is more appropriate. If you know what you're doing," \
587-
" and don't want to see this warning again, override the" \
588-
" `belongs_to` class method on your resource."
584+
JSONAPI.warn_deprecated "In #{name} you exposed a `has_one` relationship "\
585+
" using the `belongs_to` class method. We think `has_one`" \
586+
" is more appropriate. If you know what you're doing," \
587+
" and don't want to see this warning again, override the" \
588+
" `belongs_to` class method on your resource."
589589
_add_relationship(Relationship::ToOne, *attrs)
590590
end
591591

0 commit comments

Comments
 (0)