Skip to content

Commit 7ddb294

Browse files
Protocol Buffer TeamLogofile
authored andcommitted
Project import generated by Copybara.
PiperOrigin-RevId: 630060415 Change-Id: Icba137dbdd111c4557a6852b352728d2a65ca6a1
1 parent 08e0937 commit 7ddb294

File tree

2 files changed

+58
-20
lines changed

2 files changed

+58
-20
lines changed

content/programming-guides/proto2.md

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -228,19 +228,39 @@ number in the future.
228228
You should also reserve the field name to allow JSON and TextFormat encodings of
229229
your message to continue to parse.
230230

231-
### Reserved Fields {#fieldreserved}
231+
<a id="fieldreserved"></a>
232+
233+
### Reserved Field Numbers {#reserved-field-numbers}
232234

233235
If you [update](#updating) a message type by entirely deleting a field, or
234236
commenting it out, future developers can reuse the field number when making
235237
their own updates to the type. This can cause severe issues, as described in
236-
[Consequences of Reusing Field Numbers](#consequences).
238+
[Consequences of Reusing Field Numbers](#consequences). To make sure this
239+
doesn't happen, add your deleted field number to the `reserved` list.
240+
241+
The protoc compiler will generate error messages if any future developers try to
242+
use these reserved field numbers.
243+
244+
```proto
245+
message Foo {
246+
reserved 2, 15, 9 to 11;
247+
}
248+
```
249+
250+
Reserved field number ranges are inclusive (`9 to 11` is the same as `9, 10,
251+
11`).
237252

238-
To make sure this doesn't happen, add your deleted field number to the
239-
`reserved` list. To make sure JSON and TextFormat instances of your message can
240-
still be parsed, also add the deleted field name to a `reserved` list.
253+
#### Reserved Field Names {#reserved-field-names}
241254

242-
The protocol buffer compiler will complain if any future developers try to use
243-
these reserved field numbers or names.
255+
Reusing an old field name later is generally safe, except when using TextProto
256+
or JSON encodings where the field name is serialized. To avoid this risk, you
257+
can add the deleted field name to the `reserved` list.
258+
259+
Reserved names affect only the protoc compiler behavior and not runtime
260+
behavior, with one exception: TextProto implementations may discard unknown
261+
fields (without raising an error like with other unknown fields) with reserved
262+
names at parse time (only the C++ and Go implementations do so today). Runtime
263+
JSON parsing is not affected by reserved names.
244264

245265
```proto
246266
message Foo {
@@ -249,9 +269,8 @@ message Foo {
249269
}
250270
```
251271

252-
Reserved field number ranges are inclusive (`9 to 11` is the same as `9, 10,
253-
11`). Note that you can't mix field names and field numbers in the same
254-
`reserved` statement.
272+
Note that you can't mix field names and field numbers in the same `reserved`
273+
statement.
255274

256275
### What's Generated from Your `.proto`? {#generated}
257276

content/programming-guides/proto3.md

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,19 +205,39 @@ the future.
205205
You should also reserve the field name to allow JSON and TextFormat encodings of
206206
your message to continue to parse.
207207

208-
### Reserved Fields {#fieldreserved}
208+
<a id="fieldreserved"></a>
209+
210+
#### Reserved Field Numbers {#reserved-field-numbers}
209211

210212
If you [update](#updating) a message type by entirely deleting a field, or
211213
commenting it out, future developers can reuse the field number when making
212214
their own updates to the type. This can cause severe issues, as described in
213-
[Consequences of Reusing Field Numbers](#consequences).
215+
[Consequences of Reusing Field Numbers](#consequences). To make sure this
216+
doesn't happen, add your deleted field number to the `reserved` list.
217+
218+
The protoc compiler will generate error messages if any future developers try to
219+
use these reserved field numbers.
220+
221+
```proto
222+
message Foo {
223+
reserved 2, 15, 9 to 11;
224+
}
225+
```
226+
227+
Reserved field number ranges are inclusive (`9 to 11` is the same as `9, 10,
228+
11`).
214229

215-
To make sure this doesn't happen, add your deleted field number to the
216-
`reserved` list. To make sure JSON and TextFormat instances of your message can
217-
still be parsed, also add the deleted field name to a `reserved` list.
230+
#### Reserved Field Names {#reserved-field-names}
218231

219-
The protocol buffer compiler will complain if any future developers try to use
220-
these reserved field numbers or names.
232+
Reusing an old field name later is generally safe, except when using TextProto
233+
or JSON encodings where the field name is serialized. To avoid this risk, you
234+
can add the deleted field name to the `reserved` list.
235+
236+
Reserved names affect only the protoc compiler behavior and not runtime
237+
behavior, with one exception: TextProto implementations may discard unknown
238+
fields (without raising an error like with other unknown fields) with reserved
239+
names at parse time (only the C++ and Go implementations do so today). Runtime
240+
JSON parsing is not affected by reserved names.
221241

222242
```proto
223243
message Foo {
@@ -226,9 +246,8 @@ message Foo {
226246
}
227247
```
228248

229-
Reserved field number ranges are inclusive (`9 to 11` is the same as `9, 10,
230-
11`). Note that you can't mix field names and field numbers in the same
231-
`reserved` statement.
249+
Note that you can't mix field names and field numbers in the same `reserved`
250+
statement.
232251

233252
### What's Generated from Your `.proto`? {#generated}
234253

0 commit comments

Comments
 (0)