-
Notifications
You must be signed in to change notification settings - Fork 532
/
swagger-combine.go
110 lines (92 loc) · 2.44 KB
/
swagger-combine.go
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
package swaggercombine
import (
"context"
"encoding/json"
"io"
"os"
"regexp"
"github.com/ignite/cli/ignite/pkg/cmdrunner/exec"
"github.com/ignite/cli/ignite/pkg/nodetime"
)
// Config represent swagger-combine config.
type Config struct {
Swagger string `json:"swagger"`
Info Info `json:"info"`
APIs []API `json:"apis"`
}
type Info struct {
Title string `json:"title"`
Name string `json:"name"`
Description string `json:"description"`
}
type API struct {
ID string `json:"-"`
URL string `json:"url"`
OperationIDs OperationIDs `json:"operationIds"`
Dereference struct {
Circular string `json:"circular"`
} `json:"dereference"`
}
type OperationIDs struct {
Rename map[string]string `json:"rename"`
}
var opReg = regexp.MustCompile(`(?m)operationId.+?(\w+)`)
// AddSpec adds a new OpenAPI spec to Config by path in the fs and unique id of spec.
func (c *Config) AddSpec(id, path string) error {
// make operationId fields unique.
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
content, err := io.ReadAll(f)
if err != nil {
return err
}
ops := opReg.FindAllStringSubmatch(string(content), -1)
rename := make(map[string]string, len(ops))
for _, op := range ops {
o := op[1]
rename[o] = id + o
}
// add api with replaced operation ids.
c.APIs = append(c.APIs, API{
ID: id,
URL: path,
OperationIDs: OperationIDs{Rename: rename},
// Added due to https://github.com/maxdome/swagger-combine/pull/110 after enabling more services to be generated in #835
Dereference: struct {
Circular string `json:"circular"`
}(struct{ Circular string }{Circular: "ignore"}),
})
return nil
}
// Combine combines openapi specs into one and saves to out path.
// specs is a spec id-fs path pair.
func Combine(ctx context.Context, c Config, out string) error {
command, cleanup, err := nodetime.Command(nodetime.CommandSwaggerCombine)
if err != nil {
return err
}
defer cleanup()
f, err := os.CreateTemp("", "*.json")
if err != nil {
return err
}
defer os.Remove(f.Name())
if err := json.NewEncoder(f).Encode(c); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
command = append(command, []string{
f.Name(),
"-o", out,
"-f", "yaml",
"--continueOnConflictingPaths", "true",
"--includeDefinitions", "true",
}...)
// execute the command.
return exec.Exec(ctx, command, exec.IncludeStdLogsToError())
}