danielgtaylor / danielgtaylor/python-betterproto

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

Offen
#542 1 Kommentar 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
bug investigation needed
Vorherrschende Sprache
Python
Sterne
1.8k
Forks
234
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

### 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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

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.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
api, backend
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
38/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.