From c65cc65c3292076157d6ec9f767d36f9efe8d25f Mon Sep 17 00:00:00 2001 From: Sean Doyle Date: Sun, 5 Jan 2025 01:13:25 -0500 Subject: [PATCH] Support XML and JSON inflector acronyms Related to https://github.com/rails/activeresource/pull/361 Add special-case treatment of JSON and XML acronyms by declaring aliases for the `JsonFormat` and `XmlFormat` constants. In addition to the aliases, this commit also includes test coverage to cover existing behavior. The original PR included a comment citing that `ActiveSupport::Inflector::Inflections#clear` was not working as expected. It was resolved by [a76344f][], which was merged into `main` prior to the `7.0.0` release. Since the CI matrix currently includes `7-0-stable` as the minimum version, the code can rely on the resolved behavior. [a76344f]: https://github.com/rails/rails/commit/a76344ffc5a308e0ad6105fde921990f57fa0ea9 --- lib/active_resource/formats/json_format.rb | 2 + lib/active_resource/formats/xml_format.rb | 2 + test/cases/formats_test.rb | 44 ++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 test/cases/formats_test.rb diff --git a/lib/active_resource/formats/json_format.rb b/lib/active_resource/formats/json_format.rb index 67795f95e3..7898e67335 100644 --- a/lib/active_resource/formats/json_format.rb +++ b/lib/active_resource/formats/json_format.rb @@ -24,5 +24,7 @@ def decode(json) Formats.remove_root(ActiveSupport::JSON.decode(json)) end end + + JSONFormat = JsonFormat end end diff --git a/lib/active_resource/formats/xml_format.rb b/lib/active_resource/formats/xml_format.rb index 5fbf967e08..daac245d44 100644 --- a/lib/active_resource/formats/xml_format.rb +++ b/lib/active_resource/formats/xml_format.rb @@ -23,5 +23,7 @@ def decode(xml) Formats.remove_root(Hash.from_xml(xml)) end end + + XMLFormat = XmlFormat end end diff --git a/test/cases/formats_test.rb b/test/cases/formats_test.rb new file mode 100644 index 0000000000..7d11c5d8af --- /dev/null +++ b/test/cases/formats_test.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require "abstract_unit" + +class FormatsTest < ActiveSupport::TestCase + def test_json_format_uses_camelcase + assert_equal ActiveResource::Formats::JsonFormat, ActiveResource::Formats[:json] + end + + def test_xml_format_uses_camelcase + assert_equal ActiveResource::Formats::XmlFormat, ActiveResource::Formats[:xml] + end + + def test_custom_format_uses_camelcase + klass = Class.new + ActiveResource::Formats.const_set(:MsgpackFormat, klass) + + assert_equal klass, ActiveResource::Formats[:msgpack] + ensure + ActiveResource::Formats.send(:remove_const, :MsgpackFormat) + end + + def test_unknown_format_raises_not_found_error + assert_raises NameError, match: "uninitialized constant ActiveResource::Formats::MsgpackFormat" do + ActiveResource::Formats[:msgpack] + end + end + + def test_json_format_uses_acronym_inflections + ActiveSupport::Inflector.inflections { |inflect| inflect.acronym "JSON" } + + assert_equal ActiveResource::Formats::JsonFormat, ActiveResource::Formats[:json] + ensure + ActiveSupport::Inflector.inflections.clear :acronyms + end + + def test_xml_format_uses_acronym_inflections + ActiveSupport::Inflector.inflections { |inflect| inflect.acronym "XML" } + + assert_equal ActiveResource::Formats::XmlFormat, ActiveResource::Formats[:xml] + ensure + ActiveSupport::Inflector.inflections.clear :acronyms + end +end