Makogan
Makogan

Reputation: 9570

What happens to old proto files once you update the definition of the proto?

Assume one day I save a proto message in a file as my_file.pb The next day I add a new field to the message.

Then I read my_file.pb from disk and try to access the new field. Will this crash? is this UB? is it handled by the standard?

I tried reading the documentation and all I could get was:

You can add new fields to your message formats without breaking backwards-compatibility; old binaries simply ignore the new field when parsing. So if you have a communications protocol that uses protocol buffers as its data format, you can extend your protocol without having to worry about breaking existing code.

That doesn't tell me what behaviour to expect if my code tries to access a field not originally defined in the stored proto.

Upvotes: 1

Views: 1031

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063088

If you're using proto3: no problem, all fields are optional. It'll just be empty / zero.

If you're using proto2: it depends; "optional" or "repeated" - fine, no problem - it'll just be empty/default. "required": boom, you're toast. So... just don't do that?

Upvotes: 2

Related Questions