jhump / protoreflect

Reflection (Rich Descriptors) for Go Protocol Buffers
Apache License 2.0
1.33k stars 170 forks source link

First enum value must be 0 in proto3 [protoprint] #581

Closed japersik closed 10 months ago

japersik commented 10 months ago

Hi, I found another invalid case in protoprinter. If you make a negative identifier in enum (yes, it is possible), then after compiling into a descriptor something like this:

enum MY_ENUM {
    MY_ENUM_UNSPECIFIED = 0;
    MY_ENUM_ONE = 1;

    FIELD_NEGATIVE_ID = -1;
}

after using protoprint, it turns into

enum MY_ENUM {
    FIELD_NEGATIVE_ID = -1;
    MY_ENUM_UNSPECIFIED = 0;
    MY_ENUM_ONE = 1;
}

The fields are sorted in ascending order. As a result, we get an invalid proto3 file because First enum value must be 0 in proto3

japersik commented 10 months ago

It reproduce when

protoprint.Printer{
    SortElements: true
}
jhump commented 10 months ago

Fixed in #582