You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@thrift.apache.org by "Jens Geyer (Jira)" <ji...@apache.org> on 2026/10/23 09:25:00 UTC
[jira] [Created] (THRIFT-6450) Pure-Ruby reader returns binary list, set and map elements as strings
Jens Geyer created THRIFT-6450:
----------------------------------
Summary: Pure-Ruby reader returns binary list, set and map elements as strings
Key: THRIFT-6450
URL: https://issues.apache.org/jira/browse/THRIFT-6450
Project: Thrift
Issue Type: Bug
Components: Ruby - Library
Affects Versions: 0.25.0
Reporter: Jens Geyer
Without the {{thrift_native}} extension (pure Ruby, as on JRuby or when the extension is not built), binary elements of lists, sets and maps are read back as strings. For container elements, keys and values, {{Thrift::Struct_Union#read_field}} passes the field info through {{field_info()}} ([struct_union.rb:162|https://github.com/apache/thrift/blob/v0.25.0/lib/rb/lib/thrift/struct_union.rb#L162]), which copies {{:type}}, {{:class}}, {{:key}}, {{:value}} and {{:element}} but not {{:binary}}. {{read_type}} therefore reads those elements with {{read_string}} instead of {{read_binary}} (callers at lines 73, 74, 91 and 106). The writer passes the element field info on unchanged, so the elements are written as binary.
With the binary and compact protocols the bytes survive but come back as UTF-8 strings, and the struct no longer equals what was written. With the JSON protocol, which writes binary as Base64, the elements come back as the Base64 text. A binary field outside a container is read correctly, and with the native extension loaded all of it round-trips.
{code}
struct BL {
1: list<binary> l
2: binary b
3: set<binary> s
4: map<string, binary> m
}
{code}
{code}
require "thrift"
require "bl_types"
v = BL.new(l: ["\x01\xFF".b], s: Set.new(["\x02\xFE".b]), m: {"k" => "\x03\xFD".b})
[Thrift::BinaryProtocol, Thrift::JsonProtocol].each do |pc|
t = Thrift::MemoryBufferTransport.new
v.write(pc.new(t))
r = BL.new
r.read(pc.new(t))
p [pc, r.l, r.l[0].encoding, r.s.to_a, r.m, r == v]
end
{code}
{noformat}
$ thrift --gen rb bl.thrift
$ ruby -I lib/rb/lib -I gen-rb repro.rb
Unable to load thrift_native extension. Defaulting to pure Ruby libraries.
[Thrift::BinaryProtocol, ["\u0001\xFF"], #<Encoding:UTF-8>, ["\u0002\xFE"], {"k"=>"\u0003\xFD"}, false]
[Thrift::JsonProtocol, ["Af8="], #<Encoding:UTF-8>, ["Av4="], {"k"=>"A/0="}, false]
{noformat}
Reproduced with the 0.25.0 compiler and library on Ruby 3.2; {{field_info()}} is the same on master. Adding {{binary: field\[:binary\]}} to the hash that {{field_info()}} returns makes both lines print the bytes as {{ASCII-8BIT}} and {{true}}.
_Drafted with AI assistance (Claude Opus 5.5)._
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)