Fix has_presence to account for proto3 extensions in Java.

PiperOrigin-RevId: 613373080
pull/16074/head
Mike Kruskal 9 months ago committed by Copybara-Service
parent 0fd69cf163
commit dc484c131e
  1. 1
      java/core/src/main/java/com/google/protobuf/Descriptors.java
  2. 20
      java/core/src/test/java/com/google/protobuf/DescriptorsTest.java
  3. 2
      src/google/protobuf/unittest_proto3_extensions.proto

@ -1454,6 +1454,7 @@ public final class Descriptors {
return isProto3Optional
|| getType() == Type.MESSAGE
|| getType() == Type.GROUP
|| isExtension()
|| getContainingOneof() != null
|| this.features.getFieldPresence() != DescriptorProtos.FeatureSet.FieldPresence.IMPLICIT;
}

@ -54,6 +54,7 @@ import protobuf_unittest.UnittestProto.TestReservedFields;
import protobuf_unittest.UnittestProto.TestService;
import protobuf_unittest.UnittestRetention;
import proto3_unittest.UnittestProto3;
import protobuf_unittest.UnittestProto3Extensions.Proto3FileExtensions;
import java.util.Collections;
import java.util.List;
import org.junit.Before;
@ -1201,6 +1202,25 @@ public class DescriptorsTest {
assertThat(features.getExtension(JavaFeaturesProto.java).getUtf8Validation())
.isEqualTo(JavaFeaturesProto.JavaFeatures.Utf8Validation.DEFAULT);
}
@Test
public void testProto3ExtensionPresence() {
FileDescriptorProto.Builder file = FileDescriptorProto.newBuilder();
assertThat(file.getOptions().hasExtension(Proto3FileExtensions.singularInt)).isFalse();
assertThat(file.getOptions().getExtension(Proto3FileExtensions.singularInt)).isEqualTo(0);
file.getOptionsBuilder().setExtension(Proto3FileExtensions.singularInt, 1);
assertThat(file.getOptions().hasExtension(Proto3FileExtensions.singularInt)).isTrue();
assertThat(file.getOptions().getExtension(Proto3FileExtensions.singularInt)).isEqualTo(1);
}
@Test
public void testProto3ExtensionHasPresence() {
assertThat(Proto3FileExtensions.singularInt.getDescriptor().hasPresence()).isTrue();
assertThat(Proto3FileExtensions.repeatedInt.getDescriptor().hasPresence()).isFalse();
}
}
public static class FeatureInheritanceTest {

@ -4,6 +4,8 @@ package protobuf_unittest;
import "google/protobuf/descriptor.proto";
option java_outer_classname = "UnittestProto3Extensions";
// For testing proto3 extension behaviors.
message Proto3FileExtensions {
extend google.protobuf.FileOptions {

Loading…
Cancel
Save