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/22 22:37:00 UTC
[jira] [Created] (THRIFT-6447) Ruby union constants and field defaults keep a String selector, so their accessors raise
Jens Geyer created THRIFT-6447:
----------------------------------
Summary: Ruby union constants and field defaults keep a String selector, so their accessors raise
Key: THRIFT-6447
URL: https://issues.apache.org/jira/browse/THRIFT-6447
Project: Thrift
Issue Type: Bug
Components: Ruby - Library
Affects Versions: 0.25.0
Reporter: Jens Geyer
A union-typed constant or field default generated by {{\-\-gen rb}} keeps the field name as a String, and the union's generated accessors do not recognize it. The generator writes the value as a Hash with a String key ([t_rb_generator.cc:498|https://github.com/apache/thrift/blob/v0.25.0/compiler/cpp/src/thrift/generate/t_rb_generator.cc#L498]), {{Union#initialize}} keeps the key as given ([union.rb:32|https://github.com/apache/thrift/blob/v0.25.0/lib/rb/lib/thrift/union.rb#L32] and [union.rb:47|https://github.com/apache/thrift/blob/v0.25.0/lib/rb/lib/thrift/union.rb#L47]), and the accessor compares it with a Symbol ([union.rb:116|https://github.com/apache/thrift/blob/v0.25.0/lib/rb/lib/thrift/union.rb#L116]).
{code}
union U {
1: string s
2: i32 i
}
struct H {
1: U u = {"s": "x"}
}
const U C = {"s": "x"}
{code}
{noformat}
$ thrift --gen rb udef.thrift
$ grep -A1 'U.new({' gen-rb/udef_types.rb
default: ::U.new({
%q"s" => %q"x",
$ ruby -I lib/rb/lib -I gen-rb -e 'require "thrift"; require "udef_constants"
u = H.new.u; p u.get_set_field, u.s? ; p C.get_set_field; u.s'
"s"
false
"s"
lib/rb/lib/thrift/union.rb:119:in `block in field_accessor': s is not union's set field. (RuntimeError)
{noformat}
The same value is also not {{==}} to {{U.new(s: "x")}}, and after a write and a read it holds {{:s}} and no longer equals the default. Reproduced with the 0.25.0 compiler and library on Ruby 3.2 and 4.0; the code is the same on master. Converting the name with {{to_sym}} in {{Union#initialize}} ({{@setfield = name&.to_sym}}) makes the accessor, the comparison and the round trip in the example work.
_Drafted with AI assistance (Claude Opus 5.5)._
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)