1
+ package org.evomaster.json2yaml
2
+
3
+ import com.fasterxml.jackson.databind.ObjectMapper
4
+ import com.fasterxml.jackson.databind.node.ArrayNode
5
+ import com.fasterxml.jackson.databind.node.ObjectNode
6
+ import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
7
+ import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator
8
+ import io.swagger.v3.core.util.Yaml
9
+ import io.swagger.v3.parser.OpenAPIV3Parser
10
+ import java.io.File
11
+ import java.net.URL
12
+ import java.nio.file.Files
13
+ import java.nio.file.Paths
14
+ import kotlin.io.path.name
15
+
16
+
17
+ class Main {
18
+
19
+
20
+ companion object {
21
+
22
+ private val TESTCONFIG_YAML_MAPPER = ObjectMapper (YAMLFactory ().enable(YAMLGenerator .Feature .MINIMIZE_QUOTES ))
23
+ private val JSON_MAPPER = ObjectMapper ()
24
+
25
+ private val YAML_MAPPER = Yaml .mapper()
26
+ private val YAML_WRITER = Yaml .pretty()
27
+
28
+ @JvmStatic
29
+ fun main (args : Array <String >) {
30
+
31
+ if (args.isEmpty())
32
+ throw IllegalArgumentException (" args must be specified" )
33
+
34
+ when (args[0 ]) {
35
+ " authForResTest" -> {
36
+ if (args.size != 4 )
37
+ throw IllegalArgumentException (" to configure auth for ResTest, the args should be 4, ie., `authForResTest <testConfig.yaml path> <key> <value>`" )
38
+ configureAuthForResTest(args[1 ], args[2 ], args[3 ])
39
+ }
40
+ " jsonToYaml" -> {
41
+ if (args.size != 2 )
42
+ throw IllegalArgumentException (" to convert json to yaml, the args should be 2, ie., `jsonToYaml <openapi path>`" )
43
+ jsonToYaml(args[1 ])
44
+ }
45
+ " 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
+ try {
49
+ args[2 ].toInt()
50
+ }catch (ex: NumberFormatException ){
51
+ throw IllegalArgumentException (" port must be a int number" )
52
+ }
53
+ updateURLAndPort(args[1 ], args[2 ])
54
+ }
55
+ else -> {
56
+ throw IllegalArgumentException (" only able to configure `authForResTest`, `jsonToYaml` and `updateURLAndPort`" )
57
+ }
58
+ }
59
+ }
60
+
61
+ fun updateURLAndPort (openapiFile : String , port : String , saveFile : String = openapiFile){
62
+ val path = Paths .get(openapiFile)
63
+ if (! Files .exists(path)) throw IllegalArgumentException (" File cannot found" )
64
+
65
+ val mapper = if (openapiFile.endsWith(" .json" )) JSON_MAPPER else YAML_MAPPER
66
+ val openApi = mapper.readTree(path.toFile()) as ObjectNode
67
+ val writer = if (openapiFile.endsWith(" .json" )) mapper.writer() else YAML_WRITER
68
+
69
+ if (openApi.has(" swagger" )){
70
+ openApi.put(" host" , " localhost:$port " )
71
+ if (! openApi.has(" schemes" )){
72
+ openApi.putArray(" schemes" ).add(" http" )
73
+ }
74
+ }else if (openApi.has(" openapi" )){
75
+
76
+ if (openApi.has(" servers" ) && openApi.get(" servers" ).isArray && ! openApi.get(" servers" ).isEmpty){
77
+ val all = (openApi.get(" servers" ) as ArrayNode ).map { o->
78
+ val resourcePath = getResourcePath(o.get(" url" ).asText())? : " "
79
+ mapper.createObjectNode().apply {
80
+ put(" url" ," http://localhost:$port$resourcePath " )
81
+ }
82
+ }
83
+ openApi.putArray(" servers" ).addAll(all)
84
+
85
+ }else {
86
+ val urlNode = mapper.createObjectNode()
87
+ urlNode.put(" url" , " http://localhost:$port " )
88
+ openApi.putArray(" servers" ).add(urlNode)
89
+ }
90
+ }
91
+ writer.writeValue(Paths .get(saveFile).toFile(), openApi)
92
+ }
93
+
94
+ private fun getResourcePath (path : String ): String? {
95
+ try {
96
+ val url = URL (path)
97
+ return url.file
98
+ }catch (e: Exception ){
99
+ return null
100
+ }
101
+
102
+ }
103
+
104
+ fun configureAuthForResTest (file : String , headerKey : String , headerValue : String , saveFile : String = file){
105
+ val path = Paths .get(file)
106
+ if (! Files .exists(path)) throw IllegalArgumentException (" File cannot found" )
107
+ val root = TESTCONFIG_YAML_MAPPER .readTree(path.toFile()) as ObjectNode
108
+
109
+ val authNode = TESTCONFIG_YAML_MAPPER .createObjectNode()
110
+ authNode.put(headerKey,headerValue)
111
+
112
+
113
+ (root.get(" auth" ) as ObjectNode ).put(" headerParams" , authNode)
114
+
115
+ TESTCONFIG_YAML_MAPPER .writer().writeValue(Paths .get(saveFile).toFile(), root)
116
+ }
117
+
118
+ fun jsonToYaml (jsonFile : String , saveDir : String? = null){
119
+ val path = Paths .get(jsonFile)
120
+ if (! Files .exists(path)) throw IllegalArgumentException (" File cannot found" )
121
+
122
+ try {
123
+ val openApi = OpenAPIV3Parser ().read(jsonFile)
124
+ val yaml = Yaml .pretty().writeValueAsString(openApi)
125
+ val dir = (saveDir? : path.parent.toAbsolutePath().toString()).run {
126
+ if (! endsWith(File .separator))
127
+ " ${this }${File .separator} "
128
+ else
129
+ this
130
+ }
131
+
132
+ val output = path.fileName.name.replace(" .json" , " .yaml" )
133
+ Files .write(Paths .get(" $dir$output " ), yaml.toByteArray())
134
+ } catch (e: Exception ) {
135
+ e.printStackTrace()
136
+ }
137
+ }
138
+
139
+ }
140
+
141
+ }
0 commit comments