> I am confused on how to detect this scenario, and ultimately prevent
> such things occurring.

You can't, at least in the simple way.  Protocol buffers on the wire
have no type information associated with them, and they're explicitly
designed so that they can accept fields they don't expect to be
present (for forwards compatibility), so serializing a message of one
type and deserializing it as another type will work sometimes.

A couple potential solutions come to mind.  One is to send all your
messages wrapped in another message that includes a type identifier.
Another would be to have all your messages include a key field that
must be filled in in a specific way (eg, tag 1 in all messages is a
int field named tag, and there's a single value for each message type
that you require to be filled in).  What is most appropriate depends
on how you expect a message of one type to be sent to something
expecting a different type.  (Obviously, the best solution is to set
up your well-typed language APIs to keep that from happening.)

- Adam

You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to proto...@googlegroups.com.
To unsubscribe from this group, send email to 
For more options, visit this group at 

Reply via email to