googleapis/proto-plus-python

The message constructor is incompatible with `message.to_dict` and the actual REST API payloads

Opened this issue · 3 comments

The message constructor is incompatible with message.to_dict (when preserving_proto_field_name is used) and the actual REST API payloads. Meanwhile, .from_json is compatible with .to_json.

Environment details

  • Programming language: 3.7
  • OS: Linux
  • Package version: 1.22.1

Steps to reproduce

from google.cloud import batch_v1

This works:

batch_v1.Job.from_json('{"taskGroups": [{}]}')

This does not work:

batch_v1.Job({"taskGroups": [{}]})
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-145-58ddc394e1ed> in <module>
----> 1 batch_v1.Job({"taskGroups": [{}]})

/opt/conda/lib/python3.7/site-packages/proto/message.py in __init__(self, mapping, ignore_unknown_fields, **kwargs)
    564 
    565                 raise ValueError(
--> 566                     "Unknown field for {}: {}".format(self.__class__.__name__, key)
    567                 )
    568 

ValueError: Unknown field for Job: taskGroups

Compatibility between constructor and message.to_dict is not an intended feature of the library. As you noted, batch_v1.Job({"task_groups": [{}]}) will work. If you'd like this as a feature, please open a feature request instead of a bug.

What is the constructor compatible with?

@atulep

Compatibility between constructor and message.to_dict is not an intended feature of the library.

The documentation seems to imply this is in fact supported:
https://proto-plus-python.readthedocs.io/en/latest/messages.html#serialization

Similarly, messages can be converted into dictionaries via the to_dict() helper method. There is no from_dict() method because the Message constructor already allows construction from mapping types.