Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

int32 field is not defined when set to zero unless wrapped in oneof statement #1655

Closed
christian-storm opened this issue Jun 8, 2016 · 1 comment

Comments

@christian-storm
Copy link

Using protobuf release 3.0.0b3 and b2.

I built up this example to demonstrate.

document_markup.proto

syntax = "proto3";

message Corpus {
  message Document {
    message TextLabel {
      string label = 1;
      repeated TextSpan text_span = 2;
    }
    repeated TextLabel text_label = 2;
    message TextSpan {
      oneof test_oneof {
        int32 start_char_offset = 1;
      }
      int32 end_char_offset = 2;
    }
  }
  repeated Document document = 1;
}

When I run this python code:

#!/env/python

import document_markup_pb2

corpus = document_markup_pb2.Corpus()
doc = corpus.document.add()
text_label = doc.text_label.add()
text_label.label = u'foo'
text_span = text_label.text_span.add()
text_span.start_char_offset = 0
text_span.end_char_offset = 0

print text_span.start_char_offset
print text_span.end_char_offset
print text_label

I get:
0
0
label: "foo"
text_span {
start_char_offset: 0
}
so it is getting set but not staying set unless I use some oneof magic.

It seems things are going south python_message.py field_setter(self, new_value) when new_value = 0
In particular this if statement is true when new_value is zero:
if clear_when_set_to_default and not new_value:
self._fields.pop(field, None)

After having read this and "Also note that if a scalar message field is set to its default, the value will not be serialized on the wire." in the defaults documentation this may actually be desired behavior. However, this is confusing as all get out. Seemingly defaults are there to be relied on? However, in my case the field was actually being set to zero (not defaulting to zero) but acts as if that field was never set! Oh man, I chased my tail on this one.

@xfxyjwf
Copy link
Contributor

xfxyjwf commented Jun 22, 2016

duplicate of #1606

@xfxyjwf xfxyjwf closed this as completed Jun 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants