Skip to content

Commit 83828d2

Browse files
authored
Merge pull request #67 from salesforce/bugfix/jprotoc-outer-class
Append 'OuterClass' to outer proto type names when the outer and inne…
2 parents 347c90b + 9a72ce6 commit 83828d2

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

jprotoc-test/src/test/proto/helloworld.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package helloworld;
44

55
import "google/protobuf/empty.proto";
66
import "google/protobuf/timestamp.proto";
7+
import "some_parameter.proto";
78

89
option java_multiple_files = true;
910
option java_package = "com.salesforce.jprotoc";
@@ -16,11 +17,14 @@ service Greeter {
1617

1718
// Sends the current time
1819
rpc SayTime (google.protobuf.Empty) returns (TimeResponse) {}
20+
21+
rpc SomeParam (my.someparameters.SomeParameter) returns (my.someparameters.SomeParameter) {}
1922
}
2023

2124
// The request message containing the user's name.
2225
message HelloRequest {
2326
string name = 1;
27+
my.someparameters.SomeParameter sp = 2;
2428
}
2529

2630
// The response message containing the greetings
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
syntax = "proto3";
2+
3+
//option java_outer_classname = "SomeParameterProto";
4+
5+
package my.someparameters;
6+
7+
message SomeParameter {
8+
string id = 1;
9+
}

jprotoc/src/main/java/com/salesforce/jprotoc/ProtoTypeMap.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public static ProtoTypeMap of(@Nonnull Collection<DescriptorProtos.FileDescripto
5656
null :
5757
getJavaOuterClassname(fileDescriptor, fileOptions);
5858

59+
60+
5961
fileDescriptor.getEnumTypeList().forEach(
6062
e -> types.put(
6163
protoPackage + "." + e.getName(),
@@ -109,7 +111,20 @@ private static String getJavaOuterClassname(
109111
// If the outer class name is not explicitly defined, then we take the proto filename, strip its extension,
110112
// and convert it from snake case to camel case.
111113
String filename = fileDescriptor.getName().substring(0, fileDescriptor.getName().length() - ".proto".length());
114+
filename = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, filename);
115+
return appendOuterClassSuffix(filename, fileDescriptor);
116+
}
112117

113-
return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, filename);
118+
/**
119+
* In the event of a name conflict between the outer and inner type names, protoc adds an OuterClass suffix to the
120+
* outer type's name.
121+
*/
122+
private static String appendOuterClassSuffix(final String enclosingClassName, DescriptorProtos.FileDescriptorProto fd) {
123+
if (fd.getEnumTypeList().stream().anyMatch(enumProto -> enumProto.getName().equals(enclosingClassName)) ||
124+
fd.getMessageTypeList().stream().anyMatch(messageProto -> messageProto.getName().equals(enclosingClassName))) {
125+
return enclosingClassName + "OuterClass";
126+
} else {
127+
return enclosingClassName;
128+
}
114129
}
115130
}

0 commit comments

Comments
 (0)