From 49639406577dce2ef48258bd75a2710a6c862408 Mon Sep 17 00:00:00 2001 From: kafkiansky Date: Tue, 17 Nov 2026 08:51:04 +0300 Subject: [PATCH 1/2] fix: reject packed repeated fields of non-packable types --- CHANGELOG.md | 7 + src/Exception/InvalidSchema.php | 15 ++ .../Internal/Visitor/IsPackable.php | 121 +++++++++++++++ src/Reflection/ListT.php | 13 +- src/Type/ListT.php | 13 +- src/Type/Visitor/IsPacked.php | 101 ++++++++++++- src/Value.php | 1 + src/constructors.php | 2 + tests/PackedListTest.php | 138 ++++++++++++++++++ 9 files changed, 402 insertions(+), 9 deletions(-) create mode 100644 src/Exception/InvalidSchema.php create mode 100644 src/Reflection/Internal/Visitor/IsPackable.php create mode 100644 tests/PackedListTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e0e2bb..7c25b42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,3 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +### Changed + +- Declaring a repeated field of a non-packable type (string, bytes, message) as `packed: true` now throws + `Exception\InvalidSchema` when the type is created. It used to produce output that no parser reads correctly. diff --git a/src/Exception/InvalidSchema.php b/src/Exception/InvalidSchema.php new file mode 100644 index 0000000..e096d2e --- /dev/null +++ b/src/Exception/InvalidSchema.php @@ -0,0 +1,15 @@ + + */ +final class IsPackable extends DefaultTypeVisitor +{ + #[\Override] + public function bool(BoolT $type): bool + { + return true; + } + + #[\Override] + public function float(FloatT $type): bool + { + return true; + } + + #[\Override] + public function double(DoubleT $type): bool + { + return true; + } + + #[\Override] + public function int32(Int32T $type): bool + { + return true; + } + + #[\Override] + public function uint32(Uint32T $type): bool + { + return true; + } + + #[\Override] + public function sint32(SInt32T $type): bool + { + return true; + } + + #[\Override] + public function int64(Int64T $type): bool + { + return true; + } + + #[\Override] + public function uint64(Uint64T $type): bool + { + return true; + } + + #[\Override] + public function sint64(SInt64T $type): bool + { + return true; + } + + #[\Override] + public function fixed32(Fixed32T $type): bool + { + return true; + } + + #[\Override] + public function sfixed32(SFixed32T $type): bool + { + return true; + } + + #[\Override] + public function fixed64(Fixed64T $type): bool + { + return true; + } + + #[\Override] + public function sfixed64(SFixed64T $type): bool + { + return true; + } + + #[\Override] + public function enum(EnumT $type): bool + { + return true; + } + + #[\Override] + protected function default(Type $type): bool + { + return false; + } +} diff --git a/src/Reflection/ListT.php b/src/Reflection/ListT.php index d429bd8..745cae3 100644 --- a/src/Reflection/ListT.php +++ b/src/Reflection/ListT.php @@ -4,6 +4,9 @@ namespace Thesis\Protobuf\Reflection; +use Thesis\Protobuf\Exception\InvalidSchema; +use Thesis\Protobuf\Reflection\Internal\Visitor\IsPackable; + /** * @api * @template T @@ -13,11 +16,19 @@ { /** * @param Type $element + * @throws InvalidSchema if a non-packable element type is declared packed */ public function __construct( public Type $element, public ?bool $packed = null, - ) {} + ) { + if ($packed === true && !$element->accept(new IsPackable())) { + throw new InvalidSchema(\sprintf( + 'Repeated fields of type "%s" cannot be packed: only scalar numeric, bool and enum elements can.', + $element::class, + )); + } + } #[\Override] public function accept(Visitor $visitor): mixed diff --git a/src/Type/ListT.php b/src/Type/ListT.php index 54ea5b0..98e67ff 100644 --- a/src/Type/ListT.php +++ b/src/Type/ListT.php @@ -4,7 +4,9 @@ namespace Thesis\Protobuf\Type; +use Thesis\Protobuf\Exception\InvalidSchema; use Thesis\Protobuf\Type; +use Thesis\Protobuf\Type\Visitor\IsPacked; /** * @template T @@ -14,11 +16,20 @@ { /** * @param Type $element + * @throws InvalidSchema if a non-packable element type is declared packed */ public function __construct( public Type $element, public ?bool $packed = null, - ) {} + ) { + // A recursive message is not resolved here: the type it refers to may still be under construction. + if ($packed === true && ($element instanceof RecursionT || !$element->accept(new IsPacked()))) { + throw new InvalidSchema(\sprintf( + 'Repeated fields of type "%s" cannot be packed: only scalar numeric, bool and enum elements can.', + $element::class, + )); + } + } #[\Override] public function accept(Visitor $visitor): mixed diff --git a/src/Type/Visitor/IsPacked.php b/src/Type/Visitor/IsPacked.php index 6c33993..8eff70c 100644 --- a/src/Type/Visitor/IsPacked.php +++ b/src/Type/Visitor/IsPacked.php @@ -5,29 +5,116 @@ namespace Thesis\Protobuf\Type\Visitor; use Thesis\Protobuf\Type; -use Thesis\Protobuf\Type\MessageT; -use Thesis\Protobuf\Type\StringT; +use Thesis\Protobuf\Type\BoolT; +use Thesis\Protobuf\Type\DoubleT; +use Thesis\Protobuf\Type\EnumT; +use Thesis\Protobuf\Type\Fixed32T; +use Thesis\Protobuf\Type\Fixed64T; +use Thesis\Protobuf\Type\FloatT; +use Thesis\Protobuf\Type\Int32T; +use Thesis\Protobuf\Type\Int64T; +use Thesis\Protobuf\Type\SFixed32T; +use Thesis\Protobuf\Type\SFixed64T; +use Thesis\Protobuf\Type\SInt32T; +use Thesis\Protobuf\Type\SInt64T; +use Thesis\Protobuf\Type\Uint32T; +use Thesis\Protobuf\Type\Uint64T; /** + * Whether repeated elements of the type can be packed: only scalar numeric, bool and enum types can. + * Any other type, including one added later, is not packable unless listed here. + * * @template-extends DefaultTypeVisitor */ final class IsPacked extends DefaultTypeVisitor { #[\Override] - public function string(StringT $type): bool + public function bool(BoolT $type): bool { - return false; + return true; } #[\Override] - public function message(MessageT $type): mixed + public function float(FloatT $type): bool { - return false; + return true; } #[\Override] - protected function default(Type $type): bool + public function double(DoubleT $type): bool + { + return true; + } + + #[\Override] + public function int32(Int32T $type): bool + { + return true; + } + + #[\Override] + public function uint32(Uint32T $type): bool + { + return true; + } + + #[\Override] + public function sint32(SInt32T $type): bool + { + return true; + } + + #[\Override] + public function int64(Int64T $type): bool + { + return true; + } + + #[\Override] + public function uint64(Uint64T $type): bool + { + return true; + } + + #[\Override] + public function sint64(SInt64T $type): bool + { + return true; + } + + #[\Override] + public function fixed32(Fixed32T $type): bool + { + return true; + } + + #[\Override] + public function sfixed32(SFixed32T $type): bool + { + return true; + } + + #[\Override] + public function fixed64(Fixed64T $type): bool { return true; } + + #[\Override] + public function sfixed64(SFixed64T $type): bool + { + return true; + } + + #[\Override] + public function enum(EnumT $type): bool + { + return true; + } + + #[\Override] + protected function default(Type $type): bool + { + return false; + } } diff --git a/src/Value.php b/src/Value.php index 808a72e..e2c3133 100644 --- a/src/Value.php +++ b/src/Value.php @@ -151,6 +151,7 @@ public static function message(Message $message): self * @param Type $type * @param list $values * @return self> + * @throws Exception\InvalidSchema if a non-packable element type is declared packed */ public static function listOf( Type $type, diff --git a/src/constructors.php b/src/constructors.php index 9952568..bc65779 100644 --- a/src/constructors.php +++ b/src/constructors.php @@ -243,6 +243,7 @@ function recursionT(\Closure $continuation): Type\RecursionT * @param Type $type * @param list $values * @return Value> + * @throws Exception\InvalidSchema if a non-packable element type is declared packed */ function listOf( Type $type, @@ -257,6 +258,7 @@ function listOf( * @template T * @param Type $element * @return Type\ListT + * @throws Exception\InvalidSchema if a non-packable element type is declared packed */ function listT(Type $element, ?bool $packed = null): Type\ListT { diff --git a/tests/PackedListTest.php b/tests/PackedListTest.php new file mode 100644 index 0000000..d17c101 --- /dev/null +++ b/tests/PackedListTest.php @@ -0,0 +1,138 @@ +expectException(Exception\InvalidSchema::class); + $this->expectExceptionMessageMatches('/cannot be packed/'); + + $create(); + } + + /** + * @return iterable + */ + public static function provideNonPackableElementsCannotBePackedCases(): iterable + { + yield 'string' => [static fn() => listOf(stringT, ['a', 'b'], true)]; + yield 'bytes' => [static fn() => listT(bytesT, true)]; + yield 'message' => [static fn() => listT(messageT(fieldT(1, int32T)), true)]; + yield 'recursion' => [static fn() => listT(recursionT(static fn() => messageT()), true)]; + // Not allowed by the phpdoc types either: checked at runtime for callers without static analysis. + yield 'list' => [static fn() => listT( + /** @phpstan-ignore argument.type */ + listT(int32T), + true, + )]; + yield 'map' => [static fn() => listT( + /** @phpstan-ignore argument.type */ + mapT(int32T, int32T), + true, + )]; + yield 'reflection string' => [static fn() => new Reflection\ListT(Reflection\StringT::T, packed: true)]; + yield 'reflection bytes' => [static fn() => new Reflection\ListT(Reflection\BytesT::T, packed: true)]; + yield 'reflection object' => [static fn() => new Reflection\ListT(new Reflection\ObjectT(\stdClass::class), packed: true)]; + yield 'reflection list' => [static fn() => new Reflection\ListT( + /** @phpstan-ignore argument.type */ + new Reflection\ListT(Reflection\Int32T::T), + packed: true, + )]; + yield 'reflection map' => [static fn() => new Reflection\ListT( + /** @phpstan-ignore argument.type */ + new Reflection\MapT(Reflection\Int32T::T, Reflection\Int32T::T), + packed: true, + )]; + } + + public function testNonPackableElementsCanBeDeclaredUnpacked(): void + { + self::assertSame( + '0a01610a0162', + bin2hex(new Serializer()->serialize(message(fieldOf(1, listOf(stringT, ['a', 'b'], false))))), + ); + self::assertFalse(new Reflection\ListT(Reflection\StringT::T, packed: false)->packed); + self::assertNull(listT(stringT)->packed); + } + + public function testPackedNonPackableAttributeFailsEncodingAndDecoding(): void + { + try { + Encoder\Builder::buildDefault()->encode(new PackedListTestStrings(['a'])); + self::fail('Encoding must fail.'); + } catch (Exception\InvalidSchema $e) { + self::assertStringContainsString('cannot be packed', $e->getMessage()); + } + + $this->expectException(Exception\InvalidSchema::class); + Decoder\Builder::buildDefault()->decode("\x0a\x01a", PackedListTestStrings::class); + } + + /** + * @param Type $type + * @param Reflection\Type $reflection + */ + #[DataProvider('provideEveryPackableTypeCanBePackedCases')] + public function testEveryPackableTypeCanBePacked(Type $type, Reflection\Type $reflection): void + { + self::assertTrue(listT($type, true)->packed); + self::assertTrue(new Reflection\ListT($reflection, packed: true)->packed); + } + + /** + * @return iterable, Reflection\Type}> + */ + public static function provideEveryPackableTypeCanBePackedCases(): iterable + { + yield 'bool' => [boolT, Reflection\BoolT::T]; + yield 'float' => [floatT, Reflection\FloatT::T]; + yield 'double' => [doubleT, Reflection\DoubleT::T]; + yield 'int32' => [int32T, Reflection\Int32T::T]; + yield 'uint32' => [uint32T, Reflection\Uint32T::T]; + yield 'sint32' => [sint32T, Reflection\SInt32T::T]; + yield 'int64' => [int64T, Reflection\Int64T::T]; + yield 'uint64' => [uint64T, Reflection\Uint64T::T]; + yield 'sint64' => [sint64T, Reflection\SInt64T::T]; + yield 'fixed32' => [fixed32T, Reflection\Fixed32T::T]; + yield 'sfixed32' => [sfixed32T, Reflection\SFixed32T::T]; + yield 'fixed64' => [fixed64T, Reflection\Fixed64T::T]; + yield 'sfixed64' => [sfixed64T, Reflection\SFixed64T::T]; + yield 'enum' => [enumT(WireType::class), new Reflection\EnumT(WireType::class)]; + } + + public function testPackableElementsCanBePacked(): void + { + self::assertSame( + '0a020102', + bin2hex(new Serializer()->serialize(message(fieldOf(1, listOf(int32T, [1, 2], true))))), + ); + self::assertTrue(new Reflection\ListT(new Reflection\EnumT(\Thesis\Protobuf\WireType::class), packed: true)->packed); + } +} + +final readonly class PackedListTestStrings +{ + /** + * @param list $values + */ + public function __construct( + #[Reflection\Field(1, new Reflection\ListT(Reflection\StringT::T, packed: true))] + public array $values = [], + ) {} +} From 42a0a832d3f4bf6af778b396cf14b9db3fe52c9f Mon Sep 17 00:00:00 2001 From: kafkiansky Date: Tue, 17 Nov 2026 10:20:52 +0300 Subject: [PATCH 2/2] refactor: rename IsPacked visitor to IsPackable --- src/Type/ListT.php | 4 ++-- src/Type/Visitor/{IsPacked.php => IsPackable.php} | 2 +- src/Type/Visitor/TypeDeserializerVisitor.php | 2 +- src/Type/Visitor/TypeSerializerVisitor.php | 2 +- tests/PackedListTest.php | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) rename src/Type/Visitor/{IsPacked.php => IsPackable.php} (97%) diff --git a/src/Type/ListT.php b/src/Type/ListT.php index 98e67ff..e2de5a4 100644 --- a/src/Type/ListT.php +++ b/src/Type/ListT.php @@ -6,7 +6,7 @@ use Thesis\Protobuf\Exception\InvalidSchema; use Thesis\Protobuf\Type; -use Thesis\Protobuf\Type\Visitor\IsPacked; +use Thesis\Protobuf\Type\Visitor\IsPackable; /** * @template T @@ -23,7 +23,7 @@ public function __construct( public ?bool $packed = null, ) { // A recursive message is not resolved here: the type it refers to may still be under construction. - if ($packed === true && ($element instanceof RecursionT || !$element->accept(new IsPacked()))) { + if ($packed === true && ($element instanceof RecursionT || !$element->accept(new IsPackable()))) { throw new InvalidSchema(\sprintf( 'Repeated fields of type "%s" cannot be packed: only scalar numeric, bool and enum elements can.', $element::class, diff --git a/src/Type/Visitor/IsPacked.php b/src/Type/Visitor/IsPackable.php similarity index 97% rename from src/Type/Visitor/IsPacked.php rename to src/Type/Visitor/IsPackable.php index 8eff70c..a978685 100644 --- a/src/Type/Visitor/IsPacked.php +++ b/src/Type/Visitor/IsPackable.php @@ -26,7 +26,7 @@ * * @template-extends DefaultTypeVisitor */ -final class IsPacked extends DefaultTypeVisitor +final class IsPackable extends DefaultTypeVisitor { #[\Override] public function bool(BoolT $type): bool diff --git a/src/Type/Visitor/TypeDeserializerVisitor.php b/src/Type/Visitor/TypeDeserializerVisitor.php index d7d1ec7..84dc60d 100644 --- a/src/Type/Visitor/TypeDeserializerVisitor.php +++ b/src/Type/Visitor/TypeDeserializerVisitor.php @@ -149,7 +149,7 @@ public function list(ListT $type): DeserializeValue /** @phpstan-ignore argument.type */ $type->element->accept($this), $this->tag, - $this->tag->type === WireType::BYTES && $type->element->accept(new IsPacked()), + $this->tag->type === WireType::BYTES && $type->element->accept(new IsPackable()), ); } diff --git a/src/Type/Visitor/TypeSerializerVisitor.php b/src/Type/Visitor/TypeSerializerVisitor.php index 62a45ad..e7dcc5b 100644 --- a/src/Type/Visitor/TypeSerializerVisitor.php +++ b/src/Type/Visitor/TypeSerializerVisitor.php @@ -157,7 +157,7 @@ public function list(ListT $type): SerializeValue ->accept($this) ->without(SerializeTag::class), $this->tag, - $type->packed ?? $type->element->accept(new IsPacked()), + $type->packed ?? $type->element->accept(new IsPackable()), ), ); } diff --git a/tests/PackedListTest.php b/tests/PackedListTest.php index d17c101..bd73796 100644 --- a/tests/PackedListTest.php +++ b/tests/PackedListTest.php @@ -10,7 +10,7 @@ #[CoversClass(Type\ListT::class)] #[CoversClass(Reflection\ListT::class)] -#[CoversClass(Type\Visitor\IsPacked::class)] +#[CoversClass(Type\Visitor\IsPackable::class)] #[CoversClass(Reflection\Internal\Visitor\IsPackable::class)] final class PackedListTest extends TestCase {