danielgtaylor / danielgtaylor/python-betterproto
Nested message with repeated field not in to_json when mutated after initialization
- Vorherrschende Sprache
- Python
- Sterne
- 1.8k
- Forks
- 234
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beschreibung
### Summary
Possibly related to #199
Given a message with a nested message field containing a repeated field, if the field is mutated (eg. via `list.append`) after initialization, `to_dict` will not include it.
### Reproduction Steps
Given a proto schema like this
```protobuf
syntax = "proto3";
message RootMessage {
NestedMessage nested = 1;
}
message NestedMessage {
repeated string content = 1;
}
```
Running the following script
```python
message = RootMessage()
message.nested.content.append("test-content")
print("Including default values: True")
pprint(message.to_dict(include_default_values=True))
print("Including default values: False")
pprint(message.to_dict(include_default_values=False))
```
will result in:
```
Including default values: True
{'nested': {'content': ['test-content']}}
Including default values: False
{}
```
### Expected Results
I would expect the `content` field of the `nested` message to appear in the returned `dict` also when `include_default_values` is set to False, as its content was mutated and it's no longer the default value.
### Actual Results
`to_dict` returns an empty dict
### 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
Rechercherichtung
Beginne damit, die Reproduktion der verschachtelten Nachricht aus dem Issue mit betterproto 2.0.0b6 auszuführen und den to_dict-Pfad für include_default_values=False zu prüfen. Als erledigt gilt die Aufgabe, wenn ein mutiertes verschachteltes repeated-Feld im zurückgegebenen Dict erscheint, während unveränderte Standardfelder weiterhin nicht enthalten sind; füge Regressionstests für beide Fälle hinzu, falls der Testort im Repository identifiziert wird.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- backend-api-design
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 38/100