Message extensions cannot have required fields.

1,209 views
Skip to first unread message

Anthony Sampton

unread,
Oct 14, 2014, 12:47:21 PM10/14/14
to prot...@googlegroups.com
After the latest release (2.6.0) all my protos wouldn't compile.
Protoc gives me the following error: "Message extensions cannot have required fields."

Can anyone explain me why can't I use the required field in extention in the code below?

message MyMsgA {
    extensions
100 to max;
    required int32 i
= 1;
}
 
message
MyMsgB {
    extend
MyMsgA {
        required
MyMsgB b_data = 100;
   
}
    required
string s = 1;
}


Thanks in advance.

Anthony Sampton

unread,
Oct 25, 2014, 8:28:58 AM10/25/14
to prot...@googlegroups.com

I'm pretty sure it's not only me who faced with this problem, BUMP.

Oliver Jowett

unread,
Oct 25, 2014, 9:15:08 AM10/25/14
to Anthony Sampton, Protocol Buffers
Presumably this is because, if your example was allowed, a valid
MyMsgA encoding would no longer be a valid MyMsgB encoding.

Oliver

On 25 October 2014 13:28, Anthony Sampton <anthony...@gmail.com> wrote:
>
> I'm pretty sure it's not only me who faced with this problem, BUMP.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to protobuf+u...@googlegroups.com.
> To post to this group, send email to prot...@googlegroups.com.
> Visit this group at http://groups.google.com/group/protobuf.
> For more options, visit https://groups.google.com/d/optout.

Oliver Jowett

unread,
Oct 25, 2014, 9:17:26 AM10/25/14
to Anthony Sampton, Protocol Buffers
On 25 October 2014 14:15, Oliver Jowett <oliver...@gmail.com> wrote:
> Presumably this is because, if your example was allowed, a valid
> MyMsgA encoding would no longer be a valid MyMsgB encoding.

Sorry, I mean, "would no longer be a valid encoding of MyMsgA as
extended by the extension declared within MyMsgB".

Oliver

Feng Xiao

unread,
Oct 25, 2014, 5:19:59 PM10/25/14
to Anthony Sampton, Protocol Buffers
This is working as intended. Extensions, by its definition, is something optionally attached to a message and it doesn't make sense for it to be required, and technically there is no way to enforce this requirement because a binary can be compiled without the extension definition in which case there is no way to know whether the extension is required or optional.

And the change in 2.6.0 is actually a bug fix because in previous versions the required label is simply ignored and the extension is treated as optional any way.

On Sat, Oct 25, 2014 at 5:28 AM, Anthony Sampton <anthony...@gmail.com> wrote:

I'm pretty sure it's not only me who faced with this problem, BUMP.

--

Feng Xiao

unread,
Oct 25, 2014, 5:21:47 PM10/25/14
to Anthony Sampton, Protocol Buffers
To clarify, required extensions are disallowed, required fields in an extension message is allowed. I.e., the following is ok:
message A {
  extensions 1 to max;
}
message B {
  extend A { optional B extension = 1; }
  required string s = 1;
Reply all
Reply to author
Forward
0 new messages