-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument.go
More file actions
218 lines (194 loc) · 5.83 KB
/
document.go
File metadata and controls
218 lines (194 loc) · 5.83 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package openapi
import (
"encoding/json"
)
// ExternalDocs represents external documentation
type ExternalDocs struct {
Description string `json:"description,omitempty"`
URL string `json:"url"`
}
// SecurityRequirement represents a security requirement
type SecurityRequirement map[string][]string
// Document represents the root OpenAPI v3 document
type Document struct {
OpenAPI string `json:"openapi"`
Info Info `json:"info"`
JSONSchemaDialect string `json:"jsonSchemaDialect,omitempty"`
Servers []Server `json:"servers,omitempty"`
Paths map[string]PathItem `json:"paths"`
Webhooks map[string]PathItem `json:"webhooks,omitempty"`
Components *Components `json:"components,omitempty"`
Security []SecurityRequirement `json:"security,omitempty"`
Tags []Tag `json:"tags,omitempty"`
ExternalDocs *ExternalDocs `json:"externalDocs,omitempty"`
}
// NewDocument creates a new OpenAPI document with basic info
func NewDocument(title, version string) *Document {
return &Document{
OpenAPI: "3.1.0",
Info: Info{
Title: title,
Version: version,
},
Paths: make(map[string]PathItem),
Webhooks: make(map[string]PathItem),
Tags: []Tag{},
}
}
// WithInfo sets additional info for the OpenAPI document
func (d *Document) WithInfo(description, termsOfService string) *Document {
d.Info.Description = description
d.Info.TermsOfService = termsOfService
return d
}
// WithContact adds contact information to the OpenAPI document
func (d *Document) WithContact(name, url, email string) *Document {
d.Info.Contact = &Contact{
Name: name,
URL: url,
Email: email,
}
return d
}
// WithLicense adds license information to the OpenAPI document
func (d *Document) WithLicense(name, url string) *Document {
d.Info.License = &License{
Name: name,
URL: url,
}
return d
}
// AddServer adds a server to the document
func (d *Document) AddServer(url, description string) *Document {
d.Servers = append(d.Servers, Server{
URL: url,
Description: description,
Variables: make(map[string]ServerVariable),
})
return d
}
// AddTag adds a tag to the document
func (d *Document) AddTag(name, description string) *Document {
d.Tags = append(d.Tags, Tag{
Name: name,
Description: description,
})
return d
}
// AddTagWithDocs adds a tag with external documentation
func (d *Document) AddTagWithDocs(name, description, docsURL, docsDescription string) *Document {
d.Tags = append(d.Tags, Tag{
Name: name,
Description: description,
ExternalDocs: &ExternalDocs{
URL: docsURL,
Description: docsDescription,
},
})
return d
}
// SetExternalDocs sets external documentation for the entire API
func (d *Document) SetExternalDocs(url, description string) *Document {
d.ExternalDocs = &ExternalDocs{
URL: url,
Description: description,
}
return d
}
// AddPath adds a path to the document with an empty PathItem
func (d *Document) AddPath(path string) *PathItem {
if d.Paths == nil {
d.Paths = make(map[string]PathItem)
}
pathItem := PathItem{}
d.Paths[path] = pathItem
return &pathItem
}
// GetPath gets a path item or creates it if it doesn't exist
func (d *Document) GetPath(path string) *PathItem {
if d.Paths == nil {
d.Paths = make(map[string]PathItem)
}
if pathItem, exists := d.Paths[path]; exists {
return &pathItem
}
return d.AddPath(path)
}
// SetPath sets a complete path item
func (d *Document) SetPath(path string, pathItem PathItem) *Document {
if d.Paths == nil {
d.Paths = make(map[string]PathItem)
}
d.Paths[path] = pathItem
return d
}
// AddOperation adds an operation to a specific path and method
func (d *Document) AddOperation(path, method string, operation Operation) *Document {
pathItem := d.GetPath(path)
switch method {
case "GET":
pathItem.Get = &operation
case "POST":
pathItem.Post = &operation
case "PUT":
pathItem.Put = &operation
case "DELETE":
pathItem.Delete = &operation
case "PATCH":
pathItem.Patch = &operation
case "HEAD":
pathItem.Head = &operation
case "OPTIONS":
pathItem.Options = &operation
case "TRACE":
pathItem.Trace = &operation
}
d.Paths[path] = *pathItem
return d
}
// AddComponents adds or updates components section
func (d *Document) AddComponents() *Components {
if d.Components == nil {
d.Components = &Components{
Schemas: make(map[string]*Schema),
Responses: make(map[string]Response),
Parameters: make(map[string]Parameter),
Examples: make(map[string]Example),
RequestBodies: make(map[string]RequestBody),
Headers: make(map[string]Header),
SecuritySchemes: make(map[string]SecurityScheme),
Links: make(map[string]Link),
Callbacks: make(map[string]Callback),
}
}
return d.Components
}
// AddSchema adds a schema to components
func (d *Document) AddSchema(name string, schema Schema) *Document {
components := d.AddComponents()
components.Schemas[name] = &schema
return d
}
// AddSecurityScheme adds a security scheme to components
func (d *Document) AddSecurityScheme(name string, scheme SecurityScheme) *Document {
components := d.AddComponents()
components.SecuritySchemes[name] = scheme
return d
}
// AddSecurityRequirement adds a security requirement at document level
func (d *Document) AddSecurityRequirement(requirement SecurityRequirement) *Document {
d.Security = append(d.Security, requirement)
return d
}
// ToJSON converts the OpenAPI document to JSON
func (d *Document) ToJSON() ([]byte, error) {
return json.MarshalIndent(d, "", " ")
}
// ToJSONString converts the OpenAPI document to JSON string
func (d *Document) ToJSONString() (string, error) {
data, err := d.ToJSON()
if err != nil {
return "", err
}
return string(data), nil
}