Skip to content

Commit dc08077

Browse files
mkruskal-googlecopybara-github
authored andcommitted
Fix a bug calculating the file name in the absense of directories.
PiperOrigin-RevId: 814799893
1 parent 3a7ef79 commit dc08077

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

java/core/src/main/java/com/google/protobuf/GeneratorNames.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ static String getDefaultFileClassName(
139139
FileDescriptorProtoOrBuilder file, boolean useOldOuterClassnameDefault) {
140140
// Replicates the logic of ClassNameResolver::GetFileDefaultImmutableClassName.
141141
String name = file.getName();
142-
name = name.substring(name.lastIndexOf('/'));
142+
// The `+ 1` includes the case where no '/' is present.
143+
name = name.substring(name.lastIndexOf('/') + 1);
143144
name = underscoresToCamelCase(stripProto(name));
144145
return useOldOuterClassnameDefault ? name : name + "Proto";
145146
}

java/core/src/test/java/com/google/protobuf/GeneratorNamesTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,15 @@ public void getFileClassName_weirdExtension() {
234234
.isEqualTo("BarNotproto");
235235
}
236236

237+
@Test
238+
public void getFileClassName_noDirectory() {
239+
// This isn't exercisable in blazel because all our tests are in a directory.
240+
assertThat(
241+
GeneratorNames.getFileClassName(
242+
FileDescriptorProto.newBuilder().setName("bar.proto").build()))
243+
.isEqualTo("Bar");
244+
}
245+
237246
@Test
238247
public void getFileClassName_conflictingName2024() {
239248
// This isn't exercisable in blazel because conflicts trigger a protoc error in edition 2024.

0 commit comments

Comments
 (0)