Skip to content

improve path handling #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,15 @@ class JsonProcessor : OpenApiProcessor {
return
}

if (!hasScheme (apiPath)) {
apiPath = "file://${apiPath}"
}
apiPath = toURL(apiPath).toString()

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

if (!hasScheme (targetDir)) {
targetDir = "file://${targetDir}"
}
targetDir = toURL(targetDir).toString()

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

private fun hasScheme(path: String?): Boolean {
if (path == null) {
return false
/**
* convert source to a valid URL.
*
* if the source is an url string it converts it to an URL
* if the source is not an URL it assumes a local path and prefixes it with file://(//) to
* create a valid URL.
*
* @param source source path or url
* @return an URL to the given source
*/
private fun toURL(source: String): URL {
try {
return URL(source)
} catch (ignore: Exception) {
// catch
}

return path.indexOf ("://") > -1
try {
return Paths.get(source)
.normalize ()
.toUri ()
.toURL ()
} catch (e: Exception) {
throw e
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class JsonProcessorSpec extends Specification {
if (e != a) {
printUnifiedDiff(new File(expJson), new File(targetPath))
}
e == a

e.replace('\r', '') == a.replace('\r', '')
}

private void printUnifiedDiff (File expected, File generated) {
Expand Down