Skip to content

Fix format lookup when JSON or XML are defined as acronyms #361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 8 additions & 1 deletion lib/active_resource/formats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ module Formats
autoload :XmlFormat, "active_resource/formats/xml_format"
autoload :JsonFormat, "active_resource/formats/json_format"

BUILT_IN = HashWithIndifferentAccess.new(
xml: ActiveResource::Formats::XmlFormat,
json: ActiveResource::Formats::JsonFormat,
).freeze

# Lookup the format class from a mime type reference symbol. Example:
#
# ActiveResource::Formats[:xml] # => ActiveResource::Formats::XmlFormat
# ActiveResource::Formats[:json] # => ActiveResource::Formats::JsonFormat
def self.[](mime_type_reference)
ActiveResource::Formats.const_get(ActiveSupport::Inflector.camelize(mime_type_reference.to_s) + "Format")
BUILT_IN.fetch(mime_type_reference) do
ActiveResource::Formats.const_get(ActiveSupport::Inflector.camelize(mime_type_reference.to_s) + "Format")
end
end

def self.remove_root(data)
Expand Down
31 changes: 31 additions & 0 deletions test/lib/formats_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require "abstract_unit"

class FormatsTest < ActiveSupport::TestCase
def test_resolving_json_and_xml_formats_when_defined_as_acronyms
ActiveSupport::Inflector.inflections do |inflect|
inflect.acronym "JSON"
inflect.acronym "XML"
end

assert_equal ActiveResource::Formats::JsonFormat, ActiveResource::Formats[:json]
assert_equal ActiveResource::Formats::XmlFormat, ActiveResource::Formats[:xml]
ensure
ActiveSupport::Inflector.inflections do |inflect|
# N.B. It's not yet possible to use the public ActiveSupport::Inflector::Inflections#clear
# API because it doesn't reset @acronyms to be a Hash (the default value),
# instead it sets an empty array.
Copy link
Author

@odlp odlp Jun 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Planning to contribute a PR to Rails shortly to fix this.

Update: rails/rails#42475

inflect.instance_variable_set(:@acronyms, {})
end
end

def test_resolving_custom_formats_uses_const_get
klass = Class.new
ActiveResource::Formats.const_set(:MsgpackFormat, klass)

assert_equal klass, ActiveResource::Formats[:msgpack]
ensure
ActiveResource::Formats.send(:remove_const, :MsgpackFormat)
end
end