spark-swagger
spark-swagger copied to clipboard
SwaggerHammer.filesFromDir fails on Windows filesystem
When running from a Windows file system (e.g. IDE running from source) filesFromDir fails to locate any ui or ui/template files. The cause is rootPath has / as the separator whereas Files.walk returns a list with \ as the separator, so the map(path -> path.toString().replace(rootPath, "")) doesn't make any replacements. I worked around it by adding a preceding replace("\\", "/")), not sure if that is the best way to deal with it though :)
Here is my workaround:
public List<String> filesFromDir(String prefix, String dir) throws IOException {
String rootPath = dir.replace("file:/", "");
List<String> result;
try (Stream<Path> stream = Files.walk(Paths.get(rootPath).toAbsolutePath())) {
result = stream
.filter(path -> !Files.isDirectory(path))
.map(path -> path.toString().replace("\\", "/").replace(rootPath, ""))
.filter(name -> !name.equals(prefix) && name.startsWith(prefix))
.collect(Collectors.toList());
}
return result;
}