From 36d131520ce10d6a79c663dcd61c50a080fc3610 Mon Sep 17 00:00:00 2001 From: kafkiansky Date: Mon, 23 Nov 2026 16:04:52 +0300 Subject: [PATCH] fix: report proto2 groups instead of generating them as messages A group is written between start and end group tags, while the plugin generated it as a length-prefixed message: an incompatible wire format without a single warning. The runtime has no group encoding, so such a field now stops the generation with CodeCannotBeGenerated, the same way editions DELIMITED fields already do. Co-Authored-By: Claude Fable 5.2 --- src/Plugin/Generator/ProtoGenerator.php | 7 ++ tests/CompilerGroupTest.php | 99 +++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 tests/CompilerGroupTest.php diff --git a/src/Plugin/Generator/ProtoGenerator.php b/src/Plugin/Generator/ProtoGenerator.php index 90fe84a..cbd8b6c 100644 --- a/src/Plugin/Generator/ProtoGenerator.php +++ b/src/Plugin/Generator/ProtoGenerator.php @@ -317,6 +317,13 @@ private function generateMessage( $properties = []; foreach ($message->fields as $field) { + // A group is written between start and end group tags, not length-prefixed like a message. The runtime + // has no such encoding, so generating it as a message would silently produce an incompatible wire format. + // Checked before oneofs are set aside, as a group may also be a oneof variant. + if ($field->type === FieldDescriptorProto\Type::TYPE_GROUP) { + throw new CodeCannotBeGenerated("Message {$message->fqcn}: field {$field->name} is a group, groups are not supported."); + } + if ($field->oneOfIndex !== null && $field->proto3Optional === null) { $oneOfByIndex[$field->oneOfIndex][] = $field; diff --git a/tests/CompilerGroupTest.php b/tests/CompilerGroupTest.php new file mode 100644 index 0000000..948cb50 --- /dev/null +++ b/tests/CompilerGroupTest.php @@ -0,0 +1,99 @@ +expectException(CodeCannotBeGenerated::class); + $this->expectExceptionMessage('Message t.M: field result is a group, groups are not supported.'); + + new Compiler(Encoder\Builder::buildDefault(), __DIR__ . '/../map.json')->compile(new CodeGeneratorRequest( + fileToGenerate: ['t.proto'], + protoFile: [ + new FileDescriptorProto( + name: 't.proto', + package: 't', + messageType: [ + new DescriptorProto( + name: 'M', + field: [$field], + nestedType: [ + new DescriptorProto( + name: 'Result', + field: [ + new FieldDescriptorProto( + name: 'url', + number: 2, + label: Label::LABEL_OPTIONAL, + type: Type::TYPE_STRING, + jsonName: 'url', + ), + ], + ), + ], + oneofDecl: $oneof !== null ? [$oneof] : [], + ), + ], + ), + ], + )); + } + + /** + * @return iterable + */ + public static function provideGroupIsReportedCases(): iterable + { + // syntax = "proto2"; message M { optional group Result = 1 { optional string url = 2; } } + yield 'optional' => [self::group(Label::LABEL_OPTIONAL), null]; + + // syntax = "proto2"; message M { required group Result = 1 { optional string url = 2; } } + yield 'required' => [self::group(Label::LABEL_REQUIRED), null]; + + // syntax = "proto2"; message M { repeated group Result = 1 { optional string url = 2; } } + yield 'repeated' => [self::group(Label::LABEL_REPEATED), null]; + + // syntax = "proto2"; message M { oneof o { group Result = 1 { optional string url = 2; } } } + yield 'oneof variant' => [self::group(Label::LABEL_OPTIONAL, oneofIndex: 0), new OneofDescriptorProto('o')]; + } + + private static function group(Label $label, ?int $oneofIndex = null): FieldDescriptorProto + { + return new FieldDescriptorProto( + name: 'result', + number: 1, + label: $label, + type: Type::TYPE_GROUP, + typeName: '.t.M.Result', + oneofIndex: $oneofIndex, + jsonName: 'result', + ); + } +}