An Enum Rename Is Safe on the Wire (and the Diff Tab Does Not Know That)

The number on the wire is the value, and the name next to it in the .proto is a label. I took an enum with a member B at 2, renamed it to B2, and ran the old bytes through the new schema in the tool at proto.datajutsu.com. The bytes were the two bytes 08 02, tag and varint, before and after, and the decode tab printed the member as B2. Nothing in the pipeline checked the name. The old encoder wrote a number, the new decoder found a name for that number, and the rename is exactly as safe on the wire as it is invisible. The only thing that changed is what you call the value in your code and in your JSON.

The tool's diff tab does not report it that way, and this is the part I want you to see before you trust the word BREAKING in the column header. I ran the same rename through the diff and it came back with two lines. One, under breaking changes: enum member removed, new code reads old values as the default (0). The other, under safe additions: new enum member. Both lines are about the same number, 2. The wire never changed. The comparator for enum members is keyed on the name, so a name that disappeared reads as a removal and a name that appeared reads as an addition, and the value sitting under both of them is not part of the test. The removal line even claims new code will read the old value as the default, which is not what happens: new code reads it as B2, the name that is still there. It is a false alarm with a confident explanation attached.

The field side of the same tool does better, and the contrast is the lesson. Message fields are matched by number in three passes, and a field rename at the same number comes back as exactly one line: renamed name to newName, the bytes are unchanged, but the JSON key changed, clients reading JSON break, binary clients do not. One honest line for the field, a breaking-plus-safe pair for the enum, same wire-safe change. So when the diff shows a removed enum member and a new one with the same value, you have found a rename and you can close that alarm. When it shows a value changed from 2 to 3 on the same name, that one is real, and I verified the consequence: old bytes carrying 2 under the renumbered schema print as the number 2 in the JSON, not as a name, the signature the unknown enum post walks through in depth. The breaking lines the diff does get right are in the diff report post.

There is one real alias mechanism, and it is off by default. Two members at the same number, B at 2 and C at 2, is rejected by protoc 25.3 in both proto2 and proto3 with an error that names the clash and tells you the fix: set option allow_alias = true on the enum. With the option, both syntaxes compile. The engine inside the tool is stricter still: it rejects the duplicate at parse time with a duplicate id 2 in Enum E, and it does not know allow_alias, so a schema that compiles with protoc may not load in the tool at all. If your protocol genuinely uses aliases, the tool's tabs that need to parse the schema will tell you about it at the door, not silently. Duplicate names are the other half of the identity story: the name is the identity on the wire, and the number is only its label, so two labels on one number is the entire alias, and every tool that has to pick one will pick by its own rule.

The member at zero is the one where name and number stop being separable, because 0 is also the type's default and the presence sentinel. proto3 requires a member at 0, protoc 25.3 refuses to compile a proto3 enum without one, while the same list in proto2 compiles fine because proto2 has no such rule. Under the decode tab's options, a field whose value is the 0 member prints the member's name, the same string an absent field prints. I encoded the 0 member explicitly and got the two bytes 08 00, this engine writes defaults rather than skipping them, and decoded both that and an empty message and both came back with the same name. Explicit zero and never set are indistinguishable in the JSON for the same reason they are for every other scalar, the tag is the presence, and the whole presence story is the defaults post. The zero member is the one name that doubles as the type's answer to the question was it there.

So the rules that fall out: the number is the contract and the name is free to move, a rename costs nothing on the wire and costs a JSON key for text clients; the diff tab's enum lines are name-keyed, so a rename shows up as a removal plus an addition at the same value and the field side's single renamed line is the shape the enum side is missing; a value change at the same name is the breaking one and its signature is the number where a name should be; allow_alias is the only real way to put two names on a number and it is off by default and unknown to the engine this tool runs; and the 0 member is the sentinel that makes an explicit zero look exactly like a field that was never sent. You can watch all of it against your own schemas in the tool at proto.datajutsu.com: rename a member in the new version and run the diff, then decode the old bytes and see the number arrive wearing its new name. The varint and tag math underneath is in the wire format post, and the JSON side of the name-versus-number split is in the JSON mapping post.

Subscribe to Data Jutsu

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe