Skip to content

Commit 8d7b4e6

Browse files
authored
Merge pull request #11946 from protocolbuffers/cp-enum-closed-comment
Document known quirks of EnumDescriptor::is_closed() when importing a…
2 parents b7f7171 + a594141 commit 8d7b4e6

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

java/core/src/main/java/com/google/protobuf/Descriptors.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,7 @@ public static FileDescriptor buildFrom(FileDescriptorProto proto, FileDescriptor
303303
* two messages were defined with the same name.
304304
*/
305305
public static FileDescriptor buildFrom(
306-
FileDescriptorProto proto,
307-
FileDescriptor[] dependencies,
308-
boolean allowUnknownDependencies)
306+
FileDescriptorProto proto, FileDescriptor[] dependencies, boolean allowUnknownDependencies)
309307
throws DescriptorValidationException {
310308
// Building descriptors involves two steps: translating and linking.
311309
// In the translation step (implemented by FileDescriptor's
@@ -462,9 +460,9 @@ public static FileDescriptor internalBuildGeneratedFileFrom(
462460
}
463461

464462
/**
465-
* This method is to be called by generated code only. It updates the
466-
* FileDescriptorProto associated with the descriptor by parsing it again with the given
467-
* ExtensionRegistry. This is needed to recognize custom options.
463+
* This method is to be called by generated code only. It updates the FileDescriptorProto
464+
* associated with the descriptor by parsing it again with the given ExtensionRegistry. This is
465+
* needed to recognize custom options.
468466
*/
469467
public static void internalUpdateFileDescriptor(
470468
FileDescriptor descriptor, ExtensionRegistry registry) {
@@ -1778,10 +1776,23 @@ public FileDescriptor getFile() {
17781776
* <p>Closed enum means that it:
17791777
*
17801778
* <ul>
1781-
* <li>Has a fixed set of named values. *
1779+
* <li>Has a fixed set of values, rather than being equivalent to an int32.
17821780
* <li>Encountering values not in this set causes them to be treated as unknown fields.
17831781
* <li>The first value (i.e., the default) may be nonzero.
17841782
* </ul>
1783+
*
1784+
* <p>WARNING: Some runtimes currently have a quirk where non-closed enums are treated as closed
1785+
* when used as the type of fields defined in a `syntax = proto2;` file. This quirk is not
1786+
* present in all runtimes; as of writing, we know that:
1787+
*
1788+
* <ul>
1789+
* <li> C++, Java, and C++-based Python share this quirk.
1790+
* <li> UPB and UPB-based Python do not.
1791+
* <li> PHP and Ruby treat all enums as open regardless of declaration.
1792+
* </ul>
1793+
*
1794+
* <p>Care should be taken when using this function to respect the target runtime's enum
1795+
* handling quirks.
17851796
*/
17861797
public boolean isClosed() {
17871798
return getFile().getSyntax() != Syntax.PROTO3;

python/google/protobuf/descriptor.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -739,13 +739,25 @@ def __init__(self, name, full_name, filename, values,
739739

740740
@property
741741
def is_closed(self):
742-
"""If the enum is closed.
742+
"""Returns true whether this is a "closed" enum.
743743
744-
closed enum means:
745-
- Has a fixed set of named values.
746-
- Encountering values not in this set causes them to be treated as
747-
unknown fields.
748-
- The first value (i.e., the default) may be nonzero.
744+
This means that it:
745+
- Has a fixed set of values, rather than being equivalent to an int32.
746+
- Encountering values not in this set causes them to be treated as unknown
747+
fields.
748+
- The first value (i.e., the default) may be nonzero.
749+
750+
WARNING: Some runtimes currently have a quirk where non-closed enums are
751+
treated as closed when used as the type of fields defined in a
752+
`syntax = proto2;` file. This quirk is not present in all runtimes; as of
753+
writing, we know that:
754+
755+
- C++, Java, and C++-based Python share this quirk.
756+
- UPB and UPB-based Python do not.
757+
- PHP and Ruby treat all enums as open regardless of declaration.
758+
759+
Care should be taken when using this function to respect the target
760+
runtime's enum handling quirks.
749761
"""
750762
return self.file.syntax == 'proto2'
751763

src/google/protobuf/descriptor.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,10 +1151,22 @@ class PROTOBUF_EXPORT EnumDescriptor : private internal::SymbolBase {
11511151
bool is_placeholder() const;
11521152

11531153
// Returns true whether this is a "closed" enum, meaning that it:
1154-
// - Has a fixed set of named values.
1154+
// - Has a fixed set of values, rather than being equivalent to an int32.
11551155
// - Encountering values not in this set causes them to be treated as unknown
11561156
// fields.
11571157
// - The first value (i.e., the default) may be nonzero.
1158+
//
1159+
// WARNING: Some runtimes currently have a quirk where non-closed enums are
1160+
// treated as closed when used as the type of fields defined in a
1161+
// `syntax = proto2;` file. This quirk is not present in all runtimes; as of
1162+
// writing, we know that:
1163+
//
1164+
// - C++, Java, and C++-based Python share this quirk.
1165+
// - UPB and UPB-based Python do not.
1166+
// - PHP and Ruby treat all enums as open regardless of declaration.
1167+
//
1168+
// Care should be taken when using this function to respect the target
1169+
// runtime's enum handling quirks.
11581170
bool is_closed() const;
11591171

11601172
// Reserved fields -------------------------------------------------

0 commit comments

Comments
 (0)