forked from go-openapi/analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_fixtures.go
More file actions
199 lines (162 loc) · 4.02 KB
/
Copy pathmigrate_fixtures.go
File metadata and controls
199 lines (162 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// +build ignore
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"gopkg.in/yaml.v3"
)
func convertSwagger2ToOpenAPI3(data map[string]interface{}) map[string]interface{} {
// Check if already OpenAPI 3
if _, ok := data["openapi"]; ok {
return data
}
// Check if Swagger 2.0
if swagger, ok := data["swagger"].(string); !ok || swagger != "2.0" {
return data
}
// Create new OpenAPI 3 structure
result := make(map[string]interface{})
result["openapi"] = "3.0.0"
// Copy basic fields
for _, field := range []string{"info", "externalDocs", "tags", "security", "paths"} {
if val, ok := data[field]; ok {
result[field] = val
}
}
// Create components object
components := make(map[string]interface{})
if defs, ok := data["definitions"]; ok {
components["schemas"] = defs
}
if params, ok := data["parameters"]; ok {
components["parameters"] = params
}
if responses, ok := data["responses"]; ok {
components["responses"] = responses
}
if secDefs, ok := data["securityDefinitions"]; ok {
components["securitySchemes"] = secDefs
}
if len(components) > 0 {
result["components"] = components
}
// Convert servers from host/basePath/schemes
if host, hasHost := data["host"]; hasHost {
servers := make([]map[string]interface{}, 0)
schemes := []string{"http"}
if s, ok := data["schemes"].([]interface{}); ok {
schemes = nil
for _, scheme := range s {
if str, ok := scheme.(string); ok {
schemes = append(schemes, str)
}
}
}
basePath := ""
if bp, ok := data["basePath"].(string); ok {
basePath = bp
}
for _, scheme := range schemes {
server := map[string]interface{}{
"url": fmt.Sprintf("%s://%s%s", scheme, host, basePath),
}
servers = append(servers, server)
}
if len(servers) > 0 {
result["servers"] = servers
}
}
// Copy extension fields
for key, val := range data {
if strings.HasPrefix(key, "x-") {
result[key] = val
}
}
return result
}
func processFile(path string) error {
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
var content map[string]interface{}
// Try to determine format and parse
ext := strings.ToLower(filepath.Ext(path))
isYAML := ext == ".yaml" || ext == ".yml"
isJSON := ext == ".json"
if isYAML {
if err := yaml.Unmarshal(data, &content); err != nil {
return fmt.Errorf("yaml unmarshal: %w", err)
}
} else if isJSON {
if err := json.Unmarshal(data, &content); err != nil {
return fmt.Errorf("json unmarshal: %w", err)
}
} else {
return nil // Skip unknown file types
}
// Check if it's a Swagger spec (not all files are specs)
if _, hasSwagger := content["swagger"]; !hasSwagger {
if _, hasOpenAPI := content["openapi"]; !hasOpenAPI {
return nil // Not a spec file, skip
}
}
// Convert the spec
converted := convertSwagger2ToOpenAPI3(content)
// Check if anything changed
if fmt.Sprintf("%v", content) == fmt.Sprintf("%v", converted) {
return nil
}
// Write back
var output []byte
if isYAML {
output, err = yaml.Marshal(converted)
if err != nil {
return fmt.Errorf("yaml marshal: %w", err)
}
} else {
output, err = json.MarshalIndent(converted, "", " ")
if err != nil {
return fmt.Errorf("json marshal: %w", err)
}
output = append(output, '\n')
}
if err := ioutil.WriteFile(path, output, 0644); err != nil {
return fmt.Errorf("write file: %w", err)
}
fmt.Printf("✓ Converted %s\n", path)
return nil
}
func main() {
fixturesDir := "fixtures"
if len(os.Args) > 1 {
fixturesDir = os.Args[1]
}
count := 0
err := filepath.Walk(fixturesDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
ext := strings.ToLower(filepath.Ext(path))
if ext == ".yaml" || ext == ".yml" || ext == ".json" {
if err := processFile(path); err != nil {
log.Printf("Error processing %s: %v", path, err)
} else {
count++
}
}
return nil
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("\nProcessed %d files\n", count)
}