Missing field converting proto message from byte[] to "com.google.protobuf.Message"

973 views
Skip to first unread message

Joan Balagueró

unread,
Mar 27, 2023, 1:25:50 PM3/27/23
to Protocol Buffers
Hello everyone,

I'm absolutely new to probuf. So this is my issue.

We have implemented a proxy that receives a protobuf message as a byte array in a post message, and we only have to bypass this proto to the backend, receive the response (also in protobuf) and return it to the origin. That works.

The point is that I need to convert this received proto message to a com.google.protobuf.Message because I need to get a couple of values.

The point is when I convert the byte array proto to a Message object, the field I need ('Meta') is missing.

Attached the .proto file. I can't get the 'Meta' field, instead I get a:
"fields {
  key: ""
  value {
  }
}"

message Request {
   Meta Meta = 1;
   repeated int32 Hotels = 2 [packed = false];
   Country Market = 3;
   repeated Room Rooms = 4;
   .bcl.DateTime CheckIn = 5;
   .bcl.DateTime CheckOut = 6;
   OptionalCriteria OptionalCriteria = 7;
}
message Meta {
   int32 Client = 1;
   int32 Brand = 2;
   bool UseCache = 3;
   .bcl.TimeSpan CutOffTime = 4;
   bool B2C = 5;
   Language Language = 6;
   Currency Currency = 7;
   bool IncludeProviderAudit = 8;
   SalesChannel SalesChannel = 9;
}

When I try to get the message in this way:
public static Message fromByteArrayToMessage(byte[] body) throws IOException {
return Struct.newBuilder().mergeFrom(body).build();
}

I get the following, where everything is right except the 'Meta':
fields {
  key: ""
  value {
  }
}
2: 9836
2: 320488
2: 487215
3: 724
4: {
  1: {
    1: 30
  }
  1: {
    1: 30
  }
}
5: {
  1: 38994
}
6: {
  1: 38998
}
7: {
  4: 1
}

Any help will be really appreciated.

Joan.

date_time.proto
file.proto

Nadav Samet

unread,
Mar 28, 2023, 11:37:09 AM3/28/23
to Joan Balagueró, Protocol Buffers
Hi Joan,

The code example doesn't work since you are trying to deserialize a message of type "Request" into a message of type "Struct" - the messages are unrelated. I assume the misconception is that Struct would give you some generic way to inspect the Request message, but that's not the case.

Here are two alternatives you can try:

1. Use com.google.protobuf.UnknownFieldSet.parseFrom to parse the message. Use getField(1) to get the meta field, and then repeat using UnknownFieldSet.parseFrom on it to deserialize it.

2. Sounds like the proxy doesn't have access to the entire protos, but it still needs to make assumptions about some of the fields in them. You can define protos just for the part you need to extract:
```
message BaseRequest {
  BaseMeta meta = 1
}

message BaseMeta {
  int32 brand = 2
}
```

and use `BaseRequest.parseFrom` in your code, everything unknown will be ignored.

Hope this helps!



--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/2a8fe909-51fc-4550-a14c-a358148d68c9n%40googlegroups.com.


--
-Nadav
Reply all
Reply to author
Forward
0 new messages