A Reserved Protobuf Enum Value Is a Line in the Schema (and the Wire Still Carries the Number)

The reserved line inside an enum works the same way the one inside a message does, and it protects the same thing: the schema. I added reserved 2, 3 plus a reserved name PURPLE to an enum with UNKNOWN, RED and BLUE, and the bytes a decoder produces for a value of 2 on the wire did not move. The message with that field set to 2 still decodes to a 2, and the same for 3. The line is a promise between the people who edit the .proto, not a rule the stream obeys. If an old sender still carries a 2, the new code reads 2 as an unknown value, exactly the situation the line was meant to make survivable.

The interesting part is how much of that promise the JS parser actually enforces, and it is less than you would expect. A member whose number sits inside a reserved range is rejected, the error says id 3 is reserved in Enum C. But a single reserved number never blocks its own number: reserved 2 followed by a member at 2 parses clean, in both orders, and a member at the top of a range, 4 in reserved 2 to 4, parses clean too because the top is exclusive, the same open-top behavior the message reserved ranges have. The name reservation is the stricter one, and it is order dependent: write the member first and the reserved line after it, and the file parses; write the reserved line first and the member throws name PURPLE is reserved in Enum Color. The real compiler does not care about your ordering at all.

protoc is the one that closes the loop, and its errors name the member. A member at a reserved number dies with Enum value M2 uses reserved number 2, a member at a reserved name dies with Enum value PURPLE is reserved, and it catches the member at the range top that the JS parser waves through. It also enforces two things the parser skips: an enum with no zero member at all is fine in the JS tool, and a file with nothing but reserved 0 parses clean there, while the compiler wants at least one value. The directional fact holds again: every file the compiler accepts parses in the JS tool, and the tool accepts a few files the compiler would refuse, so a clean parse in a browser tool is not proof your file compiles. That is the same shape as the message reserved ranges, and the same reason the two belong to the same habit.

The cost of the line, measured, is twenty descriptor bytes and zero wire bytes. The enum descriptor with the reserved lines is twenty bytes bigger than the same enum without them, and the breakdown is exact: two reserved range entries, each a six-byte submessage carrying a start and an end, plus the reserved name as an eight-byte string. None of that touches the message bytes. A 2 on the wire is a 2 on the wire whether or not the schema ever reserved it, which is why the diff between a schema with the line and one without looks like nothing when you run the bytes through it, and everything when you diff the text. The line buys you the merge review, not the runtime.

The encode door has its own version of the unknown-value split. Setting an enum field to the reserved number 2 encodes it: the library accepts the number and writes the varint. Setting it to the reserved name PURPLE gets the field dropped silently, the message encodes without it, no error, the round trip completes. Same input class, two outcomes, and the silent one is the one you catch by comparing what you sent with what came back, which is the whole reason a verify round trip exists. The compiler, given the name, would have named your mistake instead of hiding it.

One place the tool still fell short, until the very end of this post: the Diff tab on proto.datajutsu.com, the one that compares two .proto versions and tells you what breaks, never read the reserved lines at all. A schema change that only added or removed a reserved line came back as no differences, which is the wrong answer for the exact scenario this post is about, a dead field getting marked off before it is reused. That is fixed now. The diff reports every reserved number and reserved name that appears or disappears, on messages and on enums, under the safe additions side with the reason: it is a wire-safe change, it only stops the number or the name from being reused, and it never changes what old data decodes to. Remove the line and the diff says the same thing in reverse, the number is open for reuse again, which is usually a signal it is about to come back. So a bump that only touches reserved lines now reads as a real, explained, safe change instead of silence.

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