Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

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

Abierto
#542 1 comentario 2 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
38/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
python
Área
api, backend

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

bug investigation needed
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

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de danielgtaylor/python-betterproto

Todos los issues de danielgtaylor/python-betterproto

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.