Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript / JavaScript gRPC google.protobuf.Struct cannot be read

I have a TypeScript server trying to read a JSON object using a Struct but it seems to be partially working only for objects containing a "fields" key which then expects an object as value. Nonetheless, a Struct should work with any JSON object.

Using BloomRPC I am trying the following message:

{
  "payload": {
    "fields": {
      "Hello": {
        "whatever": 0
      }
    }
  }
}

The server reads:

{ fields: { Hello: {} } }

If I send:

{
  "payload": {
    "anotherfield": {
      "HelloWorld": {
        "whatever": 0
      }
    }
  }
} 

I get an empty object on the server.

The simplified protobuf file looks like this:

syntax = "proto3";

import "google/protobuf/struct.proto";

// The service definition.
service TestTicketService {
  rpc UpdateTicket (UpdateTicketRequest) returns (UpdateTicketResponse);
}

// The request message containing the required ticket information.
message UpdateTicketRequest {
    string ticketId = 1;
    google.protobuf.Struct payload = 2;
}

// The response message containing any potential error message
message UpdateTicketResponse {
  string error = 1;
}

Any idea why google/protobuf/struct.proto doesn't work as expected?

like image 879
Va5ili5 Avatar asked Jun 01 '26 20:06

Va5ili5


1 Answers

What really confused me is that I was trying to pass normal JSON objects and expecting to read them. The whole point is that from the client side, the JSON object needs to be encoded in a very specific way.

For example:

"payload": {
    "fields": {
      "name": {
        "stringValue": "joe"
      },
      "age": {
        "numberValue": 28
      }
    }
  }

You can figure out the format of the message by looking at the Struct proto file here: https://googleapis.dev/nodejs/asset/latest/v1_doc_google_protobuf_doc_struct.js.html

like image 79
Va5ili5 Avatar answered Jun 03 '26 11:06

Va5ili5



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!