@@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode
5
5
import com.fasterxml.jackson.databind.node.ObjectNode
6
6
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
7
7
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator
8
+ import io.swagger.v3.core.util.Json
8
9
import io.swagger.v3.core.util.Yaml
9
10
import io.swagger.v3.parser.OpenAPIV3Parser
10
11
import java.io.File
@@ -24,6 +25,7 @@ class Main {
24
25
25
26
private val YAML_MAPPER = Yaml .mapper()
26
27
private val YAML_WRITER = Yaml .pretty()
28
+ private val OPENAPI_V3_WRITER = Json .pretty()
27
29
28
30
@JvmStatic
29
31
fun main (args : Array <String >) {
@@ -43,28 +45,32 @@ class Main {
43
45
jsonToYaml(args[1 ])
44
46
}
45
47
" 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 >`" )
48
50
try {
49
51
args[2 ].toInt()
52
+ if (args.size == 4 )
53
+ args[3 ].toBoolean()
50
54
}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 " )
52
56
}
53
- updateURLAndPort(args[1 ], args[2 ])
57
+ updateURLAndPort(args[1 ], args[2 ], args.size == 4 && args[ 3 ].toBoolean() )
54
58
}
55
59
else -> {
56
60
throw IllegalArgumentException (" only able to configure `authForResTest`, `jsonToYaml` and `updateURLAndPort`" )
57
61
}
58
62
}
59
63
}
60
64
61
- fun updateURLAndPort (openapiFile : String , port : String , saveFile : String = openapiFile){
65
+ fun updateURLAndPort (openapiFile : String , port : String , convertToOpenAPIV3 : Boolean , saveFile : String = openapiFile){
62
66
val path = Paths .get(openapiFile)
63
67
if (! Files .exists(path)) throw IllegalArgumentException (" File cannot found" )
64
68
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
66
72
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
68
74
69
75
if (openApi.has(" swagger" )){
70
76
openApi.put(" host" , " localhost:$port " )
@@ -88,7 +94,14 @@ class Main {
88
94
openApi.putArray(" servers" ).add(urlNode)
89
95
}
90
96
}
97
+
98
+
91
99
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
+ }
92
105
}
93
106
94
107
private fun getResourcePath (path : String ): String? {
0 commit comments