Skip to content

Generated FieldMask class does not serialize correctly #181

@nicjohnson145

Description

@nicjohnson145

Summary

FieldMask json/dict serializes as list rather than comma separated string

Reproduction Steps

Attached tarball with simple project reproducing the issue,

field_mask_repro.tar.gz

but in short:

  • Define a message with a google FieldMask field
syntax = 'proto3';

package foo.v1;

import "google/protobuf/field_mask.proto";

message Bar {
  google.protobuf.FieldMask mask = 1;
}
  • construct an instance of said message and call to_dict/to_json on it

Expected Results

the FieldMask portion serializes as a comma separate string per the doc string (and other langauge's implementations)


    # JSON Encoding of Field Masks

    In JSON, a field mask is encoded as a single string where paths are
    separated by a comma. Fields name in each path are converted
    to/from lower-camel naming conventions.

    As an example, consider the following message declarations:

        message Profile {
          User user = 1;
          Photo photo = 2;
        }
        message User {
          string display_name = 1;
          string address = 2;
        }

    In proto a field mask for `Profile` may look as such:

        mask {
          paths: "user.display_name"
          paths: "photo"
        }

    In JSON, the same mask is represented as below:

        {
          mask: "user.displayName,photo"
        }

Actual Results

it serializes as a list of strings

❯ uv run bar
      Built bar @ file:///home/policyfly/tmp/field_mask_repro/bar
      Built foo @ file:///home/policyfly/tmp/field_mask_repro/gen/python
Uninstalled 2 packages in 0.47ms
Installed 2 packages in 1ms
{'mask': {'paths': ['a.b', 'a.c']}}
{"mask": {"paths": ["a.b", "a.c"]}}

System Information

❯ uv run python --version
Python 3.12.7

not running with protoc, but running with buf & uv

❯ buf --version
1.50.0

❯ uv self version
uv 0.9.28

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

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions