Skip to content

Commit f1682d4

Browse files
authored
Merge pull request #3 from openapi-processor/improve-path-handling
improve path handling
2 parents 0931893 + 64f0ca3 commit f1682d4

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

src/main/kotlin/com/github/hauner/openapi/json/processor/JsonProcessor.kt

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,15 @@ class JsonProcessor : OpenApiProcessor {
5656
return
5757
}
5858

59-
if (!hasScheme (apiPath)) {
60-
apiPath = "file://${apiPath}"
61-
}
59+
apiPath = toURL(apiPath).toString()
6260

6361
var targetDir: String? = options["targetDir"]?.toString()
6462
if (targetDir == null) {
6563
println("openapi-processor-json: missing targetDir!")
6664
return
6765
}
6866

69-
if (!hasScheme (targetDir)) {
70-
targetDir = "file://${targetDir}"
71-
}
67+
targetDir = toURL(targetDir).toString()
7268

7369
val opts = ParseOptions()
7470
val result: SwaggerParseResult = OpenAPIV3Parser()
@@ -83,12 +79,32 @@ class JsonProcessor : OpenApiProcessor {
8379
targetPath.toFile().writeText(json)
8480
}
8581

86-
private fun hasScheme(path: String?): Boolean {
87-
if (path == null) {
88-
return false
82+
/**
83+
* convert source to a valid URL.
84+
*
85+
* if the source is an url string it converts it to an URL
86+
* if the source is not an URL it assumes a local path and prefixes it with file://(//) to
87+
* create a valid URL.
88+
*
89+
* @param source source path or url
90+
* @return an URL to the given source
91+
*/
92+
private fun toURL(source: String): URL {
93+
try {
94+
return URL(source)
95+
} catch (ignore: Exception) {
96+
// catch
8997
}
9098

91-
return path.indexOf ("://") > -1
99+
try {
100+
return Paths.get(source)
101+
.normalize ()
102+
.toUri ()
103+
.toURL ()
104+
} catch (e: Exception) {
105+
throw e
106+
}
92107
}
93108

109+
94110
}

src/test/groovy/com/github/hauner/openapi/json/processor/JsonProcessorSpec.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ class JsonProcessorSpec extends Specification {
4848
if (e != a) {
4949
printUnifiedDiff(new File(expJson), new File(targetPath))
5050
}
51-
e == a
51+
52+
e.replace('\r', '') == a.replace('\r', '')
5253
}
5354

5455
private void printUnifiedDiff (File expected, File generated) {

0 commit comments

Comments
 (0)