Skip to content
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

Fix symbolize_keys: true to properly create UTF-8 symbols #246

Merged
merged 1 commit into from
Feb 8, 2022
Merged
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
4 changes: 2 additions & 2 deletions ext/msgpack/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -494,9 +494,9 @@ static inline VALUE msgpack_buffer_read_top_as_string(msgpack_buffer_t* b, size_
#endif // HAVE_RB_ENC_INTERNED_STR
}

static inline VALUE msgpack_buffer_read_top_as_symbol(msgpack_buffer_t* b, size_t length)
static inline VALUE msgpack_buffer_read_top_as_symbol(msgpack_buffer_t* b, size_t length, bool utf8)
{
return rb_str_intern(msgpack_buffer_read_top_as_string(b, length, true, false));
return rb_str_intern(msgpack_buffer_read_top_as_string(b, length, true, utf8));
}

#endif
2 changes: 1 addition & 1 deletion ext/msgpack/unpacker.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ static inline int read_raw_body_begin(msgpack_unpacker_t* uk, int raw_type)
if(length <= msgpack_buffer_top_readable_size(UNPACKER_BUFFER_(uk))) {
int ret;
if ((uk->optimized_symbol_ext_type && uk->symbol_ext_type == raw_type) || (uk->symbolize_keys && is_reading_map_key(uk))) {
VALUE symbol = msgpack_buffer_read_top_as_symbol(UNPACKER_BUFFER_(uk), length);
VALUE symbol = msgpack_buffer_read_top_as_symbol(UNPACKER_BUFFER_(uk), length, raw_type == RAW_TYPE_STRING);
ret = object_complete_symbol(uk, symbol);
} else {
/* don't use zerocopy for hash keys but get a frozen string directly
Expand Down
25 changes: 22 additions & 3 deletions spec/unpacker_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# encoding: ascii-8bit

require 'stringio'
require 'tempfile'
require 'zlib'
Expand Down Expand Up @@ -302,6 +300,21 @@ def flush
MessagePack.unpack(MessagePack.pack(symbolized_hash), :symbolize_keys => true).should == symbolized_hash
end

it 'MessagePack.unpack symbolize_keys preserve encoding' do
hash = { :ascii => 1, :utf8_é => 2}
loaded_hash = MessagePack.load(MessagePack.pack(hash), :symbolize_keys => true)

hash.keys[0].encoding.should == Encoding::US_ASCII # Ruby coerce symbols to US-ASCII when possible.
loaded_hash.keys[0].should == hash.keys[0]
loaded_hash.keys[0].encoding.should == hash.keys[0].encoding

hash.keys[1].encoding.should == Encoding::UTF_8
loaded_hash.keys[1].should == hash.keys[1]
loaded_hash.keys[1].encoding.should == hash.keys[1].encoding

MessagePack.unpack(MessagePack.pack(hash), :symbolize_keys => true).should == hash
end

it 'Unpacker#unpack symbolize_keys' do
unpacker = MessagePack::Unpacker.new(:symbolize_keys => true)
symbolized_hash = {:a => 'b', :c => 'd'}
Expand Down Expand Up @@ -765,7 +778,13 @@ def flatten(struct, results = [])

context 'binary encoding', :encodings do
let :buffer do
MessagePack.pack({'hello' => 'world', 'nested' => ['object', {'structure' => true}]})
MessagePack.pack({
'hello'.b => 'world'.b,
'nested'.b => [
'object'.b,
{'structure'.b => true},
]
})
end

let :unpacker do
Expand Down