Skip to content

Commit c8ea834

Browse files
committed
improve path handling
1 parent 23e8164 commit c8ea834

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
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
}

0 commit comments

Comments
 (0)