Search code examples
goprotocol-buffersdatacontractprotobuf-go

Protobuf dynamic message schema verification


I'm trying to implement data contract logic. I've 2 services switching messages with each other. Service A send messages in some format + the file descriptor proto of the .proto file used to generate it. Service B gets them both, the message and the file descriptor proto and has to ensure the message doesn't break the schema defenition. What I've did until now is to create a dynamic message instance using the descriptor proto and tried to unmarshal the message into the dynamic message instance, and in case no error occurred during the unmarshal process it counts as a success (message doesn't break the schema).

  1. Is it ok to rely on the unmarshal function in order to decide whether the message is ok?
  2. I noticed that even in case I'm sending messages with totally different schemas the unmarshal succeed. the only way I found to cause the unmarshal to fail is by sending proto2 messages with missing required fields. So is it by design that every message can be unmarshled using a totally different schema definition?
  • I'm using the official Go protobuf library

Solution

    1. Yes, it is ok to use unmarshal function to check if your proto is valid or not.
    2. No, it's not design to unmarshal arbitrary message but it is designed to unmarshal only that fields which are present in your protobuf message. For example, you have proto message with structure as follow:
    message MyMessage {
        uint64 id = 1;
        string name = 2;
        string surname = 3;
    }
    

    If your server receives message that contains just id and name and your server is trying to unmarshal this message, id and name fields would be unmarshaled while surname field in your structure would be empty. This approach appliable for JSONs too.