From ad631ffc3b377a0d416f1fa6b64a8887d9cdf983 Mon Sep 17 00:00:00 2001 From: kafkiansky Date: Sun, 22 Nov 2026 18:37:25 +0300 Subject: [PATCH] fix: honor an explicit [packed = false] in proto3 Co-Authored-By: Claude Fable 5.2 --- src/Plugin/Generator/ProtoGenerator.php | 4 + tests/GeneratedCode/samples/proto3_packed.php | 70 ++++++++++++++++ .../proto3_packed/proto3_packed.proto | 35 ++++++++ .../proto3_packed/Proto3/Packed/V1/Color.php | 23 ++++++ .../Proto3/Packed/V1/DescriptorRegistry.php | 40 ++++++++++ .../proto3_packed/Proto3/Packed/V1/Inner.php | 26 ++++++ .../Proto3/Packed/V1/Repeated.php | 79 +++++++++++++++++++ .../Proto3/Packed/V1/autoload.metadata.php | 14 ++++ 8 files changed, 291 insertions(+) create mode 100644 tests/GeneratedCode/samples/proto3_packed.php create mode 100644 tests/fixtures/proto3_packed/proto3_packed.proto create mode 100644 tests/snapshots/proto3_packed/Proto3/Packed/V1/Color.php create mode 100644 tests/snapshots/proto3_packed/Proto3/Packed/V1/DescriptorRegistry.php create mode 100644 tests/snapshots/proto3_packed/Proto3/Packed/V1/Inner.php create mode 100644 tests/snapshots/proto3_packed/Proto3/Packed/V1/Repeated.php create mode 100644 tests/snapshots/proto3_packed/Proto3/Packed/V1/autoload.metadata.php diff --git a/src/Plugin/Generator/ProtoGenerator.php b/src/Plugin/Generator/ProtoGenerator.php index 2650102..2c13f2d 100644 --- a/src/Plugin/Generator/ProtoGenerator.php +++ b/src/Plugin/Generator/ProtoGenerator.php @@ -376,6 +376,10 @@ private function generateMessage( } elseif ($this->edition !== null) { // In editions, PACKED is the default for eligible types unless explicitly set to EXPANDED. $parameters[] = $features?->repeatedFieldEncoding === null || $features->repeatedFieldEncoding === FeatureSet\RepeatedFieldEncoding::PACKED; + } elseif ($field->options?->packed === false) { + // In proto3, packable fields are packed by default, which is also what the runtime does + // when packed is not given; only an explicit [packed = false] needs to be spelled out. + $parameters[] = false; } $reflectionType = Literal::new($runtime->reflection('ListT'), $parameters); diff --git a/tests/GeneratedCode/samples/proto3_packed.php b/tests/GeneratedCode/samples/proto3_packed.php new file mode 100644 index 0000000..da2f5a2 --- /dev/null +++ b/tests/GeneratedCode/samples/proto3_packed.php @@ -0,0 +1,70 @@ + new Repeated( + int32Default: [1, -2, 3], + int32Packed: [4, 5], + int32Unpacked: [-6, 7], + enumDefault: [Color::COLOR_RED, Color::COLOR_GREEN], + enumPacked: [Color::COLOR_GREEN], + enumUnpacked: [Color::COLOR_RED, Color::COLOR_UNSPECIFIED], + boolDefault: [true, false], + boolPacked: [false, true], + boolUnpacked: [true, true, false], + sint64Unpacked: [-1, 1], + fixed32Unpacked: [1, 4_294_967_295], + doubleUnpacked: [1.5, -0.25], + uint64Unpacked: [new Number(1), new Number('18446744073709551615')], + stringDefault: ['a', ''], + stringUnpacked: ['b'], + bytesUnpacked: ["\x00\xff"], + messageUnpacked: [new Inner(1), new Inner()], + ), + 'wire encoding follows the packed option' => static function (): void { + $encoder = Encoder\Builder::buildDefault(); + + $expected = [ + // packed by default and with [packed = true]: one length-delimited record + 'int32Default' => [new Repeated(int32Default: [1, 2]), "\x0A\x02\x01\x02"], + 'int32Packed' => [new Repeated(int32Packed: [1, 2]), "\x12\x02\x01\x02"], + 'enumDefault' => [new Repeated(enumDefault: [Color::COLOR_RED, Color::COLOR_GREEN]), "\x22\x02\x01\x02"], + 'enumPacked' => [new Repeated(enumPacked: [Color::COLOR_RED, Color::COLOR_GREEN]), "\x2A\x02\x01\x02"], + 'boolDefault' => [new Repeated(boolDefault: [true, false]), "\x3A\x02\x01\x00"], + 'boolPacked' => [new Repeated(boolPacked: [true, false]), "\x42\x02\x01\x00"], + // [packed = false]: a record per element + 'int32Unpacked' => [new Repeated(int32Unpacked: [1, 2]), "\x18\x01\x18\x02"], + 'enumUnpacked' => [new Repeated(enumUnpacked: [Color::COLOR_RED, Color::COLOR_GREEN]), "\x30\x01\x30\x02"], + 'boolUnpacked' => [new Repeated(boolUnpacked: [true, false]), "\x48\x01\x48\x00"], + 'sint64Unpacked' => [new Repeated(sint64Unpacked: [-1, 1]), "\x50\x01\x50\x02"], + 'fixed32Unpacked' => [new Repeated(fixed32Unpacked: [1, 2]), "\x5D\x01\x00\x00\x00\x5D\x02\x00\x00\x00"], + 'doubleUnpacked' => [new Repeated(doubleUnpacked: [1.5]), "\x61" . pack('e', 1.5)], + 'uint64Unpacked' => [new Repeated(uint64Unpacked: [new Number(1), new Number(2)]), "\x68\x01\x68\x02"], + // non-packable types: a record per element regardless of the option + 'stringDefault' => [new Repeated(stringDefault: ['a', 'b']), "\x72\x01a\x72\x01b"], + 'stringUnpacked' => [new Repeated(stringUnpacked: ['a', 'b']), "\x7A\x01a\x7A\x01b"], + 'bytesUnpacked' => [new Repeated(bytesUnpacked: ["\x01"]), "\x82\x01\x01\x01"], + 'messageUnpacked' => [new Repeated(messageUnpacked: [new Inner(1)]), "\x8A\x01\x02\x08\x01"], + ]; + + foreach ($expected as $property => [$message, $bytes]) { + $actual = $encoder->encode($message); + + if ($actual !== $bytes) { + throw new LogicException(sprintf('%s: expected %s on the wire, got %s', $property, bin2hex($bytes), bin2hex($actual))); + } + } + }, +]; diff --git a/tests/fixtures/proto3_packed/proto3_packed.proto b/tests/fixtures/proto3_packed/proto3_packed.proto new file mode 100644 index 0000000..11e58aa --- /dev/null +++ b/tests/fixtures/proto3_packed/proto3_packed.proto @@ -0,0 +1,35 @@ +syntax = "proto3"; + +package proto3.packed.v1; + +enum Color { + COLOR_UNSPECIFIED = 0; + COLOR_RED = 1; + COLOR_GREEN = 2; +} + +message Inner { + int32 value = 1; +} + +// Repeated packable fields are packed by default in proto3; [packed = false] makes them expanded. +message Repeated { + repeated int32 int32_default = 1; + repeated int32 int32_packed = 2 [packed = true]; + repeated int32 int32_unpacked = 3 [packed = false]; + repeated Color enum_default = 4; + repeated Color enum_packed = 5 [packed = true]; + repeated Color enum_unpacked = 6 [packed = false]; + repeated bool bool_default = 7; + repeated bool bool_packed = 8 [packed = true]; + repeated bool bool_unpacked = 9 [packed = false]; + repeated sint64 sint64_unpacked = 10 [packed = false]; + repeated fixed32 fixed32_unpacked = 11 [packed = false]; + repeated double double_unpacked = 12 [packed = false]; + repeated uint64 uint64_unpacked = 13 [packed = false]; + // Non-packable types are never packed: [packed = false] changes nothing for them. + repeated string string_default = 14; + repeated string string_unpacked = 15 [packed = false]; + repeated bytes bytes_unpacked = 16 [packed = false]; + repeated Inner message_unpacked = 17 [packed = false]; +} diff --git a/tests/snapshots/proto3_packed/Proto3/Packed/V1/Color.php b/tests/snapshots/proto3_packed/Proto3/Packed/V1/Color.php new file mode 100644 index 0000000..5f07ff2 --- /dev/null +++ b/tests/snapshots/proto3_packed/Proto3/Packed/V1/Color.php @@ -0,0 +1,23 @@ +add(Registry\Descriptor::base64(self::PROTO3_PACKED_DESCRIPTOR_BUFFER), new File( + name: 'proto3_packed.proto', + messages: [ + new File\MessageDescriptor('proto3.packed.v1.Inner', \Proto3\Packed\V1\Inner::class), + new File\MessageDescriptor('proto3.packed.v1.Repeated', \Proto3\Packed\V1\Repeated::class), + ], + enums: [ + new File\EnumDescriptor('proto3.packed.v1.Color', \Proto3\Packed\V1\Color::class), + ], + )); + } +} diff --git a/tests/snapshots/proto3_packed/Proto3/Packed/V1/Inner.php b/tests/snapshots/proto3_packed/Proto3/Packed/V1/Inner.php new file mode 100644 index 0000000..01e1363 --- /dev/null +++ b/tests/snapshots/proto3_packed/Proto3/Packed/V1/Inner.php @@ -0,0 +1,26 @@ + $int32Default + * @param list $int32Packed + * @param list $int32Unpacked + * @param list<\Proto3\Packed\V1\Color> $enumDefault + * @param list<\Proto3\Packed\V1\Color> $enumPacked + * @param list<\Proto3\Packed\V1\Color> $enumUnpacked + * @param list $boolDefault + * @param list $boolPacked + * @param list $boolUnpacked + * @param list $sint64Unpacked + * @param list $fixed32Unpacked + * @param list $doubleUnpacked + * @param list<\BcMath\Number> $uint64Unpacked + * @param list $stringDefault Non-packable types are never packed: [packed = false] changes nothing for them. + * @param list $stringUnpacked + * @param list $bytesUnpacked + * @param list<\Proto3\Packed\V1\Inner> $messageUnpacked + */ + public function __construct( + #[Reflection\Field(1, new Reflection\ListT(Reflection\Int32T::T))] + public array $int32Default = [], + #[Reflection\Field(2, new Reflection\ListT(Reflection\Int32T::T))] + public array $int32Packed = [], + #[Reflection\Field(3, new Reflection\ListT(Reflection\Int32T::T, false))] + public array $int32Unpacked = [], + #[Reflection\Field(4, new Reflection\ListT(new Reflection\EnumT(\Proto3\Packed\V1\Color::class)))] + public array $enumDefault = [], + #[Reflection\Field(5, new Reflection\ListT(new Reflection\EnumT(\Proto3\Packed\V1\Color::class)))] + public array $enumPacked = [], + #[Reflection\Field(6, new Reflection\ListT(new Reflection\EnumT(\Proto3\Packed\V1\Color::class), false))] + public array $enumUnpacked = [], + #[Reflection\Field(7, new Reflection\ListT(Reflection\BoolT::T))] + public array $boolDefault = [], + #[Reflection\Field(8, new Reflection\ListT(Reflection\BoolT::T))] + public array $boolPacked = [], + #[Reflection\Field(9, new Reflection\ListT(Reflection\BoolT::T, false))] + public array $boolUnpacked = [], + #[Reflection\Field(10, new Reflection\ListT(Reflection\SInt64T::T, false))] + public array $sint64Unpacked = [], + #[Reflection\Field(11, new Reflection\ListT(Reflection\Fixed32T::T, false))] + public array $fixed32Unpacked = [], + #[Reflection\Field(12, new Reflection\ListT(Reflection\DoubleT::T, false))] + public array $doubleUnpacked = [], + #[Reflection\Field(13, new Reflection\ListT(Reflection\Uint64T::T, false))] + public array $uint64Unpacked = [], + #[Reflection\Field(14, new Reflection\ListT(Reflection\StringT::T))] + public array $stringDefault = [], + #[Reflection\Field(15, new Reflection\ListT(Reflection\StringT::T, false))] + public array $stringUnpacked = [], + #[Reflection\Field(16, new Reflection\ListT(Reflection\BytesT::T, false))] + public array $bytesUnpacked = [], + #[Reflection\Field(17, new Reflection\ListT(new Reflection\ObjectT(\Proto3\Packed\V1\Inner::class), false))] + public array $messageUnpacked = [], + ) {} +} diff --git a/tests/snapshots/proto3_packed/Proto3/Packed/V1/autoload.metadata.php b/tests/snapshots/proto3_packed/Proto3/Packed/V1/autoload.metadata.php new file mode 100644 index 0000000..0fd3a1c --- /dev/null +++ b/tests/snapshots/proto3_packed/Proto3/Packed/V1/autoload.metadata.php @@ -0,0 +1,14 @@ +register( + new \Thesis\Protobuf\Registry\OnceRegistrar(new \Proto3\Packed\V1\DescriptorRegistry()), +);