Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Zeroes are not serialized with bytes() in proto2 syntax with required field

Đang mở
#676 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
38/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
api

Hướng nghiên cứu

Bắt đầu bằng cách tái hiện vấn đề với các ví dụ MyMessage và MyEnum.FOOBAR được tạo, sau đó so sánh bytes(), to_json() và to_dict() cho enum proto2 cần thiết. Hoàn thành khi biểu diễn nhị phân bao gồm enum có giá trị bằng không mà không yêu cầu include_default_values=True.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

bug investigation needed
Summary

There should be an alternative bytes method that allows to serialize zeroes for proto2 syntax. Currently zeroes enums are skipped as they are "default" values which is incorrect because unless the field is optional, it must be present in encoded form.

Reproduction Steps
  1. Create a simple proto2 file:
message MyMessage {
    enum MyEnum {
        FOOBAR = 0;
        BAZ = 1;
    }
}
  1. Compile to python:
@dataclass
class MyMessage(betterproto.Message):
	# @required
	action: "MyEnum" = betterproto.enum_field(1)

class MyEnum(betterproto.Enum):
	FOOBAR = 0
	BAZ = 1
  1. Use the FOOBAR value (0):
print(
	MyMessage(
		action=MyEnum.FOOBAR,
	)
)

You can see the MyEnum.FOOBAR here. However when you try to serialize it, the encoder skips this value incorrectly thinking it's default and hence shouldn't be added:

print(
	MyMessage(
		action=MyEnum.FOOBAR,
	).to_json()
)
# or
print(
	MyMessage(
		action=MyEnum.FOOBAR,
	).to_dict()
)
# or
print(
	bytes(MyMessage(
		action=MyEnum.FOOBAR,
	))
)

This can only be "fixed" by passing include_default_values=True in to_json or to_dict:

print(
	MyMessage(
		action=MyEnum.FOOBAR,
	).to_json(include_default_values=True)
)

But I need binary representation of the MyMessage class

Expected Results

N/A

Actual Results

N/A

System Information

Latest beta version

Checklist
  • I have searched the issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have verified this issue occurs on the latest prelease of betterproto which can be installed using pip install -U --pre betterproto, if possible.
Ngôn ngữ chính
Python
Star
1.8k
Fork
235
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của danielgtaylor/python-betterproto

Tất cả issue của danielgtaylor/python-betterproto

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.