Extending Protobuf Messages

30,721

Solution 1

This is not the exact answer to your question but we can do something like this to share common parameters.

message Child1 { 
    required int c1 = 2;
    required string c2 = 3;
}

message Child2 { 
    required int c1 = 2;
    required string c2 = 3;
}

message Request {
    required string common1 = 0;
    optional string common2 = 1;
    oneof msg { Child1 c1 = 2; Child2 c2 = 3; }

}

Other option is to use extend keyword

message Parent {
    required string common1 = 0;
    optional string common2 = 1;
}

message Child1 { 
    extend Parent
    {       
        optional Child1 c1 = 100;
    }

    required int c1 = 2;
    required string c2 = 3;
}

Solution 2

Although inheritance is not directly supported in protobuf, in some languages (e.g. C#) you can still create common contracts for a service/request/reply etc. using partial classes.

You can create a partial class of the generated one, and have it inherit/implement your common scenario.

In this way you can ensure a common contract in your internal models and services.

Note however, that this is for internal use only, as the endpoints and API surface should be completely oblivious to your specifications.

Share:
30,721

Related videos on Youtube

user1413793
Author by

user1413793

Updated on July 20, 2021

Comments

  • user1413793
    user1413793 almost 3 years

    I have many different schemas, however there are a set of fields which every schema contains. I was wondering if there was a way to have a different schema extend a parent schema and inherit its fields. For example this is what I want:

    message Parent {
        required string common1 = 0;
        optional string common2 = 1;
    }
    
    message Child1 { // can we extend the Parent?
        // I want common1, common2 to be fields here
        required int c1 = 2;
        required string c2 = 3;
    }
    
    message Child2 { // can we extend Parent?
        // I want common1, common2 to be fields here
        repeated int c3 = 2;
        repeated string c4 = 3;
    }
    

    Such that Child1 and Child2 also contain the fields common1 and common2 (and potentially more) from Parent.

    Is this possible and if so how?

    • Reinier Torenbeek
      Reinier Torenbeek over 9 years
      Inheritance is not supported, but as a poor man's solution, you could use a nested construction where the first field of Child1 and Child2 is of type Parent. In order to access the fields in the "base-class", you will have to explicitly access that Parent-typed field first.
    • engineerC
      engineerC over 9 years
  • Alex Punnen
    Alex Punnen over 7 years
    extend is not supported in proto buffer 3
  • gtato
    gtato over 4 years
    So a parent has to know all the possible children, which can be in different files... ugly
  • PragmaticProgrammer
    PragmaticProgrammer about 4 years
    @gtato check out Avro unions :) All schemas need to be in one file, ive seen them grow to dozens lol
  • Kaushal28
    Kaushal28 over 2 years
    oneof creates nesting in JSON.