Skip to content

Incorrect JSON serialization of repeated int64 #677

@somepage

Description

@somepage

Summary

When I create a contract with a repeated int64 variable, it serializes into repeated string, which is an unpredictable behaviour

Reproduction Steps

  1. Create a contract:
// ExampleContract describes example of contract.
message ExampleContract {
  // List of int64.
  repeated int64 items = 1;
  // Default int64 value.
  google.protobuf.Int64Value default = 2;
}
  1. Compile to python:
@dataclass(eq=False, repr=False)
class ExampleContract(betterproto.Message):
    """
    ExampleContract describes example of contract.
    """

    items: List[int] = betterproto.int64_field(1)
    """List of int64."""

    default: Optional[int] = betterproto.message_field(2, wraps=betterproto.TYPE_INT64)
    """Default int64 value."""
  1. Create instance of class and serialize it to JSON:
ec = ExampleContract(items=[5, 10], default=10)

print(ec.to_json())

Expected Results

{"items": [5, 10], "default": 10}

Actual Results

{"items": ["5", "10"], "default": 10}

System Information

libprotoc 25.3, Python 3.12.7

Name: betterproto
Version: 2.0.0b7
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: /opt/conda/lib/python3.12/site-packages
Requires: grpclib, python-dateutil, typing-extensions

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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions