protoc-gen-validate icon indicating copy to clipboard operation
protoc-gen-validate copied to clipboard

Any proto file that uses this package can't generate PHP code because this project is still proto2

Open veqryn opened this issue 7 years ago • 9 comments

Our servers are all in golang, but some of our clients are in PHP, so we generate PHP libraries for all of our proto files.

If you use protoc-gen-validate, you would have this import in your proto file: import "protoc-gen-validate/validate/validate.proto";

If you generate PHP out of your proto file, you will end up with a file that contains this:

<?php
# Generated by the protocol buffer compiler.  DO NOT EDIT!
# source: v1.proto

namespace GPBMetadata;
class V1
{
    public static $is_initialized = false;
    public static function initOnce() {
        $pool = \Google\Protobuf\Internal\DescriptorPool::getGeneratedPool();
        if (static::$is_initialized == true) {
          return;
        }
        \GPBMetadata\Google\Api\Annotations::initOnce();
        \GPBMetadata\Google\Protobuf\Timestamp::initOnce();
        \GPBMetadata\ProtocGenValidate\Validate\Validate::initOnce();

If you try to use this PHP code, you will need to have the php library \GPBMetadata\ProtocGenValidate\Validate\Validate

I solved this for other proto files by just generating them, so I took the same approach here:

$ protoc -I=./include -I=./vendor/github.com/lyft --plugin=protoc-gen-grpc=/go/src/github.com/grpc/bins/opt/grpc_php_plugin --grpc_out=./php/protoc-gen-validate --php_out=./php/protoc-gen-validate /vendor/github.com/lyft/protoc-gen-validate/validate/validate.proto
--php_out: protoc-gen-validate/validate/validate.proto: Can only generate PHP code for proto3 .proto files.
Please add 'syntax = "proto3";' to the top of your .proto file.

This fails because php can only generate files for proto3.

veqryn avatar Nov 01 '18 08:11 veqryn

The PHP plugin's contributors plan to eventually add proto2 support, but if we were to ignore that, this would likely require a major breaking change for this project.

We've run into this as well internally with PHP (not just extensions, but also legacy protos) that required us to eschew the official plugin and instead used https://github.com/stanley-cheung/Protobuf-PHP. Certainly not ideal, and there are even more gotchas there...

rodaine avatar Nov 01 '18 19:11 rodaine

Does this project plan to eventually upgrade to proto3?

Obviously it could be a breaking change, as most major versions bumps are, but I assume you would want to do it eventually...

veqryn avatar Nov 01 '18 19:11 veqryn

There's no strong desire to upgrade the extensions from proto2 to proto3 at the moment. Also, the extensions rely on the descriptors.proto which are defined in proto2 and are core to the entire ecosystem. Wouldn't you run into issues once you reach that level as well?

I'd rather wait for the proto2 syntax to be supported by the official PHP plugin than break current users of this tool.

rodaine avatar Nov 01 '18 19:11 rodaine

I wasn't aware that descriptors.proto was still proto2. You'd obviously have to wait on that to get upgraded to proto3 first, before you think about upgrading this library. This issue can be tabled til then I suppose.

veqryn avatar Nov 01 '18 19:11 veqryn

@rodaine The difference between a regular proto2 syntaxed file and descriptor.proto: generating bindings for descriptor.proto file is explicitly allowed by protoc. https://github.com/protocolbuffers/protobuf/blob/7089ff0ef217e573ab89653d9905f817caeb2afb/src/google/protobuf/compiler/php/php_generator.cc#L2233 😄

rauanmayemir avatar Jan 25 '21 09:01 rauanmayemir

I realise it could be painful for existing projects and is a breaking change, but I have just tried converting validate.proto to proto3 syntax and building it with experimental_allow_proto3_optional flag.

Barring the lack of default values in proto3, everything seems to just work with minimal changes to the protobuf schema.

rauanmayemir avatar Jan 25 '21 13:01 rauanmayemir

Hi, I have encountered this problem lately. The code is generated, and it is trying to call the following statement.

        \GPBMetadata\Validate\Validate::initOnce();

But, the \GPBMetadata\Validate\Validate is not exists, so I when I call the client, I get error as follows:

Error: Class 'GPBMetadata\Validate\Validate' not found

Is there any work around for this?

ganchiku avatar Jan 28 '21 13:01 ganchiku

@ganchiku There is. You generate stubs for validate/validate.proto as well.

rauanmayemir avatar Jan 28 '21 13:01 rauanmayemir

@rauanmayemir Thank you for your quick response! I will try to generate the stubs for validate/validate.proto . 🙇

ganchiku avatar Jan 28 '21 13:01 ganchiku

After hours of poking around, I finally found a brute force solution:

Create an empty file named validate/validate.proto with content:

syntax = "proto3";

package validate;

Then generate the php out and mv the files to the target directory. Then everything is ready.

acrazing avatar Mar 29 '23 01:03 acrazing

@acrazing One little thing that will break if someone's depending on it:

  // This applies to regexes HTTP_HEADER_NAME and HTTP_HEADER_VALUE to enable
  // strict header validation.
  // By default, this is true, and HTTP header validations are RFC-compliant.
  // Setting to false will enable a looser validations that only disallows
  // \r\n\0 characters, which can be used to bypass header matching rules.
  optional bool strict = 25 [default = true];

There's no custom default in proto3. Though it doesn't seem very critical.

rauanmayemir avatar Mar 29 '23 05:03 rauanmayemir

Now that @bufdev have taken on this project, is there any plan to make it proto 2 and 3 compatible? It would be very useful to be able to use Validate in projects that also have a PHP target, even if we can't generate the validation code and use it.

jbendotnet avatar Jun 14 '23 15:06 jbendotnet

Thank you for raising this issue related to generating PHP libraries from proto files. Your detailed explanation helps us understand the situation better.

Given your mixed Golang and PHP environment and the specific challenges with generating PHP files from proto2 syntax, we'd like to emphasize that we have no plans to support proto2 in our future developments. We highly recommend considering a migration to protovalidate, which is designed exclusively for proto3 and offers a smoother development experience without code generation.

As a result, I'll be closing this issue. If you have any further questions or concerns, please feel free to reach out.

elliotmjackson avatar Aug 10 '23 17:08 elliotmjackson