-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
230 lines (217 loc) · 4.84 KB
/
main.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"os"
"github.com/getkin/kin-openapi/openapi3"
"gopkg.in/yaml.v3"
)
// function showing how to use openapi3 to generate an openapi spec
// using golang. intentionally contains a number of contrived examples
// and different ways of doing the same things to illustrate a number
// of options
func main() {
//build base of spec
s := openapi3.T{
OpenAPI: "3.0.0",
}
//build info section
info := &openapi3.Info{
Title: "Swagger Petstore",
License: &openapi3.License{
Name: "MIT",
},
Version: "1.0.0",
}
//add info to spec
s.Info = info
//tags
tags := openapi3.Tags{
{
Name: "test1",
Description: "something",
},
{
Name: "test2",
Description: "something2",
},
}
//add the tags to our doc
s.Tags = tags
//build a basic server
server1 := openapi3.Server{
URL: "http://petstore.swagger.io/v1",
}
//add the server to our spec
s.AddServer(&server1)
//build a server with a variable and description
server2 := openapi3.Server{
URL: "http://petstore.{env}.swagger.io/v1",
Description: "another server",
Variables: map[string]*openapi3.ServerVariable{
"env": {
Default: "dev",
Enum: []string{
"dev",
"staging",
},
},
},
}
//add another server to our spec
s.AddServer(&server2)
//build /pets operation
//build descriptions because we need to attach them to our response as *string
var description200 = "a paged array of pets"
var descriptionDefault = "unexpected error"
getPets := openapi3.Operation{
Tags: []string{"pets"},
Summary: "List all pets",
OperationID: "listPets",
Parameters: openapi3.Parameters{
&openapi3.ParameterRef{
Ref: "",
Value: &openapi3.Parameter{
Name: "limit",
In: "query",
Description: "How many items to return at one time (max 100)",
Required: false,
Schema: &openapi3.SchemaRef{
Ref: "",
Value: &openapi3.Schema{
Type: "integer",
Format: "int32",
},
},
},
},
},
Responses: openapi3.Responses{
"200": &openapi3.ResponseRef{
Ref: "",
Value: &openapi3.Response{
Description: &description200,
Headers: openapi3.Headers{
"x-next": &openapi3.HeaderRef{
Ref: "",
Value: &openapi3.Header{
Parameter: openapi3.Parameter{
Description: "A link to the next page of responses",
Schema: &openapi3.SchemaRef{
Ref: "",
Value: &openapi3.Schema{
Type: "string",
},
},
},
},
},
},
Content: openapi3.Content{
"application/json": &openapi3.MediaType{
Schema: &openapi3.SchemaRef{
Ref: "#/components/schemas/Pets",
},
},
},
},
},
"default": &openapi3.ResponseRef{
Ref: "",
Value: &openapi3.Response{
Headers: nil,
Content: openapi3.Content{
"application/json": &openapi3.MediaType{
Schema: &openapi3.SchemaRef{
Ref: "#/components/schemas/Error",
},
},
},
Description: &descriptionDefault,
Links: nil,
},
},
},
}
//build pathitem mapping in our getPets method
pets := openapi3.PathItem{
Get: &getPets,
}
//build paths object and add out pathitem(s)
paths := openapi3.Paths{
"/pets": &pets,
}
s.Paths = paths
//build components
components := openapi3.Components{
Schemas: openapi3.Schemas{
"Pet": &openapi3.SchemaRef{
Ref: "",
Value: &openapi3.Schema{
Required: []string{"id", "name"},
//Enum: []interface{}{"VALUE1", "VALUE2"},
Properties: openapi3.Schemas{
"id": &openapi3.SchemaRef{
Ref: "",
Value: &openapi3.Schema{
Type: "integer",
Format: "int64",
},
},
"name": &openapi3.SchemaRef{
Ref: "",
Value: &openapi3.Schema{
Type: "string",
},
},
"tag": &openapi3.SchemaRef{
Ref: "",
Value: &openapi3.Schema{
Type: "string",
},
},
},
},
},
"Pets": &openapi3.SchemaRef{
Ref: "",
Value: &openapi3.Schema{
Type: "array",
Items: &openapi3.SchemaRef{
Ref: "#/components/schemas/Pet",
},
},
},
"Error": &openapi3.SchemaRef{
Ref: "",
Value: &openapi3.Schema{
Required: []string{"code", "message"},
Properties: openapi3.Schemas{
"code": &openapi3.SchemaRef{
Ref: "",
Value: &openapi3.Schema{
Type: "integer",
Format: "int32",
},
},
"message": &openapi3.SchemaRef{
Ref: "",
Value: &openapi3.Schema{
Type: "string",
},
},
},
},
},
},
}
s.Components = components
//marshall spec into yaml
d, err := yaml.Marshal(&s)
if err != nil {
panic(err)
}
//write the yaml to a file
err = os.WriteFile("output.yaml", d, 0644)
if err != nil {
panic(err)
}
}