Skip to content

Commit 64f63d1

Browse files
authored
BE: Chore: standarize Protobuf import paths (#723)
1 parent 21edbf7 commit 64f63d1

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

api/src/main/java/io/kafbat/ui/serdes/builtin/ProtobufFileSerde.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import javax.annotation.Nullable;
6565
import lombok.SneakyThrows;
6666
import lombok.extern.slf4j.Slf4j;
67+
import org.apache.commons.lang3.SystemUtils;
6768
import org.jetbrains.annotations.NotNull;
6869

6970
@Slf4j
@@ -416,7 +417,7 @@ private Map<String, ProtoFile> loadFilesWithLocations() {
416417
files.filter(p -> !Files.isDirectory(p) && p.toString().endsWith(".proto"))
417418
.forEach(path -> {
418419
// relative path will be used as "import" statement
419-
String relativePath = baseLocation.relativize(path).toString();
420+
String relativePath = removeBackSlashes(baseLocation.relativize(path).toString());
420421
var protoFileElement = ProtoParser.Companion.parse(
421422
Location.get(baseLocation.toString(), relativePath),
422423
readFileAsString(path)
@@ -426,6 +427,27 @@ private Map<String, ProtoFile> loadFilesWithLocations() {
426427
}
427428
return filesByLocations;
428429
}
430+
431+
/**
432+
* Replaces backslashes in the given file path with forward slashes if the operating system is Windows.
433+
*
434+
* <p>This method is designed to standardize file paths by converting Windows-style backslashes (`\`)
435+
* to Linux/Unix-style forward slashes (`/`) when the application is running on a Windows OS.
436+
* On other operating systems, the input path is returned unchanged.</p>
437+
*
438+
* <p>This is needed because imports in Protobuf use forward slashes (`/`)
439+
* which causes a conflict with Windows paths. For example,`language/language.proto`
440+
* would be converted to `language\language.proto` in Windows causing a resolution exception</p>
441+
*
442+
* @param path the file path to standardize; must not be {@code null}.
443+
* @return the standardized file path with forward slashes if running on Windows, or the original path otherwise.
444+
*/
445+
private @NotNull String removeBackSlashes(@NotNull final String path) {
446+
if (SystemUtils.IS_OS_WINDOWS) {
447+
return path.replace("\\", "/");
448+
}
449+
return path;
450+
}
429451
}
430452

431453
}

0 commit comments

Comments
 (0)