Skip to content

Commit 0a57469

Browse files
committed
support convert openapi v2 to openapi v3 (for resttestgen v2)
1 parent cb0bccb commit 0a57469

File tree

1 file changed

+20
-7
lines changed
  • bb-exp-util/src/main/kotlin/org/evomaster/json2yaml

1 file changed

+20
-7
lines changed

bb-exp-util/src/main/kotlin/org/evomaster/json2yaml/Main.kt

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode
55
import com.fasterxml.jackson.databind.node.ObjectNode
66
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
77
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator
8+
import io.swagger.v3.core.util.Json
89
import io.swagger.v3.core.util.Yaml
910
import io.swagger.v3.parser.OpenAPIV3Parser
1011
import java.io.File
@@ -24,6 +25,7 @@ class Main {
2425

2526
private val YAML_MAPPER = Yaml.mapper()
2627
private val YAML_WRITER = Yaml.pretty()
28+
private val OPENAPI_V3_WRITER = Json.pretty()
2729

2830
@JvmStatic
2931
fun main(args: Array<String>) {
@@ -43,28 +45,32 @@ class Main {
4345
jsonToYaml(args[1])
4446
}
4547
"updateURLAndPort" -> {
46-
if (args.size != 3)
47-
throw IllegalArgumentException("to update url and port, the args should be 3, ie., `updateURLAndPort <openapi path> <port>`")
48+
if (args.size != 4 && args.size != 3)
49+
throw IllegalArgumentException("to update url and port, the args should be 3 or 4, ie., `updateURLAndPort <openapi path> <port> <openapi V2ToV3>`")
4850
try {
4951
args[2].toInt()
52+
if (args.size == 4)
53+
args[3].toBoolean()
5054
}catch (ex: NumberFormatException){
51-
throw IllegalArgumentException("port must be a int number")
55+
throw IllegalArgumentException("port must be a int number and <openapi V2ToV3> must be boolean")
5256
}
53-
updateURLAndPort(args[1], args[2])
57+
updateURLAndPort(args[1], args[2], args.size == 4 && args[3].toBoolean())
5458
}
5559
else ->{
5660
throw IllegalArgumentException("only able to configure `authForResTest`, `jsonToYaml` and `updateURLAndPort`")
5761
}
5862
}
5963
}
6064

61-
fun updateURLAndPort(openapiFile: String, port: String, saveFile: String = openapiFile){
65+
fun updateURLAndPort(openapiFile: String, port: String, convertToOpenAPIV3: Boolean,saveFile: String = openapiFile){
6266
val path = Paths.get(openapiFile)
6367
if (!Files.exists(path)) throw IllegalArgumentException("File cannot found")
6468

65-
val mapper = if (openapiFile.endsWith(".json")) JSON_MAPPER else YAML_MAPPER
69+
val isJson = openapiFile.endsWith(".json")
70+
71+
val mapper = if (isJson) JSON_MAPPER else YAML_MAPPER
6672
val openApi = mapper.readTree(path.toFile()) as ObjectNode
67-
val writer = if (openapiFile.endsWith(".json")) mapper.writer() else YAML_WRITER
73+
val writer = if (isJson) mapper.writer() else YAML_WRITER
6874

6975
if (openApi.has("swagger")){
7076
openApi.put("host", "localhost:$port")
@@ -88,7 +94,14 @@ class Main {
8894
openApi.putArray("servers").add(urlNode)
8995
}
9096
}
97+
98+
9199
writer.writeValue(Paths.get(saveFile).toFile(), openApi)
100+
101+
if (openApi.has("swagger") && convertToOpenAPIV3){
102+
val openapiV3 = OpenAPIV3Parser().read(saveFile)
103+
(if (isJson) OPENAPI_V3_WRITER else YAML_WRITER).writeValue(Paths.get(saveFile).toFile(), openapiV3)
104+
}
92105
}
93106

94107
private fun getResourcePath(path: String): String?{

0 commit comments

Comments
 (0)