Inconsistency with google protobuf when converting to dict/json with include_default_values=True
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Aptitud para principiantes
- 38/100
Línea de trabajo
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.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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.
- Lenguaje dominante
- Python
- Estrellas
- 1.8k
- Forks
- 235
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de danielgtaylor/python-betterproto
-
Dificultad 3/5 1-2 días Aptitud para principiantes 55/100
-
Dificultad 5/5 Más de una semana Aptitud para principiantes 15/100
-
bug investigation needed
Dificultad 3/5 1-2 días Aptitud para principiantes 42/100
-
bug investigation needed
Dificultad 3/5 1-2 días Aptitud para principiantes 45/100
-
bug investigation needed
Dificultad 4/5 3-5 días Aptitud para principiantes 38/100
danielgtaylor/python-betterproto#676 · 3 comentarios ·
Todos los issues de danielgtaylor/python-betterproto
Issues similares
-
good first issue priority: low size: XS testing
Dificultad 1/5 1-3 horas Aptitud para principiantes 86/100
cubrid-lab/cubrid-mcp-server#385 ·
Los mantenedores suelen responder en 1 día
-
good first issue priority: low size: S testing
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100
cubrid-lab/cubrid-cookbook-python#447 ·
Los mantenedores suelen responder en 1 día
-
Python PointcloudAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
ArduCAM/Arducam_tof_camera#125 ·
-
area:curator enhancement
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
CassioDalla/Scrinalia#150 ·
Los mantenedores suelen responder en 1 día
-
Tutorial notebook: Find the leakAbiertodocumentation good first issue
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100