danielgtaylor / danielgtaylor/python-betterproto

Zeroes are not serialized with bytes() in proto2 syntax with required field

Abierto
#676 3 comentarios 0 reacciones 0 asignados Ver en GitHub
bug investigation needed
Lenguaje dominante
Python
Estrellas
1.8k
Forks
234
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

### Summary

There should be an alternative __bytes__ method that allows to serialize zeroes for proto2 syntax. Currently zeroes enums are skipped as they are "default" values which is incorrect because unless the field is optional, it must be present in encoded form.

### Reproduction Steps

1. Create a simple proto2 file:
```proto
message MyMessage {
enum MyEnum {
FOOBAR = 0;
BAZ = 1;
}
}
```
2. Compile to python:
```python3
@dataclass
class MyMessage(betterproto.Message):
# @required
action: "MyEnum" = betterproto.enum_field(1)

class MyEnum(betterproto.Enum):
FOOBAR = 0
BAZ = 1
```
3. Use the `FOOBAR` value (0):
```python3
print(
MyMessage(
action=MyEnum.FOOBAR,
)
)
```
You can see the MyEnum.FOOBAR here. However when you try to serialize it, the encoder skips this value incorrectly thinking it's default and hence shouldn't be added:
```python3
print(
MyMessage(
action=MyEnum.FOOBAR,
).to_json()
)
# or
print(
MyMessage(
action=MyEnum.FOOBAR,
).to_dict()
)
# or
print(
bytes(MyMessage(
action=MyEnum.FOOBAR,
))
)
```

This can only be "fixed" by passing include_default_values=True in to_json or to_dict:

```python3
print(
MyMessage(
action=MyEnum.FOOBAR,
).to_json(include_default_values=True)
)
```

But I need binary representation of the MyMessage class

### Expected Results

N/A

### Actual Results

N/A

### System Information

Latest beta version

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

Guía de contribución

Abrir la guía de contribución

Línea de trabajo

Empieza reproduciendo el problema con los ejemplos generados de MyMessage y MyEnum.FOOBAR, y después compara bytes(), to_json() y to_dict() para el enum proto2 requerido. Se considera terminado cuando la representación binaria incluye el enum con valor cero sin requerir include_default_values=True.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

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

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.