Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,13 @@ final void writeField(Object value) {

private void writeAllFields(MessageOrBuilder pb) {
Descriptor messageDescriptor = pb.getDescriptorForType();
Descriptors.FileDescriptor.Syntax syntax =
messageDescriptor.getFile().getSyntax();
String syntax = messageDescriptor.getFile().toProto().getSyntax();
if ("editions".equals(syntax)) {
throw new UnsupportedOperationException("protocol buffers 'editions' not supported");
}
boolean isProto2 = !"proto3".equals(syntax);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to check the versions explicitly, and raise on anything else (for example editions, as above).

Suggested change
boolean isProto2 = !"proto3".equals(syntax);
boolean isProto2 = "proto2".equals(syntax);

Copy link
Author

@uwemaurer uwemaurer Oct 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to keep the logic the same as in the protobuf library (v4), they have this code:

 Edition getEdition() {
      switch (proto.getSyntax()) {
        case "editions":
          return proto.getEdition();
        case "proto3":
          return Edition.EDITION_PROTO3;
        default:
          return Edition.EDITION_PROTO2;
      }
    }

In my tests the value of syntax was an empty string (not "proto2") for proto2 files, this is why it just compares against "editions" and "proto3".


if (Descriptors.FileDescriptor.Syntax.PROTO2.equals(syntax)) {
if (isProto2) {
// Returns changed fields with values. Map is ordered by id.
Map<FieldDescriptor, Object> changedPbFields = pb.getAllFields();

Expand All @@ -464,7 +467,7 @@ private void writeAllFields(MessageOrBuilder pb) {
int fieldIndex = fieldDescriptor.getIndex();
fieldWriters[fieldIndex].writeField(entry.getValue());
}
} else if (Descriptors.FileDescriptor.Syntax.PROTO3.equals(syntax)) {
} else {
List<FieldDescriptor> fieldDescriptors = messageDescriptor.getFields();
for (FieldDescriptor fieldDescriptor : fieldDescriptors) {
FieldDescriptor.Type type = fieldDescriptor.getType();
Expand Down