From 1dc2fde8fc966ef6e15e05e58e8d1ebfacc2cb75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20St=C3=B6r?= Date: Wed, 25 Apr 2018 11:43:44 +0200 Subject: [PATCH] Fix issue with mixed path separator in input path --- .gitignore | 1 + .../swagger2markup/Swagger2MarkupMojo.java | 15 +++++++-- .../Swagger2MarkupMojoTest.java | 32 ++++++++++++++++++- 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index f75e7ca..f7d8c2b 100755 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ *.iws .idea/ .gradle/ +out/ ## Eclipse ignores /.settings diff --git a/src/main/java/io/github/swagger2markup/Swagger2MarkupMojo.java b/src/main/java/io/github/swagger2markup/Swagger2MarkupMojo.java index d15cd7d..c9d0a43 100644 --- a/src/main/java/io/github/swagger2markup/Swagger2MarkupMojo.java +++ b/src/main/java/io/github/swagger2markup/Swagger2MarkupMojo.java @@ -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); } @@ -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) { @@ -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 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(); + } } diff --git a/src/test/java/io/github/swagger2markup/Swagger2MarkupMojoTest.java b/src/test/java/io/github/swagger2markup/Swagger2MarkupMojoTest.java index 0839f56..689b759 100644 --- a/src/test/java/io/github/swagger2markup/Swagger2MarkupMojoTest.java +++ b/src/test/java/io/github/swagger2markup/Swagger2MarkupMojoTest.java @@ -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 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 @@ -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(); @@ -208,4 +227,15 @@ private static Iterable 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; + } + } }