Skip to content

Commit 0e94ad8

Browse files
committed
support yaml to json
1 parent 0a57469 commit 0e94ad8

File tree

1 file changed

+27
-9
lines changed
  • bb-exp-util/src/main/kotlin/org/evomaster/json2yaml

1 file changed

+27
-9
lines changed

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

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,32 +45,42 @@ class Main {
4545
jsonToYaml(args[1])
4646
}
4747
"updateURLAndPort" -> {
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+
if (args.size > 5 || args.size < 3)
49+
throw IllegalArgumentException("to update url and port, the args should be 3--5, ie., `updateURLAndPort <openapi path> <port> <openapi V2ToV3> <output format, json or yaml>`")
50+
val format = if (args.size != 5) null else args[4]
5051
try {
5152
args[2].toInt()
52-
if (args.size == 4)
53+
if (args.size >= 4)
5354
args[3].toBoolean()
55+
if (format != null && listOf("json","yaml").none { format.equals(it, ignoreCase = true) })
56+
throw IllegalArgumentException("output format could be either json or yaml")
5457
}catch (ex: NumberFormatException){
5558
throw IllegalArgumentException("port must be a int number and <openapi V2ToV3> must be boolean")
5659
}
57-
updateURLAndPort(args[1], args[2], args.size == 4 && args[3].toBoolean())
60+
updateURLAndPort(args[1], args[2], args.size == 4 && args[3].toBoolean(), format)
5861
}
5962
else ->{
6063
throw IllegalArgumentException("only able to configure `authForResTest`, `jsonToYaml` and `updateURLAndPort`")
6164
}
6265
}
6366
}
6467

65-
fun updateURLAndPort(openapiFile: String, port: String, convertToOpenAPIV3: Boolean,saveFile: String = openapiFile){
68+
fun updateURLAndPort(openapiFile: String, port: String, convertToOpenAPIV3: Boolean, format: String?, saveFile: String = openapiFile){
6669
val path = Paths.get(openapiFile)
6770
if (!Files.exists(path)) throw IllegalArgumentException("File cannot found")
6871

6972
val isJson = openapiFile.endsWith(".json")
7073

7174
val mapper = if (isJson) JSON_MAPPER else YAML_MAPPER
7275
val openApi = mapper.readTree(path.toFile()) as ObjectNode
73-
val writer = if (isJson) mapper.writer() else YAML_WRITER
76+
77+
val jsonoutput = (format == null && isJson) || format.equals("json", ignoreCase = true)
78+
79+
val writer = if (jsonoutput == isJson) mapper.writer() else if (jsonoutput) OPENAPI_V3_WRITER else YAML_WRITER
80+
81+
val output = if (isJson && !jsonoutput) path.fileName.name.replace(".json", ".yaml")
82+
else if (!isJson && jsonoutput) path.fileName.name.replace(".yaml", ".json")
83+
else path.fileName.name
7484

7585
if (openApi.has("swagger")){
7686
openApi.put("host", "localhost:$port")
@@ -95,12 +105,20 @@ class Main {
95105
}
96106
}
97107

108+
val dir = path.parent.toAbsolutePath().toString().run {
109+
if (!endsWith(File.separator))
110+
"${this}${File.separator}"
111+
else
112+
this
113+
}
114+
115+
val savePath = "$dir$output"
98116

99-
writer.writeValue(Paths.get(saveFile).toFile(), openApi)
117+
writer.writeValue(Paths.get(savePath).toFile(), openApi)
100118

101119
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)
120+
val openapiV3 = OpenAPIV3Parser().read(savePath)
121+
(if (jsonoutput) OPENAPI_V3_WRITER else YAML_WRITER).writeValue(Paths.get(savePath).toFile(), openapiV3)
104122
}
105123
}
106124

0 commit comments

Comments
 (0)