Skip to content

Commit

Permalink
Fix issue with mixed path separator in input path
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelstoer authored and RobWin committed Apr 25, 2018
1 parent ec3329c commit 1dc2fde
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.iws
.idea/
.gradle/
out/

## Eclipse ignores
/.settings
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/io/github/swagger2markup/Swagger2MarkupMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private File getEffectiveOutputDirWhenInputIsAFolder(Swagger2MarkupConverter con
* If the folder the current Swagger file resides in contains at least one other Swagger file then the
* output dir must have an extra subdir per file to avoid markdown files getting overwritten.
*/
outputDirAddendum += File.separator + extracSwaggerFileNameWithoutExtension(converter);
outputDirAddendum += File.separator + extractSwaggerFileNameWithoutExtension(converter);
}
return new File(outputDir, outputDirAddendum);
}
Expand All @@ -159,7 +159,7 @@ private String getInputDirStructurePath(Swagger2MarkupConverter converter) {
*/
String swaggerFilePath = new File(converter.getContext().getSwaggerLocation()).getAbsolutePath(); // /Users/foo/bar-service/v1/bar.yaml
String swaggerFileFolder = StringUtils.substringBeforeLast(swaggerFilePath, File.separator); // /Users/foo/bar-service/v1
return StringUtils.remove(swaggerFileFolder, swaggerInput); // /bar-service/v1
return StringUtils.remove(swaggerFileFolder, getSwaggerInputAbsolutePath()); // /bar-service/v1
}

private boolean multipleSwaggerFilesInSwaggerLocationFolder(Swagger2MarkupConverter converter) {
Expand All @@ -168,11 +168,20 @@ private boolean multipleSwaggerFilesInSwaggerLocationFolder(Swagger2MarkupConver
return swaggerFiles != null && swaggerFiles.size() > 1;
}

private String extracSwaggerFileNameWithoutExtension(Swagger2MarkupConverter converter) {
private String extractSwaggerFileNameWithoutExtension(Swagger2MarkupConverter converter) {
return FilenameUtils.removeExtension(new File(converter.getContext().getSwaggerLocation()).getName());
}

private Collection<File> getSwaggerFiles(File directory, boolean recursive) {
return FileUtils.listFiles(directory, new String[]{"yaml", "yml", "json"}, recursive);
}

/*
* The 'swaggerInput' provided by the user can be anything; it's just a string. Hence, it could by Unix-style,
* Windows-style or even a mix thereof. This methods turns the input into a File and returns its absolute path. It
* will be platform dependent as far as file separators go but at least the separators will be consistent.
*/
private String getSwaggerInputAbsolutePath(){
return new File(swaggerInput).getAbsolutePath();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,25 @@ public void shouldConvertIntoDirectoryIfInputIsDirectory() throws Exception {
assertThat(outputFiles).containsOnly("definitions.adoc", "overview.adoc", "paths.adoc", "security.adoc");
}

@Test
public void shouldConvertIntoDirectoryIfInputIsDirectoryWithMixedSeparators() throws Exception {
//given that the input folder contains a nested structure with Swagger files but path to it contains mixed file
//separators on Windows (/ and \)
Swagger2MarkupMojo mojo = new Swagger2MarkupMojo();
String swaggerInputPath = new File(RESOURCES_DIR).getAbsoluteFile().getAbsolutePath();
mojo.swaggerInput = replaceLast(swaggerInputPath, "\\", "/");
mojo.outputDir = new File(OUTPUT_DIR).getAbsoluteFile();

//when
mojo.execute();

//then
Iterable<String> outputFiles = recursivelyListFileNames(new File(mojo.outputDir, SWAGGER_DIR));
assertThat(outputFiles).containsOnly("definitions.adoc", "overview.adoc", "paths.adoc", "security.adoc");
outputFiles = listFileNames(new File(mojo.outputDir, SWAGGER_DIR + "2"), false);
assertThat(outputFiles).containsOnly("definitions.adoc", "overview.adoc", "paths.adoc", "security.adoc");
}

@Test
public void shouldConvertIntoSubDirectoryIfMultipleSwaggerFilesInSameInput() throws Exception {
//given that the input folder contains two Swagger files
Expand All @@ -123,7 +142,7 @@ public void shouldConvertIntoSubDirectoryOneFileIfMultipleSwaggerFilesInSameInpu
mojo.swaggerInput = new File(INPUT_DIR).getAbsoluteFile().getAbsolutePath();
mojo.outputDir = new File(OUTPUT_DIR).getAbsoluteFile();
mojo.outputFile = new File(SWAGGER_OUTPUT_FILE);

//when
mojo.execute();

Expand Down Expand Up @@ -208,4 +227,15 @@ private static Iterable<String> listFileNames(File dir, boolean recursive) {
private static void verifyFileContains(File file, String value) throws IOException {
assertThat(IOUtils.toString(file.toURI(), StandardCharsets.UTF_8)).contains(value);
}

private static String replaceLast(String input, String search, String replace) {
int lastIndex = input.lastIndexOf(search);
if (lastIndex > -1) {
return input.substring(0, lastIndex)
+ replace
+ input.substring(lastIndex + search.length(), input.length());
} else {
return input;
}
}
}

0 comments on commit 1dc2fde

Please sign in to comment.