title: Polymorphism in Protocol Buffers 3
body: <h1>The current design</h1>
  
  <p>I am refactoring some exiting API code that returns a feed of events for a user. The API is a normal RESTful API, and the current implementation simply queries a DB and returns a feed.</p>
  
  <p>The code is long and cumbersome, so I've decided to move the feed generation to a microservice that will be called from the API server.</p>
  
  <h1>The new design</h1>
  
  <p>For the sake of decoupling, I thought that the data may move back and forth from the API server to the microservice as Protobuf objects. This way, I can change the programming language on either end and still enjoy the type safety and slim size of protobuf.</p>
  
  <p><a href="https://i.stack.imgur.com/u2k3A.png" rel="noreferrer"><img src="https://i.stack.imgur.com/u2k3A.png" alt="enter image description here"></a></p>
  
  <h1>The problem</h1>
  
  <p>The feed contains multiple types (e.g. likes, images and voice messages). In the future, new types can be added. They all share a few properties  timestamp and title, for instance - but other than that they might be completely different.</p>
  
  <p>In classic OOP, the solution is simple - a base <code>FeedItem</code> class from which all feed items inherit, and a <code>Feed</code> class which contains a sequence of <code>FeedItem</code> classes.</p>
  
  <p><strong>How do I express the notion of Polymorphism in Protocol Buffers 3, or at least enable different types of messages in a list?</strong></p>
  
  <h1>What <a href="https://developers.google.com/protocol-buffers/docs/proto3" rel="noreferrer">have I checked</a></h1>
  
  <ul>
  <li><code>Oneof</code>: "A oneof cannot be repeated".</li>
  <li><code>Any</code>: Too broad (like Java's <code>List&lt;Object&gt;</code>.</li>
  </ul>