Skip to content
This repository was archived by the owner on Feb 23, 2026. It is now read-only.

Commit fef7983

Browse files
authored
test: unknown fields are preserved (#160)
* test: unknown fields are preserved Consider the following: ```proto message Old { string name = 1; } message New { string name = 1; string path = 2; } ``` We can think of `New` as being a minor version release update of `Old`. If a client using the older version receives a message over the wire from a server using the newer version, it is desirable that any new, unknown fields are preserved. These can store context the server needs, which is important in get/modify/set loops.
1 parent b12971a commit fef7983

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

proto/message.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ def __new__(mcls, name, bases, attrs):
155155

156156
# Same thing, but for enums.
157157
elif field.enum and not isinstance(field.enum, str):
158-
field_enum = field.enum
159158
field_enum = (
160159
field.enum._meta.pb
161160
if hasattr(field.enum, "_meta")

tests/test_message.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,28 @@ class Octopus_New(proto.Message):
285285
assert not hasattr(o_old, "length_cm")
286286

287287

288+
def test_unknown_field_deserialize_keep_fields():
289+
# This is a somewhat common setup: a client uses an older proto definition,
290+
# while the server sends the newer definition. The client still needs to be
291+
# able to interact with the protos it receives from the server.
292+
293+
class Octopus_Old(proto.Message):
294+
mass_kg = proto.Field(proto.INT32, number=1)
295+
296+
class Octopus_New(proto.Message):
297+
mass_kg = proto.Field(proto.INT32, number=1)
298+
length_cm = proto.Field(proto.INT32, number=2)
299+
300+
o_new = Octopus_New(mass_kg=20, length_cm=100)
301+
o_ser = Octopus_New.serialize(o_new)
302+
303+
o_old = Octopus_Old.deserialize(o_ser)
304+
assert not hasattr(o_old, "length_cm")
305+
306+
o_new = Octopus_New.deserialize(Octopus_Old.serialize(o_old))
307+
assert o_new.length_cm == 100
308+
309+
288310
def test_unknown_field_from_dict():
289311
class Squid(proto.Message):
290312
mass_kg = proto.Field(proto.INT32, number=1)

0 commit comments

Comments
 (0)