jhunters / jprotobuf

A useful utility library for java programmer using google protobuf
Apache License 2.0
896 stars 283 forks source link

can't find correct message from imported proto #190

Closed kevincai closed 1 year ago

kevincai commented 2 years ago

a.proto

syntax="proto2";                                                                             

package a;
option java_package = "com.a";

message A {                                                                               
    repeated string column_name = 1;                                                     
};  

b.proto

syntax="proto2";

import public "a.proto";                                                                     

package b;
option java_package = "com.b";

message B {
    optional a.A column_name = 1;
};              

Error:

Exception in thread "main" java.lang.RuntimeException: Message 'B' depend on message 'a.A' is missed
    at com.baidu.bjf.remoting.protobuf.ProtobufIDLProxy.hasDependency(ProtobufIDLProxy.java:1804)
    at com.baidu.bjf.remoting.protobuf.ProtobufIDLProxy.createMessageClass(ProtobufIDLProxy.java:1090)
    at com.baidu.bjf.remoting.protobuf.ProtobufIDLProxy.doCreatePro(ProtobufIDLProxy.java:1436)
    at com.baidu.bjf.remoting.protobuf.ProtobufIDLProxy.doCreatePro(ProtobufIDLProxy.java:1357)
    at com.baidu.bjf.remoting.protobuf.ProtobufIDLProxy.generateSource(ProtobufIDLProxy.java:1595)
    at com.baidu.bjf.remoting.protobuf.ProtobufIDLProxy.generateSource(ProtobufIDLProxy.java:1580)
    at com.baidu.bjf.remoting.protobuf.command.Main.main(Main.java:71)

jprotobuf jar version: 2.4.14

jhunters commented 2 years ago

as jprotobuf will detects "java_package" option name, so while has this option package name reference will change to use this option. follow suggestion will show you how to fix it:

s1: message B { optional com.a.A column_name = 1; };

or s2: message B { optional A column_name = 1; };

kevincai commented 2 years ago

s1:

if use com.a.A, will protoc recognize it as correct message type? After all, the .proto will be used for both java code and c++ code.

s2:

what if b.proto has its own defined message A? how to distinguish b.A and a.A in that case?

kevincai commented 2 years ago

Even with solution 2, putting aside name conflict in different protobuf packages, jprotobuf generates wrong java code

b.proto

syntax="proto2";

import public "a.proto";                                                                     

package b;
option java_package = "com.b";

message B {
    optional A column_name = 1;
};              

generated code

package com.b;

import com.baidu.bjf.remoting.protobuf.FieldType;
import com.baidu.bjf.remoting.protobuf.EnumReadable;
import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;
public class B {
/**
* 
* optional A column_name=1
*/
@Protobuf(fieldType=FieldType.OBJECT, order=1, required=false)
public com.b.A columnName;
public void setColumnName(com.b.A columnName) {
this.columnName=columnName;
};
public com.b.A getColumnName() {
return this.columnName;
};
}
kevincai commented 2 years ago

@jhunters not sure if more information is needed from me?