protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

Undefined Index Notice - PHP

Open tbillington opened this issue 9 years ago • 29 comments

I'm getting PHP notices for a lot of my types, not sure why.

PHP Notice:  Undefined index: .foo.bar.baz in /Users/trent/src/github.com/sajari/phptest/vendor/google/protobuf/php/src/Google/Protobuf/Internal/DescriptorPool.php on line 125

tbillington avatar Feb 21 '17 04:02 tbillington

which version are u using? On Mon, Feb 20, 2017 at 20:26 tbillington [email protected] wrote:

I'm getting PHP notices for a lot of my types, not sure why.

PHP Notice: Undefined index: .foo.bar.baz in /Users/trent/src/github.com/sajari/phptest/vendor/google/protobuf/php/src/Google/Protobuf/Internal/DescriptorPool.php on line 125

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/google/protobuf/issues/2743, or mute the thread https://github.com/notifications/unsubscribe-auth/AE9H5Zi0h8uQQw7dzS4N8sbUrr4GaVbzks5remdjgaJpZM4MG3d8 .

TeBoring avatar Feb 21 '17 04:02 TeBoring

php grpc library: "grpc/grpc": "v1.1.2" protoc: libprotoc 3.2.0

Just got the grpc_php_plugin from master a few hours ago.

tbillington avatar Feb 21 '17 05:02 tbillington

Have you used protoc to generate code? On Mon, Feb 20, 2017 at 21:08 tbillington [email protected] wrote:

php grpc library: "grpc/grpc": "v1.1.2" protoc: libprotoc 3.2.0

Just got the grpc_php_plugin from master a few hours ago.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/google/protobuf/issues/2743#issuecomment-281246783, or mute the thread https://github.com/notifications/unsubscribe-auth/AE9H5WurnUtMbOwdFoePG5D_qXoMwg-rks5renFWgaJpZM4MG3d8 .

TeBoring avatar Feb 21 '17 05:02 TeBoring

Yup

protoc -I$PROTO_SRC --php_out=$dest --grpc_out=$dest --plugin=protoc-gen-grpc=/Users/trent/grpc/bins/opt/grpc_php_plugin $dir/*.proto

tbillington avatar Feb 21 '17 05:02 tbillington

What's the protoc version? And did you autoload your generated proto files? On Mon, Feb 20, 2017 at 21:50 tbillington [email protected] wrote:

Yup

protoc -I$PROTO_SRC --php_out=$dest --grpc_out=$dest --plugin=protoc-gen-grpc=/Users/trent/grpc/bins/opt/grpc_php_plugin $dir/*.proto

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/google/protobuf/issues/2743#issuecomment-281251578, or mute the thread https://github.com/notifications/unsubscribe-auth/AE9H5S3iv8XgEubbIUNVJilJpE7srXwFks5rensTgaJpZM4MG3d8 .

TeBoring avatar Feb 21 '17 05:02 TeBoring

Yes, I autoloaded. I posted just before your previous comment the protoc version (maybe harder to see in email): libprotoc 3.2.0

tbillington avatar Feb 21 '17 05:02 tbillington

Can I see your proto file and test code?

TeBoring avatar Feb 21 '17 18:02 TeBoring

Seems this is only happening on enums.

tbillington avatar Feb 21 '17 23:02 tbillington

Maybe it can be fixed in you have a non-empty namespace. Let me know if it works.

TeBoring avatar Feb 21 '17 23:02 TeBoring

Still had the issue in a non-empty namespace.

tbillington avatar Feb 22 '17 00:02 tbillington

Here is some test code that demonstrates the issue. I removed the namespace because it had no impact.

Proto File:

syntax = "proto3";

package simpletest;

service SimpleTest {
  rpc Test (TestRequest) returns (TestResponse);
}

message TestRequest {
  enum TestEnum {
    ZERO = 0;
    ONE = 1;
  }

  string name = 1;

  TestEnum test_enum = 2;
}

message TestResponse {
  string reply = 1;
}

Test Code:

<?php

require  __DIR__ . "/vendor/autoload.php";

include_once  __DIR__ . "/proto/SimpleTest/SimpleTestClient.php";
include_once  __DIR__ . "/proto/SimpleTest/TestRequest.php";
include_once  __DIR__ . "/proto/SimpleTest/TestRequest_TestEnum.php";
include_once  __DIR__ . "/proto/SimpleTest/TestResponse.php";
include_once  __DIR__ . "/proto/GPBMetadata/Messages.php";

$client = new SimpleTest\SimpleTestClient("localhost:1111", ['credentials' => Grpc\ChannelCredentials::createInsecure()]);

$req = new SimpleTest\TestRequest();
$req->setName("Foo");
$req->setTestEnum(SimpleTest\TestRequest_TestEnum::ONE);

try {
    list($res, $status) = $client->Test($req)->wait();
    printf("%s\n", $res->getReply());
} catch (Exception $e) {
    printf("%s\n", $e);
}

Output:

PHP Notice:  Undefined index: .simpletest.TestRequest.TestEnum in /Users/trent/src/github.com/sajari/proto-php-example/vendor/google/protobuf/php/src/Google/Protobuf/Internal/DescriptorPool.php on line 125

Notice: Undefined index: .simpletest.TestRequest.TestEnum in /Users/trent/src/github.com/sajari/proto-php-example/vendor/google/protobuf/php/src/Google/Protobuf/Internal/DescriptorPool.php on line 125
PHP Notice:  Undefined index:  in /Users/trent/src/github.com/sajari/proto-php-example/vendor/google/protobuf/php/src/Google/Protobuf/Internal/DescriptorPool.php on line 126

Notice: Undefined index:  in /Users/trent/src/github.com/sajari/proto-php-example/vendor/google/protobuf/php/src/Google/Protobuf/Internal/DescriptorPool.php on line 126
Hello Foo!

tbillington avatar Feb 22 '17 00:02 tbillington

@TeBoring: This is still happening. It's due to the fact that enum types nested inside messages aren't being registered correctly in the descriptor pool.

message TestRequest {
  enum TestEnum {
    ZERO = 0;
    ONE = 1;
  }

  string name = 1;

  TestEnum test_enum = 2;
}

The TestEnum enum is being generated as \SimpleTest\TestRequest_TestEnum, yet there's no record of it in anything internal to DescriptorPool. This leads me to believe that there's either a bug or an unhandled valid case in descriptor.php. This is almost certainly a bug in the protobuf syntax parser, and I'd really appreciate if you could give it some priority, as it's not treating valid protobuf syntax correctly.

mcos avatar Mar 27 '17 21:03 mcos

Also, following on from this, there's no test for this situation in https://github.com/google/protobuf/blob/d9e0119f2c2f49ea89012a03c030e2b5011d0bc3/php/tests/proto/test.proto, which I would expect to fail here.

mcos avatar Mar 27 '17 21:03 mcos

@TeBoring any progress on this?

mcos avatar Apr 04 '17 14:04 mcos

Sorry, I don't have time to work on this yet. But I do want to include the fix in the 3.3 release. @mcos Could you send a fix?

TeBoring avatar Apr 07 '17 19:04 TeBoring

@mcos @TeBoring I'm having the same issue. It seems that nested enums inside messages are not being parsed correctly. I believe this is happening at \Google\Protobuf\Internal\FileDescriptor::buildFromProto($proto). It seems that only the messages/enums in the top level are being parsed. Shouldn't this be a recursive function to be able to parse types for more than 1 level based on the .proto file ?

svdev avatar Apr 16 '17 12:04 svdev

Added test in #2989. However, I don't find the bug. @mcos, @svdev, could you verify? BTW, grpc may depend on old version of protobuf. The latest changes for php is in 3.3.x branch.

TeBoring avatar Apr 20 '17 03:04 TeBoring

@TeBoring My issue was that I didn't specify a package statement in each .proto file so the descriptor had loaded all the nested enums classes but with an incorrect namespace. For instance, my generated classes were trying to load \MyPackage\.MyMessage_MyEnumClass.php but they were registered by the descriptor pool as .MyMessage_MyEnumClass.php .

After specifying the package in each .proto file I'm able to load all nested enum/messages classes correctly.

svdev avatar Apr 20 '17 11:04 svdev

Could you paste your proto?

TeBoring avatar Apr 20 '17 17:04 TeBoring

test_no_namespace.proto should have covered your case.

TeBoring avatar Apr 20 '17 17:04 TeBoring