title: Thrift java client cannot handle union properly
body: <p>Dead simple thrift union example.
  Env: latest thrift, cpp as server, java as client
  <code>mytest.thrift</code>:</p>
  
  <pre><code>namespace java com.wilbeibi.thrift
  
  union Value {
    1: i16     i16_v,
    2: string  str_v,
  }
  
  struct Box {
    1: Value value;
  }
  
  service MyTest {
    Box echoUnion(1: i32 number);
  }
  </code></pre>
  
  <p><code>C++ server code:</code></p>
  
  <pre><code>#include "MyTest.h"
  #include &lt;thrift/protocol/TBinaryProtocol.h&gt;
  #include &lt;thrift/server/TSimpleServer.h&gt;
  #include &lt;thrift/transport/TServerSocket.h&gt;
  #include &lt;thrift/transport/TBufferTransports.h&gt;
  
  using namespace ::apache::thrift;
  using namespace ::apache::thrift::protocol;
  using namespace ::apache::thrift::transport;
  using namespace ::apache::thrift::server;
  
  using boost::shared_ptr;
  
  class MyTestHandler : virtual public MyTestIf {
   public:
    MyTestHandler() {
      // Your initialization goes here
    }
  
    void echoUnion(Box&amp; _return, const int32_t number) {
      // Your implementation goes here
    printf("Into echoUnion\n");
    if (number % 2 == 0) {
      Value v;
      v.__set_i16_v(100);
      v.__isset.i16_v = true;
      _return.__set_value(v);
      printf("Even number set int32\n");
    } else {
      Value v;
      v.__set_str_v("String value");
      v.__isset.str_v = true;
      _return.__set_value(v);
      printf("Odd number set string\n");
    }
      printf("echoUnion\n");
    }
  
  };
  
  int main(int argc, char **argv) {
    int port = 9090;
    shared_ptr&lt;MyTestHandler&gt; handler(new MyTestHandler());
    shared_ptr&lt;TProcessor&gt; processor(new MyTestProcessor(handler));
    shared_ptr&lt;TServerTransport&gt; serverTransport(new TServerSocket(port));
    shared_ptr&lt;TTransportFactory&gt; transportFactory(new TBufferedTransportFactory());
    shared_ptr&lt;TProtocolFactory&gt; protocolFactory(new TBinaryProtocolFactory());
  
    TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
    printf("Server is running on %d\n", port);
    server.serve();
    return 0;
  }
  </code></pre>
  
  <p><code>java client code</code>:</p>
  
  <pre><code>// some imports here
  public class Client {
      public void startClient() {
          TTransport transport;
              try {
                 transport = new TSocket("localhost", 9090);
                 TProtocol protocol = new TBinaryProtocol(transport);
                 MyTest.Client client = new MyTest.Client(protocol);
                 transport.open();
                 Box box = client.echoUnion(1);              
                 System.out.println(box.toString());
  
                 Box box2 = client.echoUnion(2);
                 System.out.println(box2.toString());
  
                 transport.close();
             } catch (TTransportException e) {
                 e.printStackTrace();
             } catch (TException e) {
                 e.printStackTrace();
             }
         }
       public static void main(String[] args) {
             Client client = new Client();
             client.startClient();
         }
  }     
  </code></pre>
  
  <p>Somehow, java client cannot print out string properly. (I also wrote a python client, but that seems work)</p>
  
  <p>Full code on gist here: <a href="https://gist.github.com/Wilbeibi/ee05f49c00d33d973558">thrift file, c++ and java code</a></p>