danielgtaylor / danielgtaylor/python-betterproto

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

Open
#542 1 comment 2 reactions 0 assignees View on GitHub
bug investigation needed
Dominant language
Python
Stars
1.8k
Forks
234
PR merge metrics
No merged PRs in 30d

Description

### 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](https://github.com/dallagi/proto_dict_serialization_comparison).

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

```protobuf
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`

```python
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`:

```python
{'intValue': 10}
```

### Actual Results

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

```python
{'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: danielgtaylor@gmail.com
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

- [X] I have searched the issues for duplicates.
- [X] I have shown the entire traceback, if possible.
- [X] I have verified this issue occurs on the latest prelease of betterproto which can be installed using `pip install -U --pre betterproto`, if possible.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.