Document known quirks of EnumDescriptor::is_closed() when importing across files with different syntaxes.

PiperOrigin-RevId: 509581394
pull/11946/head
Protobuf Team Bot 2 years ago committed by Sandy Zhang
parent b7f7171c31
commit a594141cc4
  1. 25
      java/core/src/main/java/com/google/protobuf/Descriptors.java
  2. 24
      python/google/protobuf/descriptor.py
  3. 14
      src/google/protobuf/descriptor.h

@ -303,9 +303,7 @@ public final class Descriptors {
* two messages were defined with the same name. * two messages were defined with the same name.
*/ */
public static FileDescriptor buildFrom( public static FileDescriptor buildFrom(
FileDescriptorProto proto, FileDescriptorProto proto, FileDescriptor[] dependencies, boolean allowUnknownDependencies)
FileDescriptor[] dependencies,
boolean allowUnknownDependencies)
throws DescriptorValidationException { throws DescriptorValidationException {
// Building descriptors involves two steps: translating and linking. // Building descriptors involves two steps: translating and linking.
// In the translation step (implemented by FileDescriptor's // In the translation step (implemented by FileDescriptor's
@ -462,9 +460,9 @@ public final class Descriptors {
} }
/** /**
* This method is to be called by generated code only. It updates the * This method is to be called by generated code only. It updates the FileDescriptorProto
* FileDescriptorProto associated with the descriptor by parsing it again with the given * associated with the descriptor by parsing it again with the given ExtensionRegistry. This is
* ExtensionRegistry. This is needed to recognize custom options. * needed to recognize custom options.
*/ */
public static void internalUpdateFileDescriptor( public static void internalUpdateFileDescriptor(
FileDescriptor descriptor, ExtensionRegistry registry) { FileDescriptor descriptor, ExtensionRegistry registry) {
@ -1778,10 +1776,23 @@ public final class Descriptors {
* <p>Closed enum means that it: * <p>Closed enum means that it:
* *
* <ul> * <ul>
* <li>Has a fixed set of named values. * * <li>Has a fixed set of values, rather than being equivalent to an int32.
* <li>Encountering values not in this set causes them to be treated as unknown fields. * <li>Encountering values not in this set causes them to be treated as unknown fields.
* <li>The first value (i.e., the default) may be nonzero. * <li>The first value (i.e., the default) may be nonzero.
* </ul> * </ul>
*
* <p>WARNING: Some runtimes currently have a quirk where non-closed enums are treated as closed
* when used as the type of fields defined in a `syntax = proto2;` file. This quirk is not
* present in all runtimes; as of writing, we know that:
*
* <ul>
* <li> C++, Java, and C++-based Python share this quirk.
* <li> UPB and UPB-based Python do not.
* <li> PHP and Ruby treat all enums as open regardless of declaration.
* </ul>
*
* <p>Care should be taken when using this function to respect the target runtime's enum
* handling quirks.
*/ */
public boolean isClosed() { public boolean isClosed() {
return getFile().getSyntax() != Syntax.PROTO3; return getFile().getSyntax() != Syntax.PROTO3;

@ -739,13 +739,25 @@ class EnumDescriptor(_NestedDescriptorBase):
@property @property
def is_closed(self): def is_closed(self):
"""If the enum is closed. """Returns true whether this is a "closed" enum.
closed enum means: This means that it:
- Has a fixed set of named values. - Has a fixed set of values, rather than being equivalent to an int32.
- Encountering values not in this set causes them to be treated as - Encountering values not in this set causes them to be treated as unknown
unknown fields. fields.
- The first value (i.e., the default) may be nonzero. - The first value (i.e., the default) may be nonzero.
WARNING: Some runtimes currently have a quirk where non-closed enums are
treated as closed when used as the type of fields defined in a
`syntax = proto2;` file. This quirk is not present in all runtimes; as of
writing, we know that:
- C++, Java, and C++-based Python share this quirk.
- UPB and UPB-based Python do not.
- PHP and Ruby treat all enums as open regardless of declaration.
Care should be taken when using this function to respect the target
runtime's enum handling quirks.
""" """
return self.file.syntax == 'proto2' return self.file.syntax == 'proto2'

@ -1151,10 +1151,22 @@ class PROTOBUF_EXPORT EnumDescriptor : private internal::SymbolBase {
bool is_placeholder() const; bool is_placeholder() const;
// Returns true whether this is a "closed" enum, meaning that it: // Returns true whether this is a "closed" enum, meaning that it:
// - Has a fixed set of named values. // - Has a fixed set of values, rather than being equivalent to an int32.
// - Encountering values not in this set causes them to be treated as unknown // - Encountering values not in this set causes them to be treated as unknown
// fields. // fields.
// - The first value (i.e., the default) may be nonzero. // - The first value (i.e., the default) may be nonzero.
//
// WARNING: Some runtimes currently have a quirk where non-closed enums are
// treated as closed when used as the type of fields defined in a
// `syntax = proto2;` file. This quirk is not present in all runtimes; as of
// writing, we know that:
//
// - C++, Java, and C++-based Python share this quirk.
// - UPB and UPB-based Python do not.
// - PHP and Ruby treat all enums as open regardless of declaration.
//
// Care should be taken when using this function to respect the target
// runtime's enum handling quirks.
bool is_closed() const; bool is_closed() const;
// Reserved fields ------------------------------------------------- // Reserved fields -------------------------------------------------

Loading…
Cancel
Save