Skip to content

Commit

Permalink
Support hexBinary format in XML
Browse files Browse the repository at this point in the history
  • Loading branch information
heka1024 committed Apr 22, 2024
1 parent 82054e8 commit 1e70807
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
4 changes: 4 additions & 0 deletions activesupport/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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*
Expand Down
8 changes: 7 additions & 1 deletion activesupport/lib/active_support/xml_mini.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions activesupport/test/xml_mini_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <<BASE64
TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
Expand Down

0 comments on commit 1e70807

Please sign in to comment.