From 5bb945321234bf564107a6337ec800289b5dde57 Mon Sep 17 00:00:00 2001 From: kafkiansky Date: Sun, 22 Nov 2026 16:56:06 +0300 Subject: [PATCH 1/2] fix: resolve types re-exported through "import public" A file sees the types of the files it imports and of everything those files re-export through `import public`, at any depth. The dependency registry only looked at direct imports, so using a re-exported type failed with "No type '...' was found". The registry now follows `public_dependency` of every imported file transitively. A plain import of an imported file is still not re-exported, and files reached more than once are visited once. Co-Authored-By: Claude Fable 5.2 --- README.md | 1 + src/Plugin/Dependency/Registry.php | 39 ++- tests/CompilerImportPublicTest.php | 110 ++++++++ tests/GeneratedCode/samples/import_public.php | 41 +++ tests/Plugin/Dependency/RegistryTest.php | 239 ++++++++++++++++++ tests/fixtures/import_public/a.proto | 26 ++ tests/fixtures/import_public/b.proto | 6 + tests/fixtures/import_public/c.proto | 11 + tests/fixtures/import_public/d.proto | 8 + tests/fixtures/import_public/legacy.proto | 12 + tests/fixtures/import_public/ns.proto | 22 ++ tests/fixtures/import_public/reexport.proto | 14 + tests/snapshots/import_public/T/Pub/A/A.php | 48 ++++ .../T/Pub/A/DescriptorRegistry.php | 49 ++++ .../import_public/T/Pub/A/LookupClient.php | 79 ++++++ .../import_public/T/Pub/A/LookupServer.php | 30 +++ .../T/Pub/A/LookupServerRegistry.php | 44 ++++ tests/snapshots/import_public/T/Pub/C/C.php | 28 ++ .../T/Pub/C/DescriptorRegistry.php | 39 +++ tests/snapshots/import_public/T/Pub/D/D.php | 28 ++ .../T/Pub/D/DescriptorRegistry.php | 36 +++ .../T/Pub/Legacy/DescriptorRegistry.php | 39 +++ .../import_public/T/Pub/Legacy/Legacy.php | 28 ++ .../T/Pub/Re/DescriptorRegistry.php | 40 +++ .../import_public/T/Pub/Re/Wrapper.php | 32 +++ .../import_public/T/Pub/autoload.metadata.php | 18 ++ .../Test/ImportPublic/Custom/Box.php | 28 ++ .../Test/ImportPublic/Custom/Box/Item.php | 26 ++ .../Test/ImportPublic/Custom/Color.php | 23 ++ .../Custom/DescriptorRegistry.php | 40 +++ .../ImportPublic/Custom/autoload.metadata.php | 14 + 31 files changed, 1197 insertions(+), 1 deletion(-) create mode 100644 tests/CompilerImportPublicTest.php create mode 100644 tests/GeneratedCode/samples/import_public.php create mode 100644 tests/Plugin/Dependency/RegistryTest.php create mode 100644 tests/fixtures/import_public/a.proto create mode 100644 tests/fixtures/import_public/b.proto create mode 100644 tests/fixtures/import_public/c.proto create mode 100644 tests/fixtures/import_public/d.proto create mode 100644 tests/fixtures/import_public/legacy.proto create mode 100644 tests/fixtures/import_public/ns.proto create mode 100644 tests/fixtures/import_public/reexport.proto create mode 100644 tests/snapshots/import_public/T/Pub/A/A.php create mode 100644 tests/snapshots/import_public/T/Pub/A/DescriptorRegistry.php create mode 100644 tests/snapshots/import_public/T/Pub/A/LookupClient.php create mode 100644 tests/snapshots/import_public/T/Pub/A/LookupServer.php create mode 100644 tests/snapshots/import_public/T/Pub/A/LookupServerRegistry.php create mode 100644 tests/snapshots/import_public/T/Pub/C/C.php create mode 100644 tests/snapshots/import_public/T/Pub/C/DescriptorRegistry.php create mode 100644 tests/snapshots/import_public/T/Pub/D/D.php create mode 100644 tests/snapshots/import_public/T/Pub/D/DescriptorRegistry.php create mode 100644 tests/snapshots/import_public/T/Pub/Legacy/DescriptorRegistry.php create mode 100644 tests/snapshots/import_public/T/Pub/Legacy/Legacy.php create mode 100644 tests/snapshots/import_public/T/Pub/Re/DescriptorRegistry.php create mode 100644 tests/snapshots/import_public/T/Pub/Re/Wrapper.php create mode 100644 tests/snapshots/import_public/T/Pub/autoload.metadata.php create mode 100644 tests/snapshots/import_public/Test/ImportPublic/Custom/Box.php create mode 100644 tests/snapshots/import_public/Test/ImportPublic/Custom/Box/Item.php create mode 100644 tests/snapshots/import_public/Test/ImportPublic/Custom/Color.php create mode 100644 tests/snapshots/import_public/Test/ImportPublic/Custom/DescriptorRegistry.php create mode 100644 tests/snapshots/import_public/Test/ImportPublic/Custom/autoload.metadata.php diff --git a/README.md b/README.md index bec75c0..02c5a9f 100644 --- a/README.md +++ b/README.md @@ -779,6 +779,7 @@ To avoid having to generate these types each time, it is recommended to use libr - [ ] `extensions` - [ ] `groups` - [x] `nested types` +- [x] `import public` - [x] `packed repeated` - [x] `oneof` - [x] `grpc` diff --git a/src/Plugin/Dependency/Registry.php b/src/Plugin/Dependency/Registry.php index 0fef475..d0048c9 100644 --- a/src/Plugin/Dependency/Registry.php +++ b/src/Plugin/Dependency/Registry.php @@ -60,9 +60,16 @@ private function createIndex(Parser\Request $request, NamespaceResolver $namespa private function mergeIndex(Parser\Request $request): void { foreach ($request as $name => $descriptor) { + $visible = [$name]; + $seen = [$name => true]; + + foreach ($descriptor->dependencies as $dep) { + $this->collectExported($request, $dep, $visible, $seen); + } + $indexes = []; - foreach ([$name, ...$descriptor->dependencies] as $dep) { + foreach ($visible as $dep) { if (isset($this->index[$dep])) { $indexes = [ ...$indexes, @@ -75,6 +82,36 @@ private function mergeIndex(Parser\Request $request): void } } + /** + * Collects an imported file along with everything it re-exports through "import public", + * at any depth. A plain import of the imported file is not re-exported. + * + * @param list $visible + * @param array $seen + * @param-out list $visible + */ + private function collectExported(Parser\Request $request, string $file, array &$visible, array &$seen): void + { + if (isset($seen[$file])) { + return; + } + + $seen[$file] = true; + $visible[] = $file; + + $descriptor = $request->descriptors[$file] ?? null; + + if ($descriptor === null) { + return; + } + + foreach ($descriptor->file->publicDependency as $idx) { + if (isset($descriptor->dependencies[$idx])) { + $this->collectExported($request, $descriptor->dependencies[$idx], $visible, $seen); + } + } + } + private function createDependencyGraph(Parser\Request $request): void { foreach ($request->descriptors as $name => $proto) { diff --git a/tests/CompilerImportPublicTest.php b/tests/CompilerImportPublicTest.php new file mode 100644 index 0000000..15cfbef --- /dev/null +++ b/tests/CompilerImportPublicTest.php @@ -0,0 +1,110 @@ + $expected generated files, autoload.metadata.php aside (it is shared by the whole case) + */ + #[DataProvider('provideOnlyImporterIsGeneratedCases')] + public function testOnlyImporterIsGenerated(string $file, array $expected): void + { + $request = self::request(); + + self::assertContains($file, $request->fileToGenerate); + + $request = new CodeGeneratorRequest( + fileToGenerate: [$file], + parameter: $request->parameter, + protoFile: $request->protoFile, + compilerVersion: $request->compilerVersion, + ); + + $generated = []; + + foreach (new Compiler(Encoder\Builder::buildDefault(), __DIR__ . '/../map.json')->compile($request) as $out) { + self::assertNotNull($out->name); + self::assertNotNull($out->content); + + if (basename($out->name) !== 'autoload.metadata.php') { + $generated[$out->name] = $out->content; + } + } + + ksort($generated); + + self::assertSame($expected, array_keys($generated)); + + foreach ($generated as $name => $content) { + $snapshot = file_get_contents(__DIR__ . '/snapshots/' . self::CASE . "/{$name}"); + self::assertIsString($snapshot); + self::assertSame(self::normalize($snapshot), self::normalize($content), $name); + } + } + + /** + * @return iterable}> + */ + public static function provideOnlyImporterIsGeneratedCases(): iterable + { + yield 'messages, maps and a service with re-exported types' => ['a.proto', [ + 'T/Pub/A/A.php', + 'T/Pub/A/DescriptorRegistry.php', + 'T/Pub/A/LookupClient.php', + 'T/Pub/A/LookupServer.php', + 'T/Pub/A/LookupServerRegistry.php', + ]]; + + yield 'enum defaults with a re-exported enum' => ['legacy.proto', [ + 'T/Pub/Legacy/DescriptorRegistry.php', + 'T/Pub/Legacy/Legacy.php', + ]]; + + yield 'file using the types it re-exports' => ['reexport.proto', [ + 'T/Pub/Re/DescriptorRegistry.php', + 'T/Pub/Re/Wrapper.php', + ]]; + } + + private static function request(): CodeGeneratorRequest + { + $root = getenv('TESTDATA_DIR'); + $root = \is_string($root) && $root !== '' ? $root : \dirname(__DIR__) . '/var/testdata'; + + $hex = file_get_contents($root . '/' . self::CASE . '/request.hex'); + self::assertIsString($hex, 'No generated testdata found. Run `make test` or `make generate-testdata` first.'); + + $bytes = hex2bin($hex); + self::assertIsString($bytes); + + return Decoder\Builder::buildDefault()->decode($bytes, CodeGeneratorRequest::class); + } + + private static function normalize(string $content): string + { + return (string) preg_replace('/thesis\/protoc-plugin — v[^\n]+/', 'thesis/protoc-plugin — v', $content); + } +} diff --git a/tests/GeneratedCode/samples/import_public.php b/tests/GeneratedCode/samples/import_public.php new file mode 100644 index 0000000..35042b2 --- /dev/null +++ b/tests/GeneratedCode/samples/import_public.php @@ -0,0 +1,41 @@ + new A( + c: new C('c', new D('nested')), + d: new D('d'), + box: new Box(new Box\Item('in box'), Color::COLOR_RED), + item: new Box\Item('item'), + color: Color::COLOR_GREEN, + byName: Map::fromArray(['first' => new D('1'), 'second' => new D('2')]), + colors: Map::fromArray([1 => Color::COLOR_RED, 2 => Color::COLOR_GREEN]), + list: [new C('x'), new C('y', new D('z'))], + maybeColor: Color::COLOR_UNSPECIFIED, + ), + 're-exporting file using the re-exported types' => new Wrapper( + box: new Box(new Box\Item('wrapped')), + color: Color::COLOR_RED, + c: new C('c'), + d: new D('d'), + ), + 'enum defaults of a re-exported enum' => new Legacy( + color: Color::COLOR_RED, + requiredColor: Color::COLOR_GREEN, + ), +]; diff --git a/tests/Plugin/Dependency/RegistryTest.php b/tests/Plugin/Dependency/RegistryTest.php new file mode 100644 index 0000000..88e4e7e --- /dev/null +++ b/tests/Plugin/Dependency/RegistryTest.php @@ -0,0 +1,239 @@ + $files + * @param array $expected type name => fqcn + */ + #[DataProvider('provideVisibleTypesCases')] + public function testVisibleTypes(array $files, array $expected): void + { + foreach ([$files, array_reverse($files)] as $order) { + $graph = self::registry($order)->graph('a.proto'); + + foreach ($expected as $typeName => $fqcn) { + self::assertSame($fqcn, $graph->get($typeName)->fqcn); + } + } + } + + /** + * @return iterable, array}> + */ + public static function provideVisibleTypesCases(): iterable + { + // a.proto: import "b.proto"; message A { t.c.C c = 1; t.d.D d = 2; } + // b.proto: import public "c.proto"; + // c.proto: import public "d.proto"; message C {} + // d.proto: message D {} + yield 'public imports at any depth' => [ + [ + self::file('a.proto', 't.a', ['b.proto'], messages: [self::message('A', ['.t.c.C', '.t.d.D'])]), + self::file('b.proto', 't.b', ['c.proto'], public: [0]), + self::file('c.proto', 't.c', ['d.proto'], public: [0], messages: [self::message('C')]), + self::file('d.proto', 't.d', messages: [self::message('D')]), + ], + ['.t.c.C' => '\T\C\C', '.t.d.D' => '\T\D\D'], + ]; + + // a.proto: import "b.proto"; import "e.proto"; message A { t.c.C c = 1; t.d.D d = 2; } + // b.proto, e.proto: import public "c.proto"; + // c.proto: import public "d.proto"; message C {} + yield 'diamond' => [ + [ + self::file('a.proto', 't.a', ['b.proto', 'e.proto'], messages: [self::message('A', ['.t.c.C', '.t.d.D'])]), + self::file('b.proto', 't.b', ['c.proto'], public: [0]), + self::file('e.proto', 't.e', ['c.proto'], public: [0]), + self::file('c.proto', 't.c', ['d.proto'], public: [0], messages: [self::message('C')]), + self::file('d.proto', 't.d', messages: [self::message('D')]), + ], + ['.t.c.C' => '\T\C\C', '.t.d.D' => '\T\D\D'], + ]; + + // protoc rejects cyclic imports, the registry still must not loop on them. + yield 'cycle of public imports' => [ + [ + self::file('a.proto', 't.a', ['b.proto'], messages: [self::message('A', ['.t.b.B', '.t.c.C'])]), + self::file('b.proto', 't.b', ['c.proto'], public: [0], messages: [self::message('B')]), + self::file('c.proto', 't.c', ['b.proto'], public: [0], messages: [self::message('C')]), + ], + ['.t.b.B' => '\T\B\B', '.t.c.C' => '\T\C\C'], + ]; + + // A type keeps the namespace of the file that declares it, not of the re-exporting one. + // b.proto: option php_namespace = "Via\\B"; import public "c.proto"; + // c.proto: option php_namespace = "Custom\\C"; message C { message Inner {} } + yield 'php_namespace of the declaring file' => [ + [ + self::file('a.proto', 't.a', ['b.proto'], messages: [self::message('A', ['.t.c.C', '.t.c.C.Inner'])]), + self::file('b.proto', 't.b', ['c.proto'], public: [0], phpNamespace: 'Via\B'), + self::file('c.proto', 't.c', phpNamespace: 'Custom\C', messages: [ + new DescriptorProto(name: 'C', nestedType: [new DescriptorProto(name: 'Inner')]), + ]), + ], + ['.t.c.C' => '\Custom\C\C', '.t.c.C.Inner' => '\Custom\C\C\Inner'], + ]; + + // A re-exported type relocated by map.json. + // b.proto: import public "google/type/date.proto"; + yield 'type relocated by map.json' => [ + [ + self::file('a.proto', 't.a', ['b.proto'], messages: [self::message('A', ['.google.type.Date'])]), + self::file('b.proto', 't.b', ['google/type/date.proto'], public: [0]), + self::file('google/type/date.proto', 'google.type', messages: [self::message('Date')]), + ], + ['.google.type.Date' => '\Thesis\Google\Type\Date'], + ]; + + // Only the public one of the imports of b is re-exported. + // b.proto: import "x.proto"; import public "c.proto"; + yield 'public index among plain imports' => [ + [ + self::file('a.proto', 't.a', ['b.proto'], messages: [self::message('A', ['.t.c.C'])]), + self::file('b.proto', 't.b', ['x.proto', 'c.proto'], public: [1]), + self::file('x.proto', 't.x', messages: [self::message('X')]), + self::file('c.proto', 't.c', messages: [self::message('C')]), + ], + ['.t.c.C' => '\T\C\C'], + ]; + } + + /** + * @param list $files + */ + #[DataProvider('provideInvisibleTypesCases')] + public function testInvisibleTypes(array $files, string $typeName): void + { + $graph = self::registry($files)->graph('a.proto'); + + $this->expectException(\RuntimeException::class); + $this->expectExceptionMessage("No type '{$typeName}' was found."); + + $graph->get($typeName); + } + + /** + * protoc rejects these schemas itself, the registry must not be wider than the import semantics either. + * + * @return iterable, string}> + */ + public static function provideInvisibleTypesCases(): iterable + { + // b.proto: import "c.proto"; + yield 'plain import is not re-exported' => [ + [ + self::file('a.proto', 't.a', ['b.proto'], messages: [self::message('A', ['.t.c.C'])]), + self::file('b.proto', 't.b', ['c.proto']), + self::file('c.proto', 't.c', messages: [self::message('C')]), + ], + '.t.c.C', + ]; + + // b.proto: import public "c.proto"; c.proto: import "d.proto"; + yield 'plain import behind a public one is not re-exported' => [ + [ + self::file('a.proto', 't.a', ['b.proto'], messages: [self::message('A', ['.t.d.D'])]), + self::file('b.proto', 't.b', ['c.proto'], public: [0]), + self::file('c.proto', 't.c', ['d.proto']), + self::file('d.proto', 't.d', messages: [self::message('D')]), + ], + '.t.d.D', + ]; + + // b.proto: import "x.proto"; import public "c.proto"; + yield 'plain import next to a public one is not re-exported' => [ + [ + self::file('a.proto', 't.a', ['b.proto'], messages: [self::message('A', ['.t.x.X'])]), + self::file('b.proto', 't.b', ['x.proto', 'c.proto'], public: [1]), + self::file('x.proto', 't.x', messages: [self::message('X')]), + self::file('c.proto', 't.c', messages: [self::message('C')]), + ], + '.t.x.X', + ]; + } + + /** + * @param list $files + */ + private static function registry(array $files): Registry + { + $request = new CodeGeneratorRequest(fileToGenerate: ['a.proto'], protoFile: $files); + + return new Registry( + new Parser()->parse($request), + new NamespaceResolver( + TypeMap::fromFile(__DIR__ . '/../../../map.json'), + CompilerOptions::fromRequest($request), + ), + ); + } + + /** + * @param list $dependencies + * @param list $public + * @param list $messages + */ + private static function file( + string $name, + string $package, + array $dependencies = [], + array $public = [], + ?string $phpNamespace = null, + array $messages = [], + ): FileDescriptorProto { + return new FileDescriptorProto( + name: $name, + package: $package, + dependency: $dependencies, + messageType: $messages, + publicDependency: $public, + options: $phpNamespace === null ? null : new FileOptions(phpNamespace: $phpNamespace), + syntax: 'proto3', + ); + } + + /** + * @param list $fieldTypes type names of the message fields + */ + private static function message(string $name, array $fieldTypes = []): DescriptorProto + { + $fields = []; + + foreach ($fieldTypes as $idx => $typeName) { + $fields[] = new FieldDescriptorProto( + name: "f{$idx}", + number: $idx + 1, + label: FieldDescriptorProto\Label::LABEL_OPTIONAL, + type: FieldDescriptorProto\Type::TYPE_MESSAGE, + typeName: $typeName, + jsonName: "f{$idx}", + ); + } + + return new DescriptorProto(name: $name, field: $fields); + } +} diff --git a/tests/fixtures/import_public/a.proto b/tests/fixtures/import_public/a.proto new file mode 100644 index 0000000..3442331 --- /dev/null +++ b/tests/fixtures/import_public/a.proto @@ -0,0 +1,26 @@ +syntax = "proto3"; + +package t.pub.a; + +// Neither import is public, and a uses no type of b or reexport itself: +// every type below is visible only through "import public". +// c and d are reached twice (via b and via reexport), a diamond. +import "b.proto"; +import "reexport.proto"; + +message A { + t.pub.c.C c = 1; + t.pub.d.D d = 2; + t.pub.ns.Box box = 3; + t.pub.ns.Box.Item item = 4; + t.pub.ns.Color color = 5; + map by_name = 6; + map colors = 7; + repeated t.pub.c.C list = 8; + optional t.pub.ns.Color maybe_color = 9; +} + +service Lookup { + rpc Get(t.pub.c.C) returns (t.pub.d.D); + rpc Pack(t.pub.ns.Box.Item) returns (t.pub.ns.Box); +} diff --git a/tests/fixtures/import_public/b.proto b/tests/fixtures/import_public/b.proto new file mode 100644 index 0000000..adc9810 --- /dev/null +++ b/tests/fixtures/import_public/b.proto @@ -0,0 +1,6 @@ +syntax = "proto3"; + +package t.pub.b; + +// A file that declares nothing and only re-exports c (and, through c, d). +import public "c.proto"; diff --git a/tests/fixtures/import_public/c.proto b/tests/fixtures/import_public/c.proto new file mode 100644 index 0000000..ead37a5 --- /dev/null +++ b/tests/fixtures/import_public/c.proto @@ -0,0 +1,11 @@ +syntax = "proto3"; + +package t.pub.c; + +// Re-exported to the importers of c, and through b to the importers of b. +import public "d.proto"; + +message C { + string a = 1; + t.pub.d.D d = 2; +} diff --git a/tests/fixtures/import_public/d.proto b/tests/fixtures/import_public/d.proto new file mode 100644 index 0000000..85fba47 --- /dev/null +++ b/tests/fixtures/import_public/d.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; + +package t.pub.d; + +// The end of the chain a -> b =public=> c =public=> d. +message D { + string value = 1; +} diff --git a/tests/fixtures/import_public/legacy.proto b/tests/fixtures/import_public/legacy.proto new file mode 100644 index 0000000..bdb9591 --- /dev/null +++ b/tests/fixtures/import_public/legacy.proto @@ -0,0 +1,12 @@ +syntax = "proto2"; + +package t.pub.legacy; + +// A proto2 importer: the default of an enum field names a case of an enum +// visible only through "import public". +import "reexport.proto"; + +message Legacy { + optional t.pub.ns.Color color = 1 [default = COLOR_GREEN]; + required t.pub.ns.Color required_color = 2 [default = COLOR_RED]; +} diff --git a/tests/fixtures/import_public/ns.proto b/tests/fixtures/import_public/ns.proto new file mode 100644 index 0000000..18e20a8 --- /dev/null +++ b/tests/fixtures/import_public/ns.proto @@ -0,0 +1,22 @@ +syntax = "proto3"; + +package t.pub.ns; + +// Re-exported types live in the namespace of the file that declares them, +// not in the one of the file that re-exports them. +option php_namespace = "Test\\ImportPublic\\Custom"; + +enum Color { + COLOR_UNSPECIFIED = 0; + COLOR_RED = 1; + COLOR_GREEN = 2; +} + +message Box { + message Item { + string name = 1; + } + + Item item = 1; + Color color = 2; +} diff --git a/tests/fixtures/import_public/reexport.proto b/tests/fixtures/import_public/reexport.proto new file mode 100644 index 0000000..9328262 --- /dev/null +++ b/tests/fixtures/import_public/reexport.proto @@ -0,0 +1,14 @@ +syntax = "proto3"; + +package t.pub.re; + +// A file that re-exports ns and c and also uses their types itself. +import public "ns.proto"; +import public "c.proto"; + +message Wrapper { + t.pub.ns.Box box = 1; + t.pub.ns.Color color = 2; + t.pub.c.C c = 3; + t.pub.d.D d = 4; +} diff --git a/tests/snapshots/import_public/T/Pub/A/A.php b/tests/snapshots/import_public/T/Pub/A/A.php new file mode 100644 index 0000000..0a2251e --- /dev/null +++ b/tests/snapshots/import_public/T/Pub/A/A.php @@ -0,0 +1,48 @@ + $byName + * @param Protobuf\Map $colors + * @param list<\T\Pub\C\C> $list + */ + public function __construct( + #[Reflection\Field(1, new Reflection\ObjectT(\T\Pub\C\C::class))] + public ?\T\Pub\C\C $c = null, + #[Reflection\Field(2, new Reflection\ObjectT(\T\Pub\D\D::class))] + public ?\T\Pub\D\D $d = null, + #[Reflection\Field(3, new Reflection\ObjectT(\Test\ImportPublic\Custom\Box::class))] + public ?\Test\ImportPublic\Custom\Box $box = null, + #[Reflection\Field(4, new Reflection\ObjectT(\Test\ImportPublic\Custom\Box\Item::class))] + public ?\Test\ImportPublic\Custom\Box\Item $item = null, + #[Reflection\Field(5, new Reflection\EnumT(\Test\ImportPublic\Custom\Color::class))] + public \Test\ImportPublic\Custom\Color $color = \Test\ImportPublic\Custom\Color::COLOR_UNSPECIFIED, + #[Reflection\Field(6, new Reflection\MapT(Reflection\StringT::T, new Reflection\ObjectT(\T\Pub\D\D::class)))] + public Protobuf\Map $byName = new Protobuf\Map(), + #[Reflection\Field(7, new Reflection\MapT(Reflection\Int32T::T, new Reflection\EnumT(\Test\ImportPublic\Custom\Color::class)))] + public Protobuf\Map $colors = new Protobuf\Map(), + #[Reflection\Field(8, new Reflection\ListT(new Reflection\ObjectT(\T\Pub\C\C::class)))] + public array $list = [], + #[Reflection\Field(9, new Reflection\EnumT(\Test\ImportPublic\Custom\Color::class))] + public ?\Test\ImportPublic\Custom\Color $maybeColor = null, + ) {} +} diff --git a/tests/snapshots/import_public/T/Pub/A/DescriptorRegistry.php b/tests/snapshots/import_public/T/Pub/A/DescriptorRegistry.php new file mode 100644 index 0000000..c52fe65 --- /dev/null +++ b/tests/snapshots/import_public/T/Pub/A/DescriptorRegistry.php @@ -0,0 +1,49 @@ +add(Registry\Descriptor::base64(self::A_DESCRIPTOR_BUFFER), new File( + name: 'a.proto', + dependencies: [ + 'b.proto', + 'reexport.proto', + ], + messages: [ + new File\MessageDescriptor('t.pub.a.A', \T\Pub\A\A::class), + ], + services: [ + new File\ServiceDescriptor( + name: 't.pub.a.Lookup', + methods: [ + new File\MethodDescriptor('Get', false, false), + new File\MethodDescriptor('Pack', false, false), + ], + ), + ], + )); + } +} diff --git a/tests/snapshots/import_public/T/Pub/A/LookupClient.php b/tests/snapshots/import_public/T/Pub/A/LookupClient.php new file mode 100644 index 0000000..83bee04 --- /dev/null +++ b/tests/snapshots/import_public/T/Pub/A/LookupClient.php @@ -0,0 +1,79 @@ + $invoke */ + $invoke = new Client\Invoke( + method: '/t.pub.a.Lookup/Get', + output: \T\Pub\D\D::class, + type: Grpc\RpcType::Unary, + ); + + return $this->client->invoke( + request: $request, + invoke: $invoke, + md: $md, + cancellation: $cancellation, + ); + } + + /** + * @throws ClientStreamIsClosed + * @throws InvokeError + */ + public function pack( + \Test\ImportPublic\Custom\Box\Item $request, + Metadata $md = new Metadata(), + Cancellation $cancellation = new NullCancellation(), + ): \Test\ImportPublic\Custom\Box { + /** @var Client\Invoke<\Test\ImportPublic\Custom\Box\Item, \Test\ImportPublic\Custom\Box> $invoke */ + $invoke = new Client\Invoke( + method: '/t.pub.a.Lookup/Pack', + output: \Test\ImportPublic\Custom\Box::class, + type: Grpc\RpcType::Unary, + ); + + return $this->client->invoke( + request: $request, + invoke: $invoke, + md: $md, + cancellation: $cancellation, + ); + } +} diff --git a/tests/snapshots/import_public/T/Pub/A/LookupServer.php b/tests/snapshots/import_public/T/Pub/A/LookupServer.php new file mode 100644 index 0000000..feac3d2 --- /dev/null +++ b/tests/snapshots/import_public/T/Pub/A/LookupServer.php @@ -0,0 +1,30 @@ +server->get(...)), + type: Grpc\RpcType::Unary, + ), + new Server\Rpc( + handle: new Server\Handle('Pack', \Test\ImportPublic\Custom\Box\Item::class), + handler: new Server\UnaryHandler($this->server->pack(...)), + type: Grpc\RpcType::Unary, + ), + ]); + } +} diff --git a/tests/snapshots/import_public/T/Pub/C/C.php b/tests/snapshots/import_public/T/Pub/C/C.php new file mode 100644 index 0000000..9c0315c --- /dev/null +++ b/tests/snapshots/import_public/T/Pub/C/C.php @@ -0,0 +1,28 @@ +add(Registry\Descriptor::base64(self::C_DESCRIPTOR_BUFFER), new File( + name: 'c.proto', + dependencies: [ + 'd.proto', + ], + messages: [ + new File\MessageDescriptor('t.pub.c.C', \T\Pub\C\C::class), + ], + )); + } +} diff --git a/tests/snapshots/import_public/T/Pub/D/D.php b/tests/snapshots/import_public/T/Pub/D/D.php new file mode 100644 index 0000000..d60100a --- /dev/null +++ b/tests/snapshots/import_public/T/Pub/D/D.php @@ -0,0 +1,28 @@ + b =public=> c =public=> d. + */ +final readonly class D +{ + public function __construct( + #[Reflection\Field(1, Reflection\StringT::T)] + public string $value = '', + ) {} +} diff --git a/tests/snapshots/import_public/T/Pub/D/DescriptorRegistry.php b/tests/snapshots/import_public/T/Pub/D/DescriptorRegistry.php new file mode 100644 index 0000000..54594a0 --- /dev/null +++ b/tests/snapshots/import_public/T/Pub/D/DescriptorRegistry.php @@ -0,0 +1,36 @@ +add(Registry\Descriptor::base64(self::D_DESCRIPTOR_BUFFER), new File( + name: 'd.proto', + messages: [ + new File\MessageDescriptor('t.pub.d.D', \T\Pub\D\D::class), + ], + )); + } +} diff --git a/tests/snapshots/import_public/T/Pub/Legacy/DescriptorRegistry.php b/tests/snapshots/import_public/T/Pub/Legacy/DescriptorRegistry.php new file mode 100644 index 0000000..26d1832 --- /dev/null +++ b/tests/snapshots/import_public/T/Pub/Legacy/DescriptorRegistry.php @@ -0,0 +1,39 @@ +add(Registry\Descriptor::base64(self::LEGACY_DESCRIPTOR_BUFFER), new File( + name: 'legacy.proto', + dependencies: [ + 'reexport.proto', + ], + messages: [ + new File\MessageDescriptor('t.pub.legacy.Legacy', \T\Pub\Legacy\Legacy::class), + ], + )); + } +} diff --git a/tests/snapshots/import_public/T/Pub/Legacy/Legacy.php b/tests/snapshots/import_public/T/Pub/Legacy/Legacy.php new file mode 100644 index 0000000..c994917 --- /dev/null +++ b/tests/snapshots/import_public/T/Pub/Legacy/Legacy.php @@ -0,0 +1,28 @@ +add(Registry\Descriptor::base64(self::REEXPORT_DESCRIPTOR_BUFFER), new File( + name: 'reexport.proto', + dependencies: [ + 'ns.proto', + 'c.proto', + ], + messages: [ + new File\MessageDescriptor('t.pub.re.Wrapper', \T\Pub\Re\Wrapper::class), + ], + )); + } +} diff --git a/tests/snapshots/import_public/T/Pub/Re/Wrapper.php b/tests/snapshots/import_public/T/Pub/Re/Wrapper.php new file mode 100644 index 0000000..b4832b8 --- /dev/null +++ b/tests/snapshots/import_public/T/Pub/Re/Wrapper.php @@ -0,0 +1,32 @@ +register( + new \Thesis\Protobuf\Registry\OnceRegistrar(new \T\Pub\A\DescriptorRegistry()), + new \Thesis\Protobuf\Registry\OnceRegistrar(new \T\Pub\C\DescriptorRegistry()), + new \Thesis\Protobuf\Registry\OnceRegistrar(new \T\Pub\D\DescriptorRegistry()), + new \Thesis\Protobuf\Registry\OnceRegistrar(new \T\Pub\Legacy\DescriptorRegistry()), + new \Thesis\Protobuf\Registry\OnceRegistrar(new \T\Pub\Re\DescriptorRegistry()), +); diff --git a/tests/snapshots/import_public/Test/ImportPublic/Custom/Box.php b/tests/snapshots/import_public/Test/ImportPublic/Custom/Box.php new file mode 100644 index 0000000..d67dc8d --- /dev/null +++ b/tests/snapshots/import_public/Test/ImportPublic/Custom/Box.php @@ -0,0 +1,28 @@ +add(Registry\Descriptor::base64(self::NS_DESCRIPTOR_BUFFER), new File( + name: 'ns.proto', + messages: [ + new File\MessageDescriptor('t.pub.ns.Box', \Test\ImportPublic\Custom\Box::class), + new File\MessageDescriptor('t.pub.ns.Box.Item', \Test\ImportPublic\Custom\Box\Item::class), + ], + enums: [ + new File\EnumDescriptor('t.pub.ns.Color', \Test\ImportPublic\Custom\Color::class), + ], + )); + } +} diff --git a/tests/snapshots/import_public/Test/ImportPublic/Custom/autoload.metadata.php b/tests/snapshots/import_public/Test/ImportPublic/Custom/autoload.metadata.php new file mode 100644 index 0000000..202f359 --- /dev/null +++ b/tests/snapshots/import_public/Test/ImportPublic/Custom/autoload.metadata.php @@ -0,0 +1,14 @@ +register( + new \Thesis\Protobuf\Registry\OnceRegistrar(new \Test\ImportPublic\Custom\DescriptorRegistry()), +); From 354c0a976bab5b0cebc554c3d9d39b9a2af5d983 Mon Sep 17 00:00:00 2001 From: kafkiansky Date: Sun, 22 Nov 2026 17:02:48 +0300 Subject: [PATCH 2/2] chore: require thesis/protobuf ^0.2.2 thesis/protobuf 0.2.0 writes expanded repeated fields packed and cannot read them back. A file with `import public` has such a field in its descriptor (public_dependency), so with the lowest dependencies the plugin embedded a broken descriptor into DescriptorRegistry and the generated code failed to decode it. Co-Authored-By: Claude Fable 5.2 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 6207bb4..2bb657f 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ "composer-runtime-api": "^2.2", "nette/php-generator": "^4.2.1", "thesis/package-version": "^0.1.2", - "thesis/protobuf": "^0.2", + "thesis/protobuf": "^0.2.2", "thesis/protobuf-compiler-types": "^0.3", "thesis/protobuf-descriptor-types": "^0.3", "thesis/protoregistry": "^0.1.2"