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

Inconsistency with google protobuf when converting to dict/json with include_default_values=True

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

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

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 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, backend

Hướng nghiên cứu

Start with the Message.to_dict(include_default_values=True) entry point and compare its output with google.protobuf.json_format.MessageToDict using the linked reproduction repository. Add coverage for the shown oneof case; done means only the active variant appears in dict and JSON output when default values are included.

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

When converting a message to dict (or to json) with include_default_values set to True, the dict/json message will contain all variants for a oneof, not just the set one.

This is inconsistent with the google protobuf library, and it can IMHO be confusing especially when variants are complex messages (as all the nested fields of all the variants will be contained in the dict/json)

Reproduction Steps

I created a simple repo to reproduce this, with a slightly more complete example, here.

But basically, given a message with a oneof field eg.

syntax = "proto3";

message Message {
  oneof root_field {
    int32 int_value = 1;
    string string_value = 2;
  }
}

Convert it to dict setting the including_default_values flag to True

from betterproto_compiled import Message
from pprint import pprint

message = Message(
    int_value=10
)

pprint(message.to_dict(include_default_values=True))
Expected Results

I expected to obtain a dict with only the active variant of the oneof being included (which is the same behavior as the MessageToDict from the standard python protobuf library).

This is the output I get from google.protobuf.json_format.MessageToDict, setting including_default_value_fields=True:

{'intValue': 10}
Actual Results

A dict with all the variants of the oneof, including the non-set ones:

{'intValue': 10, 'stringValue': ''}
System Information
libprotoc 3.21.6
Python 3.10.13
Name: betterproto
Version: 2.0.0b6
Summary: A better Protobuf / gRPC generator & library
Home-page: https://github.com/danielgtaylor/python-betterproto
Author: Daniel G. Taylor
Author-email: [email protected]
License: MIT
Location: /home/marco/.local/share/rtx/installs/python/3.10.13/lib/python3.10/site-packages
Requires: grpclib, python-dateutil
Required-by: 
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.