Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Can't generate PHP code #319

Open
oguzhand95 opened this issue Feb 27, 2025 · 3 comments
Open

[Question] Can't generate PHP code #319

oguzhand95 opened this issue Feb 27, 2025 · 3 comments
Labels
Language Support Interest in adding support for another language Question Questions about the project or libraries

Comments

@oguzhand95
Copy link

We have an open-source http/gRPC service written in Go utilizing protovalidate to validate requests/responses.

For our php SDK, I was generating code with the following buf.gen.yaml by executing buf generate <path-to-protos-from-open-source-project>:

version: v2
plugins:
  - remote: buf.build/protocolbuffers/php:v29.3
    out: ./src

  - remote: buf.build/grpc/php:v1.70.1
    out: ./src

This allowed us to generate the php code required in the SDK up until protovalidate migrated from proto3 to proto2 in this change.

Since then the following error is thrown:

Failure: failed_precondition: plugin "buf.build/protocolbuffers/php:v29.3" failed: Can't generate PHP code for closed enum buf.validate.Ignore.  Please use either proto3 or editions without `enum_type = CLOSED`.

I was wondering if anyone had any experience with such problem and have any suggestions?

@oguzhand95 oguzhand95 changed the title Can't generate PHP code [Question] Can't generate PHP code Feb 27, 2025
@rodaine rodaine added Language Support Interest in adding support for another language Question Questions about the project or libraries labels Feb 27, 2025
@nicksnyder
Copy link
Member

The underlying issue you are encountering is that the official PHP code generator that Google maintains doesn't support proto2, and it is the only generator that doesn't.

Protovalidate needs to use extensions to be able to provide the ability for users to define custom sharable constraints (which has been a highly requested feature), and unfortunately this requires either proto2 or edition2023. We chose proto2, but PHP codegen supports neither right now so either way this was going to be a problem with PHP.

A similar conversation about this happened in protoc-gen-validate (the predecessor to protovalidate) here: bufbuild/protoc-gen-validate#113.

Your high level options are:

  1. Use a third party PHP generator that does support proto2
  2. Try to contribute to the official generator to add support for proto2
  3. Write a script to pre-process your protos and strip protovalidate annotations+imports before passing them to the official PHP generator.

#3 is likely the most pragmatic solution for you in the near term.

Sorry there isn't a better answer to this right now, but keep us posted if you find success in one of these options.

@timostamm
Copy link
Member

The buf CLI has an open feature request that would make it very easy to apply option 3: bufbuild/buf#645

With a few lines of configuration, you'll be able to filter out Protovalidate options (and related imports) from the Protobuf files you want to generate. You will not be able to validate with PHP, but you will still be able to generate code for PHP.

This feature is currently being worked on, and is hopefully available soon, but please be aware that we might run into roadblocks that force us to reprioritize.

@oguzhand95
Copy link
Author

Thanks for the through explanation, as both of you suggested the number 3 seems the most sensible approach for us. I will follow the related PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Language Support Interest in adding support for another language Question Questions about the project or libraries
Projects
None yet
Development

No branches or pull requests

4 participants