Skip to content

Commit 1dc2fde

Browse files
marcelstoerRobWin
authored andcommitted
Fix issue with mixed path separator in input path
1 parent ec3329c commit 1dc2fde

3 files changed

Lines changed: 44 additions & 4 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*.iws
55
.idea/
66
.gradle/
7+
out/
78

89
## Eclipse ignores
910
/.settings

src/main/java/io/github/swagger2markup/Swagger2MarkupMojo.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ private File getEffectiveOutputDirWhenInputIsAFolder(Swagger2MarkupConverter con
142142
* If the folder the current Swagger file resides in contains at least one other Swagger file then the
143143
* output dir must have an extra subdir per file to avoid markdown files getting overwritten.
144144
*/
145-
outputDirAddendum += File.separator + extracSwaggerFileNameWithoutExtension(converter);
145+
outputDirAddendum += File.separator + extractSwaggerFileNameWithoutExtension(converter);
146146
}
147147
return new File(outputDir, outputDirAddendum);
148148
}
@@ -159,7 +159,7 @@ private String getInputDirStructurePath(Swagger2MarkupConverter converter) {
159159
*/
160160
String swaggerFilePath = new File(converter.getContext().getSwaggerLocation()).getAbsolutePath(); // /Users/foo/bar-service/v1/bar.yaml
161161
String swaggerFileFolder = StringUtils.substringBeforeLast(swaggerFilePath, File.separator); // /Users/foo/bar-service/v1
162-
return StringUtils.remove(swaggerFileFolder, swaggerInput); // /bar-service/v1
162+
return StringUtils.remove(swaggerFileFolder, getSwaggerInputAbsolutePath()); // /bar-service/v1
163163
}
164164

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

171-
private String extracSwaggerFileNameWithoutExtension(Swagger2MarkupConverter converter) {
171+
private String extractSwaggerFileNameWithoutExtension(Swagger2MarkupConverter converter) {
172172
return FilenameUtils.removeExtension(new File(converter.getContext().getSwaggerLocation()).getName());
173173
}
174174

175175
private Collection<File> getSwaggerFiles(File directory, boolean recursive) {
176176
return FileUtils.listFiles(directory, new String[]{"yaml", "yml", "json"}, recursive);
177177
}
178+
179+
/*
180+
* The 'swaggerInput' provided by the user can be anything; it's just a string. Hence, it could by Unix-style,
181+
* Windows-style or even a mix thereof. This methods turns the input into a File and returns its absolute path. It
182+
* will be platform dependent as far as file separators go but at least the separators will be consistent.
183+
*/
184+
private String getSwaggerInputAbsolutePath(){
185+
return new File(swaggerInput).getAbsolutePath();
186+
}
178187
}

src/test/java/io/github/swagger2markup/Swagger2MarkupMojoTest.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,25 @@ public void shouldConvertIntoDirectoryIfInputIsDirectory() throws Exception {
9797
assertThat(outputFiles).containsOnly("definitions.adoc", "overview.adoc", "paths.adoc", "security.adoc");
9898
}
9999

100+
@Test
101+
public void shouldConvertIntoDirectoryIfInputIsDirectoryWithMixedSeparators() throws Exception {
102+
//given that the input folder contains a nested structure with Swagger files but path to it contains mixed file
103+
//separators on Windows (/ and \)
104+
Swagger2MarkupMojo mojo = new Swagger2MarkupMojo();
105+
String swaggerInputPath = new File(RESOURCES_DIR).getAbsoluteFile().getAbsolutePath();
106+
mojo.swaggerInput = replaceLast(swaggerInputPath, "\\", "/");
107+
mojo.outputDir = new File(OUTPUT_DIR).getAbsoluteFile();
108+
109+
//when
110+
mojo.execute();
111+
112+
//then
113+
Iterable<String> outputFiles = recursivelyListFileNames(new File(mojo.outputDir, SWAGGER_DIR));
114+
assertThat(outputFiles).containsOnly("definitions.adoc", "overview.adoc", "paths.adoc", "security.adoc");
115+
outputFiles = listFileNames(new File(mojo.outputDir, SWAGGER_DIR + "2"), false);
116+
assertThat(outputFiles).containsOnly("definitions.adoc", "overview.adoc", "paths.adoc", "security.adoc");
117+
}
118+
100119
@Test
101120
public void shouldConvertIntoSubDirectoryIfMultipleSwaggerFilesInSameInput() throws Exception {
102121
//given that the input folder contains two Swagger files
@@ -123,7 +142,7 @@ public void shouldConvertIntoSubDirectoryOneFileIfMultipleSwaggerFilesInSameInpu
123142
mojo.swaggerInput = new File(INPUT_DIR).getAbsoluteFile().getAbsolutePath();
124143
mojo.outputDir = new File(OUTPUT_DIR).getAbsoluteFile();
125144
mojo.outputFile = new File(SWAGGER_OUTPUT_FILE);
126-
145+
127146
//when
128147
mojo.execute();
129148

@@ -208,4 +227,15 @@ private static Iterable<String> listFileNames(File dir, boolean recursive) {
208227
private static void verifyFileContains(File file, String value) throws IOException {
209228
assertThat(IOUtils.toString(file.toURI(), StandardCharsets.UTF_8)).contains(value);
210229
}
230+
231+
private static String replaceLast(String input, String search, String replace) {
232+
int lastIndex = input.lastIndexOf(search);
233+
if (lastIndex > -1) {
234+
return input.substring(0, lastIndex)
235+
+ replace
236+
+ input.substring(lastIndex + search.length(), input.length());
237+
} else {
238+
return input;
239+
}
240+
}
211241
}

0 commit comments

Comments
 (0)