danielgtaylor / danielgtaylor/python-betterproto

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

Aperta
#542 1 commento 2 reazioni 0 assegnatari Vedi su GitHub
bug investigation needed
Lingua principale
Python
Stelle
1.8k
Fork
234
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
api, backend
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.