Fix descriptor validation logic for packed enum fields.
This commit is contained in:
parent
6147768171
commit
f5691a617e
2 changed files with 34 additions and 7 deletions
|
@ -1103,13 +1103,6 @@ public final class Descriptors {
|
|||
"Field numbers must be positive integers.");
|
||||
}
|
||||
|
||||
// Only repeated primitive fields may be packed.
|
||||
if (proto.getOptions().getPacked() && !isPackable()) {
|
||||
throw new DescriptorValidationException(this,
|
||||
"[packed = true] can only be specified for repeated primitive " +
|
||||
"fields.");
|
||||
}
|
||||
|
||||
if (isExtension) {
|
||||
if (!proto.hasExtendee()) {
|
||||
throw new DescriptorValidationException(this,
|
||||
|
@ -1218,6 +1211,13 @@ public final class Descriptors {
|
|||
}
|
||||
}
|
||||
|
||||
// Only repeated primitive fields may be packed.
|
||||
if (proto.getOptions().getPacked() && !isPackable()) {
|
||||
throw new DescriptorValidationException(this,
|
||||
"[packed = true] can only be specified for repeated primitive " +
|
||||
"fields.");
|
||||
}
|
||||
|
||||
// We don't attempt to parse the default value until here because for
|
||||
// enums we need the enum type's descriptor.
|
||||
if (proto.hasDefaultValue()) {
|
||||
|
|
|
@ -705,4 +705,31 @@ public class DescriptorsTest extends TestCase {
|
|||
assertFalse(TestMultipleExtensionRanges.getDescriptor().isExtensionNumber(4142));
|
||||
assertTrue(TestMultipleExtensionRanges.getDescriptor().isExtensionNumber(4143));
|
||||
}
|
||||
|
||||
public void testPackedEnumField() throws Exception {
|
||||
FileDescriptorProto fileDescriptorProto = FileDescriptorProto.newBuilder()
|
||||
.setName("foo.proto")
|
||||
.addEnumType(EnumDescriptorProto.newBuilder()
|
||||
.setName("Enum")
|
||||
.addValue(EnumValueDescriptorProto.newBuilder()
|
||||
.setName("FOO")
|
||||
.setNumber(1)
|
||||
.build())
|
||||
.build())
|
||||
.addMessageType(DescriptorProto.newBuilder()
|
||||
.setName("Message")
|
||||
.addField(FieldDescriptorProto.newBuilder()
|
||||
.setName("foo")
|
||||
.setTypeName("Enum")
|
||||
.setNumber(1)
|
||||
.setLabel(FieldDescriptorProto.Label.LABEL_REPEATED)
|
||||
.setOptions(DescriptorProtos.FieldOptions.newBuilder()
|
||||
.setPacked(true)
|
||||
.build())
|
||||
.build())
|
||||
.build())
|
||||
.build();
|
||||
Descriptors.FileDescriptor.buildFrom(
|
||||
fileDescriptorProto, new FileDescriptor[0]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue