Skip to content

Python, Protobuf 3: ListFields method doesn't return field if it has value equaled default value #1772

@ve-i-uj

Description

@ve-i-uj

In protobuf 3 I can't get the fields in the message which are not empty. ListFields method doesn't work like it is described in the documentation. This problem appears when value in field equals default value. So, for example, I can't get boolean field through ListFields method if the value of field is False or integer field is 0 and so on. In protobuf 2 ListFields method works correct. Following example illustrates problem:

Protofile:

syntax = "proto3";

message TestMethodResponse{
    int32 integer = 1;
    string string = 2;
    bool boolean = 3;
    string empty = 4;
    string nonempty = 5;    
}

Python example:

import testservice_pb2

req = testservice_pb2.TestMethodResponse(
    integer=1,
    string='string',
    boolean=True,
    nonempty='nonempty'
)

print 'All is well:'
print [f[0].name for f in req.ListFields()]

req = testservice_pb2.TestMethodResponse(
    integer=0,
    string='',
    boolean=False,
    nonempty='nonempty'
)

print "Something's wrong:"
print [f[0].name for f in req.ListFields()]

Output:

>>> All is well:
>>> ['integer', 'string', 'boolean', 'nonempty']
>>> Something's wrong:
>>> ['nonempty']

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions