Skip to content

Commit 9c44a7e

Browse files
committed
rewrite using own parser & bundler (#5, #6)
1 parent 6d84bcd commit 9c44a7e

7 files changed

Lines changed: 70 additions & 69 deletions

File tree

build.gradle.kts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,11 @@ tasks.compileTestGroovy {
4141

4242
dependencies {
4343
implementation (libs.openapi.processor.api)
44+
implementation (platform(libs.openapi.parser.bom))
45+
implementation (libs.openapi.parser.parser)
46+
implementation (libs.io.jackson)
4447

45-
implementation("io.swagger.parser.v3:swagger-parser:2.0.24") {
46-
exclude(group = "io.swagger.parser.v3", module = "swagger-parser-v2-converter")
47-
exclude(group = "io.swagger.core.v3", module = "swagger-annotations")
48-
}
49-
50-
testImplementation("io.github.java-diff-utils:java-diff-utils:4.9")
48+
testImplementation(libs.diff)
5149
testImplementation (platform(libs.groovy.bom))
5250
testImplementation ("org.apache.groovy:groovy")
5351
testImplementation ("org.apache.groovy:groovy-nio")
@@ -58,7 +56,7 @@ dependencies {
5856
testImplementation (libs.mockk)
5957
testImplementation (libs.logback)
6058

61-
testIntImplementation("io.github.java-diff-utils:java-diff-utils:4.9")
59+
testIntImplementation(libs.diff)
6260
testIntImplementation (platform(libs.groovy.bom))
6361
testIntImplementation ("org.apache.groovy:groovy")
6462
testIntImplementation ("org.apache.groovy:groovy-nio")

gradle/libs.versions.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ build-jdk = "17"
77
build-plugins = "2025.9"
88

99
api = "2024.2"
10-
#base = "2025.5.1"
10+
parser = "2025.5-SNAPSHOT"
1111

1212
junit = "5.9.3"
1313
jacoco = "0.8.7"
1414

1515
[libraries]
1616
openapi-processor-api = { module = "io.openapiprocessor:openapi-processor-api", version.ref = "api" }
17+
openapi-parser-bom = { module = "io.openapiprocessor:openapi-parser-bom", version.ref = "parser" }
18+
openapi-parser-parser = { module = "io.openapiprocessor:openapi-parser" }
19+
io-jackson = { module = "io.openapiprocessor:io-jackson" }
20+
diff = { module = "io.github.java-diff-utils:java-diff-utils", version = "4.16" }
1721

1822
logback = "ch.qos.logback:logback-classic:1.5.18"
1923

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

Lines changed: 22 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,20 @@
55

66
package 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+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2020 https://github.com/openapi-processor/openapi-processor-core
3+
* PDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package com.github.hauner.openapi.json.processor
7+
8+
import java.net.URI
9+
import java.nio.file.Paths
10+
11+
/**
12+
* convert a source string to a valid URI.
13+
*
14+
* if the source is an uri string, it converts it to a URI. If the source has no scheme it assumes
15+
* a local path and adds the file scheme (i.e., file:).
16+
*
17+
* @param source source path or url
18+
* @return a URI to the given source
19+
*/
20+
fun toURI(source: String): URI {
21+
try {
22+
val uri = URI(source)
23+
if (uri.scheme != null) {
24+
return uri
25+
}
26+
} catch (ignore: Exception) {
27+
// ignore
28+
}
29+
30+
// no scheme, assume a file path
31+
return Paths.get(source)
32+
.normalize()
33+
.toAbsolutePath()
34+
.toUri()
35+
}

src/main/resources/META-INF/services/com.github.hauner.openapi.api.OpenApiProcessor renamed to src/main/resources/META-INF/services/com.github.hauner.openapi.api.v2.OpenApiProcessor

File renamed without changes.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class JsonProcessorSpec extends Specification {
2222
def targetPath = [targetDir, 'openapi.json'].join(File.separator)
2323

2424
def options = [
25-
apiPath: new File(apiPath).canonicalPath,
25+
apiPath: new File(apiPath).absolutePath,
2626
targetDir: targetDir
2727
]
2828

src/test/resources/openapi.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
"title" : "sample api",
55
"version" : "1.0.0"
66
},
7-
"servers" : [ {
8-
"url" : "/"
9-
} ],
107
"paths" : {
118
"/ping" : {
129
"get" : {
@@ -34,11 +31,9 @@
3431
"description" : "sample endpoint with parameter",
3532
"parameters" : [ {
3633
"name" : "source",
37-
"in" : "query",
3834
"description" : "query, required, string",
35+
"in" : "query",
3936
"required" : true,
40-
"style" : "form",
41-
"explode" : true,
4237
"schema" : {
4338
"type" : "string"
4439
}
@@ -58,4 +53,4 @@
5853
}
5954
}
6055
}
61-
}
56+
}

0 commit comments

Comments
 (0)