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

Class 'GPBMetadata\ProtocGenSwagger\Options\Annotations' not found #794

Closed
veqryn opened this issue Nov 1, 2018 · 5 comments
Closed

Class 'GPBMetadata\ProtocGenSwagger\Options\Annotations' not found #794

veqryn opened this issue Nov 1, 2018 · 5 comments

Comments

@veqryn
Copy link

veqryn commented Nov 1, 2018

What version of protobuf and what language are you using?
Version: v3.6.1
Language: PHP 5.6.38

What operating system (Linux, Windows, ...) and version?
CentOS Linux release 7.5.1804 (Core)

What runtime / compiler are you using (e.g., python version or gcc version)
PHP 5.6.38 (cli) (built: Sep 12 2018 08:52:18)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

What did you do?
I use GRPC Gateway (https://github.com/grpc-ecosystem/grpc-gateway), generate code for Go and PHP. Go code works well. But I get the error for PHP:

PHP Fatal error: Class 'GPBMetadata\ProtocGenSwagger\Options\Annotations' not found
My *.proto file:

syntax = "proto3";

package foo;

import "request/request.proto";
import "google/api/annotations.proto";
import "google/protobuf/empty.proto";
import "protoc-gen-swagger/options/annotations.proto";

service Bar {
  rpc GetBar (request.ByID) returns (Bar) {
    option (google.api.http).get = "/v1alpha/bar/id/{ID}";
  }
}

Generated php file with metadata:

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

namespace Foo\GPBMetadata;

class Bar
{
    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\GPBEmpty::initOnce();
        \GPBMetadata\ProtocGenSwagger\Options\Annotations::initOnce();
        $pool->internalAddGeneratedFile(hex2bin(
        ...
    }

    ...
}

My composer.json:

    "require": {
        "grpc/grpc": "v1.15.0",
        "google/protobuf": "v3.6.1",
        "google/gax": "0.37.0"
    },
    "repositories": [
      {
          "type": "vcs",
          "url":  "git@github.com:googleapis/gax-php.git"
      }
    ],

What did you expect to see
I expect to have working generated code and to have GPBMetadata\ProtocGenSwagger\Options\Annotations on any of my composer packages but I don't have it.

What did you see instead?
I see the fatal error and I really couldn't find this class anywhere on Github. Maybe I didn't add some additional composer package? If yes, I don't see any docs about this point.

If you need more info, please ask.

@johanbrandhorst
Copy link
Collaborator

This is because you need to generate a PHP version of the swagger annotations proto file. We're not going to maintain a selection of pre generated files for all supported languages I'm afraid. It should be simple to do it manually. Does that makes sense?

@veqryn
Copy link
Author

veqryn commented Nov 1, 2018

K, I will give that a try.

@ethanhinson
Copy link

Are there any examples of how compiling the annotations into an individual project would work? I'm trying to use this:

protoc --proto_path=$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger/options \
  -I /usr/local/include \
  -I $GOPATH/src/github.com/grpc-ecosystem/grpc-gateway \
  -I $GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
  -I $GOPATH/src \
  --php_out=/app/gen/php $GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger/options/annotations.proto

protoc --proto_path=$GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger/options \
  -I /usr/local/include \
  -I $GOPATH/src/github.com/grpc-ecosystem/grpc-gateway \
  -I $GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
  -I $GOPATH/src \
  --php_out=/app/gen/php $GOPATH/src/github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger/options/openapiv2.proto

Both files are created successfully but their namespace is just GPBMetadata instead of GPBMetadata\ProtocGenSwagger\Options.

Even if I manually hack the namespaces and the location of the files. When I try to use one of the messages I receive the error:

Fatal error: Unable to load descriptor: Depends on file 'protoc-gen-swagger/options/annotations.proto', but it has not been loaded

I suspect there is something I am doing wrong rather than a bug. An example or pointer in the right direction would be great.

@johanbrandhorst
Copy link
Collaborator

Hey Ethan, unfortunately we don't have any PHP examples in the repo or the docs, and I have no personal experience with the PHP generator, so you may have to ask elsewhere. This is a generic protobuf problem, unrelated to grpc-gateway.

@ethanhinson
Copy link

ethanhinson commented May 17, 2020

Thank you for the response on such an old issue. For those landing here from Google (it's the top search result when looking for info about this error). I ended up just splitting the messages into separate .proto files from the rpc service .proto file that has the Swagger annotations in it. This allowed me to import the messages into the Swagger definition, as well as the generated PHP client. Another solution that frankly is probably more idiomatic for protoc-gen-swagger artifacts would be to generate a client/server for PHP from the generated Swagger spec using Swagger Codegen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants