spiral-modules / php-grpc

:electric_plug: Fast and furious GRPC server for PHP applications

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Missing namespace on generated interface parameters

zarianec opened this issue · comments

Namespaces are missed in generated interface method parameters if this parameters are classes which are generated from proto files imported from subfolders.

Structure of test proto files:
image

service.proto

syntax = "proto3";

package test.service;

import "subfolder/something.proto";

service TestService {
    rpc getSomething (test.subfolder.Something) returns (test.subfolder.Something);
}

something.proto

syntax = "proto3";

package test.subfolder;

message Something {
    int64 id = 1;
    string content = 2;
}

Generated files:
image

TestServiceInterface

<?php
# Generated by the protocol buffer compiler (spiral/php-grpc). DO NOT EDIT!
# source: service.proto
 
namespace Test\Service;

use Spiral\GRPC;

interface TestServiceInterface extends GRPC\ServiceInterface
{
    // GRPC specific service name.
    public const NAME = "test.service.TestService"; 

    /**
    * @param GRPC\ContextInterface $ctx
    * @param Something $in
    * @return Something
    *
    * @throws GRPC\Exception\InvokeException
    */
    public function getSomething(GRPC\ContextInterface $ctx, Something $in): Something;
}

You can see that getSomething method requires Something class as a second parameter but this class isn't in the same namespace so it must be imported before or defined with FQCN in parameters list (like \Test\Subfolder\Something)

I tried to fix it to be able to generate proper stubs for my project and it works and passes the test. But i think that one more test case must be added to cover this bug.

What i changed:
cmd/protoc-gen-php-grpc/php/template.go:65

func init() {
	tpl = template.Must(template.New("phpBody").Funcs(template.FuncMap{
		"namespace": func(pkg *string) string {
			return namespace(pkg, "\\")
		},
		"message": func(name *string) string {
			fqcn := "\\" + namespace(name, "\\") // HERE i use namespace() instead of identifier()
			return fqcn
		},
		"interface": func(name *string) string {
			return identifier(*name, "interface")
		},
		"name": func(pkg *string, name *string) string {
			return fmt.Sprintf("%s.%s", *pkg, *name)
		},
	}).Parse(phpBody))
}

So now it generates fqcn for all the parameter classes even if they are in the same namespace.

Seems connected to #2

@zarianec can you create PR with your test? Thank you

Finally had some time to create a tests for output generated by plugin (#10).
Test_UseImportedMessage is related to this issue.
Also i fixed the bug with php_namespace option (#2) earlier for the project i'm working on but now it conflicts with possible easy fix for this issue.
I will try to find more time for fixing it.

Thank you for an awesome test suite!

Fixed in 1.0.2