55
66package com.github.hauner.openapi.json.processor
77
8- import io.openapiprocessor.api.OpenApiProcessor
9- import io.swagger.v3.core.util.Json
10- import io.swagger.v3.parser.OpenAPIV3Parser
11- import io.swagger.v3.parser.core.models.ParseOptions
12- import io.swagger.v3.parser.core.models.SwaggerParseResult
13- import java.net.URL
14- import java.nio.file.Files
15- import java.nio.file.Paths
8+ import io.openapiparser.OpenApiParser
9+ import io.openapiprocessor.jackson.JacksonConverter
10+ import io.openapiprocessor.jackson.JacksonJsonWriter
11+ import io.openapiprocessor.jsonschema.reader.UriReader
12+ import io.openapiprocessor.jsonschema.schema.DocumentLoader
13+ import io.openapiprocessor.jsonschema.schema.DocumentStore
14+ import java.io.File
15+ import java.io.FileWriter
1616
1717/* *
1818 * Entry point of the openapi-processor-json.
19- *
20- * @author Martin Hauner
2119 */
22- class JsonProcessor : OpenApiProcessor {
23-
20+ class JsonProcessor : io.openapiprocessor.api.v2. OpenApiProcessor
21+ {
2422 /* *
2523 * provides the generatr name.
2624 */
@@ -45,55 +43,26 @@ class JsonProcessor : OpenApiProcessor {
4543 return
4644 }
4745
48- apiPath = toURL(apiPath).toString()
49-
5046 var targetDir: String? = options[" targetDir" ]?.toString()
5147 if (targetDir == null ) {
5248 println (" openapi-processor-json: missing targetDir!" )
5349 return
5450 }
5551
56- targetDir = toURL(targetDir).toString()
57-
58- val opts = ParseOptions ()
59- val result: SwaggerParseResult = OpenAPIV3Parser ()
60- .readLocation(apiPath, null , opts)
52+ val reader = UriReader ()
53+ val converter = JacksonConverter ()
54+ val loader = DocumentLoader (reader, converter)
6155
62- var json = Json .pretty(result.openAPI )
63- json + = " \n "
56+ val documents = DocumentStore ( )
57+ val parser = OpenApiParser (documents, loader)
6458
65- val p = Paths .get(URL (targetDir).toURI())
66- val dir = Files .createDirectories(p)
67- val targetPath = dir.resolve(" openapi.json" )
68- targetPath.toFile().writeText(json)
69- }
59+ val baseUri = toURI(apiPath)
60+ val result = parser.parse (baseUri)
61+ val bundled = result.bundle()
7062
71- /* *
72- * convert source to a valid URL.
73- *
74- * if the source is an url string it converts it to an URL
75- * if the source is not an URL it assumes a local path and prefixes it with file://(//) to
76- * create a valid URL.
77- *
78- * @param source source path or url
79- * @return an URL to the given source
80- */
81- private fun toURL (source : String ): URL {
82- try {
83- return URL (source)
84- } catch (ignore: Exception ) {
85- // catch
86- }
63+ val out = FileWriter (listOf (targetDir, " openapi.json" ).joinToString(File .separator))
64+ val writer = JacksonJsonWriter (out )
8765
88- try {
89- return Paths .get(source)
90- .normalize ()
91- .toUri ()
92- .toURL ()
93- } catch (e: Exception ) {
94- throw e
95- }
66+ writer.write(bundled)
9667 }
97-
98-
99- }
68+ }
0 commit comments