Skip to content

Commit 416d8dd

Browse files
committed
improved file handling
1 parent cd6a6a0 commit 416d8dd

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

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

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import io.swagger.v3.core.util.Json
2121
import io.swagger.v3.parser.OpenAPIV3Parser
2222
import io.swagger.v3.parser.core.models.ParseOptions
2323
import io.swagger.v3.parser.core.models.SwaggerParseResult
24-
import java.io.File
24+
import java.net.URL
25+
import java.nio.file.Files
26+
import java.nio.file.Paths
2527

2628
/**
2729
* Entry point of the openapi-processor-json.
@@ -48,28 +50,45 @@ class JsonProcessor : OpenApiProcessor {
4850
* @param options map of processor properties
4951
*/
5052
override fun run(options: MutableMap<String, *>) {
51-
val apiPath: String? = options["apiPath"]?.toString()
52-
val targetDir: String? = options["targetDir"]?.toString()
53-
53+
var apiPath: String? = options["apiPath"]?.toString()
5454
if (apiPath == null) {
5555
println("openapi-processor-json: missing apiPath!")
5656
return
5757
}
5858

59+
if (!hasScheme (apiPath)) {
60+
apiPath = "file://${apiPath}"
61+
}
62+
63+
var targetDir: String? = options["targetDir"]?.toString()
5964
if (targetDir == null) {
6065
println("openapi-processor-json: missing targetDir!")
6166
return
6267
}
6368

69+
if (!hasScheme (targetDir)) {
70+
targetDir = "file://${targetDir}"
71+
}
72+
6473
val opts = ParseOptions()
6574
val result: SwaggerParseResult = OpenAPIV3Parser()
6675
.readLocation(apiPath, null, opts)
6776

68-
val json = Json.pretty(result.openAPI)
69-
val jsonn = json + "\n"
77+
var json= Json.pretty(result.openAPI)
78+
json += "\n"
79+
80+
val p = Paths.get(URL(targetDir).toURI())
81+
val dir = Files.createDirectories(p)
82+
val targetPath = dir.resolve("openapi.json")
83+
targetPath.toFile().writeText(json)
84+
}
85+
86+
private fun hasScheme(path: String?): Boolean {
87+
if (path == null) {
88+
return false
89+
}
7090

71-
val targetPath = listOf(targetDir, "openapi.json").joinToString(separator = File.separator)
72-
File(targetPath).writeText(jsonn)
91+
return path.indexOf ("://") > -1
7392
}
7493

7594
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ class JsonProcessorSpec extends Specification {
3434
def targetPath = [targetDir, 'openapi.json'].join(File.separator)
3535

3636
def options = [
37-
apiPath: apiPath,
37+
apiPath: new File(apiPath).canonicalPath,
3838
targetDir: targetDir
3939
]
4040

4141
when:
42-
def generatr = new JsonProcessor()
43-
generatr.run (options)
42+
def processor = new JsonProcessor()
43+
processor.run (options)
4444

4545
then:
4646
def e = new File(expJson).text

0 commit comments

Comments
 (0)