Hi,

I am using protobuf for a project, and I've run into an interesting case, 
which I've had no luck finding a standard solution to. For the record, I'm 
using the Java APIs to interact with my messages

My project has several messages defined, a few of which have similar fields 
(for example, two of them have a required text field with index 1). The 
problem I'm running into is that if I erroneously pass the byte array form 
of one message to a builder of the other message, it will happily parse it 
without any exceptions or other issues. (Apparently 
InvalidProto....Exception is thrown only is the array is completely invalid 
as a protobuf message of any type, and does not indicate anything else)

I have tried checking "isInitialized", but because they both have the same 
required fields at the same indexes, it doesn't have an error. 

I considered changing the indexes or something like that, but that depends 
on every person who works on this project knowing about and respecting that 
convention - which doesn't seem like a sustainable solution. I also thought 
of checking the unknown fields, but one of the reasons I chose protobuf was 
for its forward and backwards compatibility - and I believe that the 
unknown filed list will be (validly) non-empty if I add a field down the 
road and an old version of the project receives a new version of the 
message.

The only other thing I can think to do is to add a field in my messages 
which indicates the message - is this my best option, or am I missing 
something in the standard libraries which handles this?

Thanks!

Example:

message Message1 {

      required string field1 = 1;
      
      optional string field2 = 2;
}

message Message2 {

      required string field1 = 1;
      
      optional int32 field2 = 2;

}


public void parseMessage1(byte[] protobuf){
    try {
        Message1 message = Messag1e.parseFrom(protobuf);

        if(message.isInitialized()){
            //Do some stuff
        }else{
            //Never get here
        }
    } catch (InvalidProtocolBufferException e) {
        //Not thrown
    }
}

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to