From 5dbbda58fbf9792e31962515adb98ac39fa88bcd Mon Sep 17 00:00:00 2001 From: Jeroen van Disseldorp Date: Tue, 3 Dec 2024 23:13:13 +0100 Subject: [PATCH] [Protobuf] Fix generation of "optional" labels in proto3 schema outW When parsing a Protobuf schema, and then generating it back, fields marked with "optional" become required. This patch checks per field if it needs to set the "proto3Optional" flag (ignored for oneOf fields). The flag is set if the field is optional, rendering the expected "optional" keyword in fileElement.toSchema(). --- .../registry/utils/protobuf/schema/MessageDefinition.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/utils/protobuf-schema-utilities/src/main/java/io/apicurio/registry/utils/protobuf/schema/MessageDefinition.java b/utils/protobuf-schema-utilities/src/main/java/io/apicurio/registry/utils/protobuf/schema/MessageDefinition.java index efc70a671a..c8521f7d82 100644 --- a/utils/protobuf-schema-utilities/src/main/java/io/apicurio/registry/utils/protobuf/schema/MessageDefinition.java +++ b/utils/protobuf-schema-utilities/src/main/java/io/apicurio/registry/utils/protobuf/schema/MessageDefinition.java @@ -136,6 +136,11 @@ private void doAddField( // Note: changed if (label != null) { fieldBuilder.setLabel(label); + // If this field is a regular optional field, then force the "optional" keyword in the schema output. + // Ignore if the field is part of a oneOf. + if (oneofBuilder == null && label.equals(DescriptorProtos.FieldDescriptorProto.Label.LABEL_OPTIONAL)) { + fieldBuilder.setProto3Optional(true); + } } DescriptorProtos.FieldDescriptorProto.Type primType = sTypeMap.get(type); if (primType != null) {