From 1e70807a70d45da4e5bcdc47cb1fc2a4322eca51 Mon Sep 17 00:00:00 2001 From: Miller Date: Mon, 22 Apr 2024 22:12:51 +0900 Subject: [PATCH] Support hexBinary format in XML --- activesupport/CHANGELOG.md | 4 ++++ activesupport/lib/active_support/xml_mini.rb | 8 +++++++- activesupport/test/xml_mini_test.rb | 13 +++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index b90c5df6a78e6..51380f031941c 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,7 @@ +* Support `hexBinary` type in `ActiveSupport::XmlMini`. + + *heka1024* + * `stub_const` now accepts a `exists: false` parameter to allow stubbing missing constants. *Jean Boussier* diff --git a/activesupport/lib/active_support/xml_mini.rb b/activesupport/lib/active_support/xml_mini.rb index 60d155eb5ecdf..ff3a4202ad182 100644 --- a/activesupport/lib/active_support/xml_mini.rb +++ b/activesupport/lib/active_support/xml_mini.rb @@ -79,6 +79,7 @@ def content_type "string" => Proc.new { |string| string.to_s }, "yaml" => Proc.new { |yaml| YAML.load(yaml) rescue yaml }, "base64Binary" => Proc.new { |bin| ::Base64.decode64(bin) }, + "hexBinary" => Proc.new { |bin| parse_hex_binary(bin) }, "binary" => Proc.new { |bin, entity| _parse_binary(bin, entity) }, "file" => Proc.new { |file, entity| _parse_file(file, entity) } } @@ -162,11 +163,12 @@ def _dasherize(key) "#{left}#{middle.tr('_ ', '--')}#{right}" end - # TODO: Add support for other encodings def _parse_binary(bin, entity) case entity["encoding"] when "base64" ::Base64.decode64(bin) + when "hex", "hexBinary" + parse_hex_binary(bin) else bin end @@ -180,6 +182,10 @@ def _parse_file(file, entity) f end + def parse_hex_binary(bin) + [bin].pack("H*") + end + def current_thread_backend IsolatedExecutionState[:xml_mini_backend] end diff --git a/activesupport/test/xml_mini_test.rb b/activesupport/test/xml_mini_test.rb index 022df2955f277..131028a28918c 100644 --- a/activesupport/test/xml_mini_test.rb +++ b/activesupport/test/xml_mini_test.rb @@ -337,6 +337,19 @@ def test_yaml assert_equal({ "1 => 'test'" => nil }, parser.call("{1 => 'test'}")) end + def test_hexBinary + parser = @parsing["hexBinary"] + + expected = "Hello, World!" + hex_binary = "48656C6C6F2C20576F726C6421" + + assert_equal expected, parser.call(hex_binary) + + parser = @parsing["binary"] + assert_equal expected, parser.call(hex_binary, "encoding" => "hexBinary") + assert_equal expected, parser.call(hex_binary, "encoding" => "hex") + end + def test_base64Binary_and_binary base64 = <