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

Fix descriptor validation logic for packed enum fields. #39

Merged
merged 1 commit into from
Oct 3, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions java/src/main/java/com/google/protobuf/Descriptors.java
Original file line number Diff line number Diff line change
Expand Up @@ -1103,13 +1103,6 @@ private FieldDescriptor(final FieldDescriptorProto proto,
"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,
Expand Down Expand Up @@ -1218,6 +1211,13 @@ private void crossLink() throws DescriptorValidationException {
}
}

// 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()) {
Expand Down
27 changes: 27 additions & 0 deletions java/src/test/java/com/google/protobuf/DescriptorsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -705,4 +705,31 @@ public void testMessageDescriptorExtensions() throws Exception {
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]);
}
}